首页 > 其他 > 详细

hdu1698(区间延迟更新+区间求和)

时间:2014-02-14 22:48:22      阅读:407      评论:0      收藏:0      [点我收藏+]
1013ms G++代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int NUM = 100010;
typedef struct node
{
int left,mid,right,key;
}node;
node T[ NUM<<2 ];
int flag[ NUM<<2 ];
int a[NUM];
void create(int u,int l,int r)
{
T[u].left = l;
T[u].right = r;
T[u].mid = (l+r)>>1;
flag[ u ] = 0;
if(l == r)
{
T[u].key = 1;
return;
}
int mid = (l+r)>>1;
create(u+u,l,mid);
create(u+u+1,mid+1,r);
T[u].key = T[u+u].key+T[u+u+1].key;
}
void PushDown(int u,int m)//把当前结点的信息更新给儿子结点
{
if (flag[u] !=0 )
{
flag[u+u] = flag[u];
flag[u+u+1] = flag[u];
T[u+u].key = flag[u] * (m - (m >> 1));
T[u+u+1].key = flag[u] * (m >> 1);
flag[u] = 0;
}
}
void change(int from,int to,int newKey ,int i)//延迟更新
{
if(from <= T[i].left && to >= T[i].right)
{
T[i].key = newKey*(T[i].right - T[i].left +1);
flag[i] = newKey;
return ;
}
PushDown(i,T[i].right - T[i].left + 1);
if(from <= T[i].mid)
change( from , to ,newKey, i+i );
if(to > T[i].mid)
change( from , to ,newKey, i+i+1 );
T[i].key = T[i+i].key + T[i+i+1].key;
}
int main()
{
int n,t,q,x,y,ca=1,nn,z;
while(scanf("%d",&t) !=EOF)
{
while(t--)
{
scanf("%d",&n);
scanf("%d",&q);
create(1,1,n);
for(int i = 1; i <= q; i++)
{
scanf("%d %d %d",&x,&y,&z);
change(x,y,z,1);
}
printf("Case %d: The total value of the hook is %d.\n",ca++,T[1].key);
}
}
return 0;
}

 

本文出自 “Qero” 博客,请务必保留此出处http://8590696.blog.51cto.com/8580696/1358901

hdu1698(区间延迟更新+区间求和)

原文:http://8590696.blog.51cto.com/8580696/1358901

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!