标签:int php value wan input cond cut next update
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698
题目:
思路:
懒惰标记法,延迟更新。没学过这个知识点的,可以先学习一下。网上有篇介绍该知识点的博客:http://blog.csdn.net/zip_fan/article/details/46775633
代码:
1 #include <cstdio> 2 const int N=4e5; 3 struct node{ 4 int l,r; 5 int lazy,sum; 6 }tree[N]; 7 int n; 8 void pushup(int i){//更新父节点 9 tree[i].sum=tree[(i*2)+1].sum+tree[i*2].sum; 10 } 11 void pushdown(int i){//更新子节点 12 if(tree[i].lazy){ 13 tree[2*i].lazy=tree[2*i+1].lazy=tree[i].lazy;//将子节点也懒惰标记 14 tree[2*i].sum=(tree[2*i].r-tree[2*i].l+1)*tree[2*i].lazy;//sum会等于长度值*标记值 15 tree[2*i+1].sum=(tree[2*i+1].r-tree[2*i+1].l+1)*tree[2*i+1].lazy; 16 tree[i].lazy=0;//更新完,取消该节点的标记 17 } 18 } 19 void build(int bg,int ed,int i){ 20 if(i>4*n) return; 21 tree[i].l=bg; 22 tree[i].r=ed; 23 tree[i].lazy=0;//多个测试样例,注意初始化 24 if (bg == ed) tree[i].sum=1; 25 else{ 26 int mid=(bg+ed)/2; 27 build(bg, mid, 2*i); 28 build(mid+1, ed, 2*i+1); 29 pushup(i);//回溯更新父节点 30 31 } 32 } 33 void update(int bg,int ed,int i,int v){ 34 if(bg<=tree[i].l && tree[i].r<=ed){ 35 tree[i].lazy=v; 36 tree[i].sum=(tree[i].r-tree[i].l+1)*v; 37 return ; 38 } 39 pushdown(i);//当用到该节点时,就向下更新 40 int mid=(tree[i].r+tree[i].l)/2; 41 if(ed<=mid) update(bg, ed, 2*i, v);//该区间完全在左子树 42 else if(bg>mid) update(bg, ed, 2*i+1, v);//该区间完全在右子树 43 else if(bg<=mid && ed>mid){//既在左子树又在右子树 44 update(bg, mid, 2*i, v); 45 update(mid+1, ed, 2*i+1, v); 46 } 47 pushup(i);//回溯更新父节点 48 } 49 int main(){ 50 int t,m; 51 scanf("%d",&t); 52 for (int i=1; i<=t; i++) { 53 scanf("%d%d",&n,&m); 54 build(1, n, 1); 55 for (int j=0; j<m; j++) { 56 int x,y,v; 57 scanf("%d%d%d",&x,&y,&v); 58 update(x, y, 1, v); 59 } 60 printf("Case %d: The total value of the hook is %d.\n",i,tree[1].sum); 61 } 62 return 0; 63 }
标签:int php value wan input cond cut next update
原文地址:http://www.cnblogs.com/uniles/p/7162673.html