Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11425 Accepted Submission(s): 5650
题意:dota中的英雄 Pudge 有一hook 这hook 可由三种材料构成 分别如下
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
现输入 x y z 表示x到y的hook 的值变为z 。z如上如述
求最后,hook的值
思路:线段树 普通的线段树会超时,得成段更新 用到了延迟覆盖
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=100010; #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) struct Tree{ int l,r; int val,sum; int cover; }tree[N<<2]; void PushUp(int rt){ tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum; } void build(int L,int R,int rt){ tree[rt].l=L; tree[rt].r=R; tree[rt].cover=1; tree[rt].val=1; if(tree[rt].l==tree[rt].r){ tree[rt].sum=1; return ; } int mid=(L+R)>>1; build(L,mid,L(rt)); build(mid+1,R,R(rt)); PushUp(rt); } void PushDown(int rt){ tree[L(rt)].val=tree[rt].val; tree[L(rt)].sum=(tree[L(rt)].r-tree[L(rt)].l+1)*tree[rt].val; tree[L(rt)].cover=1; tree[R(rt)].val=tree[rt].val; tree[R(rt)].sum=(tree[R(rt)].r-tree[R(rt)].l+1)*tree[rt].val; tree[R(rt)].cover=1; tree[rt].cover=0; } void update(int val,int L,int R,int rt){ if(tree[rt].l==L && tree[rt].r==R){ tree[rt].cover=1; tree[rt].val=val; tree[rt].sum=(tree[rt].r-tree[rt].l+1)*val; return ; } if(tree[rt].cover) PushDown(rt); if(tree[rt].val==val && tree[rt].cover) //当这一整段已为val,且覆盖整段,则继续向下已没有意义 return ; int mid=(tree[rt].l+tree[rt].r)>>1; if(R<=mid) update(val,L,R,L(rt)); else if(L>=mid+1) update(val,L,R,R(rt)); else{ update(val,L,mid,L(rt)); update(val,mid+1,R,R(rt)); } PushUp(rt); } int main(){ //freopen("input.txt","r",stdin); int t,n,q; int cases=0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&q); build(1,n,1); int a,b,z; while(q--){ scanf("%d%d%d",&a,&b,&z); update(z,a,b,1); } printf("Case %d: The total value of the hook is %d.\n",++cases,tree[1].sum); } return 0; }