Just a Hook -區間更新
阿新 • • 發佈:2018-12-13
- 注意push-up push-down 操作
- 可以直接把len mid 打成函式
-
#include<iostream> #include<stdio.h> using namespace std; #define maxn 111111 int t,q,x,y,z,n; struct node { int l,r,num,lazy; int mid() { return (r+l)/2; } int len() { return r-l+1; } } tree[maxn*4]; void pushup(int root) { tree[root].num=tree[root*2].num+tree[root*2+1].num; } void pushdown(int root) { if(tree[root].lazy==0) return ; tree[root*2].num=tree[root*2].len()*tree[root].lazy; tree[root*2+1].num=tree[root*2+1].len()*tree[root].lazy; tree[root*2].lazy=tree[root*2+1].lazy=tree[root].lazy; tree[root].lazy=0; } void init(int l,int r,int root) { tree[root].l=l; tree[root].r=r; tree[root].lazy=0; if(l==r) { tree[root].num=1; return ; } init(tree[root].mid()+1,r,root*2+1); init(l,tree[root].mid(),root*2); pushup(root); } void updata(int l,int r,int ad,int root) { if(tree[root].l==l&&tree[root].r==r) { tree[root].num=tree[root].len()*ad; tree[root].lazy=ad; return ; } pushdown(root); if(l>tree[root].mid()) updata(l,r,ad,root*2+1); else if(r<=tree[root].mid()) updata(l,r,ad,root*2); else { updata(tree[root].mid()+1,r,ad,root*2+1); updata(l,tree[root].mid(),ad,root*2); } pushup(root); } int main() { scanf("%d",&t); for(int i=1; i<=t; i++) { scanf("%d%d",&n,&q); init(1,n,1); while(q--) { scanf("%d%d%d",&x,&y,&z); updata(x,y,z,1); } printf("Case %d: The total value of the hook is %d.\n",i,tree[1].num); } return 0; }