线段树 延迟更新
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; }
相关文章推荐
- Poj3468(线段树延迟更新)
- hdu1698 Just a Hook 线段树更新延迟标记
- poj 3468 A Simple Problem with Integers(线段树、延迟更新)
- FZU Problem 2105 Digits Count线段树之延迟更新(插线取线)
- codeforce 315B Sereja and Array 【水题,封顶距离统计 OR 线段树延迟更新】
- 线段树(4)--poj3468(区间更新 延迟更新)
- poj3468 A Simple Problem with Integers 线段树 更新延迟
- [uva11992]Fast Matrix Operations(多延迟标记,线段树,区间更新)
- poj2777 Count Color线段树延迟更新,二进制状态记录
- poj3667 Hotel 线段树延迟更新 区间合并
- poj4047(线段树+延迟更新)
- 【线段树延迟更新】Codeforces Round #104 (Div. 1) E
- poj 3468 A Simple Problem with Integers(线段树、延迟更新)
- zoj 1610 Count the Colors(线段树延迟更新)
- hdu1698 Just a Hook 线段树延迟标记区间更新 求加权区间总和
- poj2777(延迟更新+线段树)
- 【线段树延迟更新】HDU 3275
- hdu 5023 线段树延迟更新+状态压缩
- 线段树区间更新区间求和(转延迟标记精讲)
- poj3468---线段树----区间单点更新、区间和查询----有延迟标记