Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15143 Accepted Submission(s): 7516
#include <cstdio> #include <string> #include <iostream> using namespace std; #define MAXN 1000001 typedef struct Node { int left; int right; int date; int lazy; int mid(){ return (left+right)>>1; } int len(){ return (right-left+1); } }Node; Node ST[MAXN<<3]; void Pushdown(int root,int len) { if(ST[root].lazy){ ST[root<<1].lazy=ST[root<<1|1].lazy=ST[root].lazy; ST[root<<1].date=ST[root].lazy*(len-(len>>1)); ST[root<<1|1].date=ST[root].lazy*(len>>1); ST[root].lazy=0; } } void CreateTree(int root,int l,int r) { ST[root].left=l; ST[root].right=r; ST[root].lazy=0; if(l==r){ ST[root].date=1; return; } else { int mid=ST[root].mid(); CreateTree(root<<1,l,mid); CreateTree(root<<1|1,mid+1,r); ST[root].date=ST[root<<1].date+ST[root<<1|1].date; } } void InsertTree(int root,int l,int r,int value) { if(ST[root].left>=l && ST[root].right<=r){ ST[root].lazy=value; ST[root].date=ST[root].len()*value; } else { Pushdown(root,ST[root].len()); int mid=ST[root].mid(); if(r<=mid){ InsertTree(root<<1,l,r,value); } else if(l>mid){ InsertTree(root<<1|1,l,r,value); } else { InsertTree(root<<1,l,mid,value); InsertTree(root<<1|1,mid+1,r,value); } ST[root].date=ST[root<<1].date+ST[root<<1|1].date; } } int main() { int a,b,c; int T,n,t,cnt; scanf("%d",&T); while(T--){ cnt=0; scanf("%d%d",&n,&t); CreateTree(1,1,n); while(t--){ scanf("%d%d%d",&a,&b,&c); InsertTree(1,a,b,c); } printf("Case %d: The total value of the hook is %d.\n",++cnt,ST[1].date); } return 0; }