标签:
Case 1: The total value of the hook is 24.
//类似延迟标记一样的思路
//#include<bits/stdc++.h> #include<cstdio> #include<algorithm> using namespace std; const int maxn=100011; const int inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,n) for(int i=0;i<(n);i++) template<class T>inline T read(T&x) { char c; while((c=getchar())<=32); bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return x; } template<class T> inline void read_(T&x,T&y) { read(x); read(y); } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------IO template------ typedef long long LL; struct node { int sum; int val; } p[maxn<<3]; void build(int rt,int L,int R) { p[rt].val=0; if(L==R) { p[rt].sum=1; p[rt].val=1; return; } build(lson); build(rson); p[rt].sum=p[rt<<1].sum+p[rt<<1|1].sum; } void update(int rt,int L,int R,int x,int y,int z) { // printf("%d %d %d %d %d %d\n",rt,L,R,x,y,z); if(x==L&&y==R) { p[rt].sum=(R-L+1)*z; p[rt].val=z; return ; } if(p[rt].val>0)//向下更新时,判断条件写好, { p[rt<<1].sum=(M-L+1)*p[rt].val; p[rt<<1|1].sum=(R-M)*p[rt].val;//右儿子 区间是R-M即R-(M+1)+1 p[rt<<1].val=p[rt<<1|1].val=p[rt].val; p[rt].val=0; } if(y<=M) update(lson,x,y,z); else if(x>M) update(rson,x,y,z); else { update(lson,x,M,z); update(rson,M+1,y,z); } p[rt].sum=p[rt<<1].sum+p[rt<<1|1].sum; } int main() { //#ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); //#endif // ONLINE_JUDGE int T; int n,m,i,j,k,t; read(T); int cas=1; while(T--) { read_(n,m); build(1,1,n); int x,y,z; while(m--) { read_(x,y); read(z); update(1,1,n,x,y,z); } printf("Case %d: The total value of the hook is %d.\n",cas++,p[1].sum); } return 0; }
E - Just a Hook HDU 1698 (线段树+类似延迟标记)
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/44874513