您的位置:首页 > 其它

线段树 延迟更新

2015-03-20 11:27 106 查看
fafu 1008

#include<stdio.h>
#include<string.h>
const int N=100000+5;
int n, q;
int a
, tree[N<<2], t[N<<2];

void build(int rt, int l, int r)
{
if(l==r)
tree[rt]=10;
else
{
int m=(l+r)>>1;
build(rt<<1, l, m);
build((rt<<1)+1, m+1, r);

tree[rt]=tree[rt<<1]+tree[(rt<<1)+1];
}
}

void pushDown(int rt, int m)
{
if(t[rt]!=-1)
{
t[rt<<1]=t[rt];
t[(rt<<1)+1]=t[rt];
tree[rt<<1]=t[rt]*(m-(m>>1));
tree[(rt<<1)+1]=t[rt]*(m>>1);
t[rt]=-1;
}
}

void update(int rt, int l, int r, int x, int y, int v)
{
if(x<=l && r<=y)
{
t[rt]=v;
tree[rt]=(r-l+1)*v;
return;
}
pushDown(rt, r-l+1);
int m=(l+r)>>1;
if(x<=m)
update(rt<<1, l, m, x, y, v);
if(m<y)
update((rt<<1)+1, m+1, r, x, y, v);
tree[rt]=tree[rt<<1]+tree[(rt<<1)+1];
}

int main()
{
int T;
scanf("%d", &T);
for(int k=1; k<=T; k++)
{
scanf("%d%d", &n, &q);
int x, y, v;
build(1, 1, n);
memset(t, -1, sizeof(t));
for(int i=0; i<q; i++)
{
scanf("%d%d%d", &x, &y, &v);
int mi=x<y?x:y;
int ma=x>y?x:y;
update(1, 1, n, mi, ma, v);
}
printf("Case %d: The total value of the stick is %d.\n", k, tree[1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: