hdu1698 线段树区间赋值

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<math.h>
 6 using namespace std;
 7 int yy2,yy1,v,fsum;
 8 int sumv[1000005],setv[1000005];
 9 
10 void maintain(int o,int l,int r)
11 {
12     int lc=o*2,rc=o*2+1;
13     sumv[o]=0;
14     if (setv[o]>=0) sumv[o]=setv[o]*(r-l+1);
15     else if (r>l) sumv[o]+=sumv[lc]+sumv[rc];
16 }
17 void  update(int o,int l,int r)
18 {
19     int lc=o*2,rc=o*2+1;
20     if (yy1<=l&&yy2>=r) setv[o]=v;
21     else{
22         if (setv[o]>=0)//pushdown
23         {
24             setv[lc]=setv[rc]=setv[o];
25             setv[o]=-1;
26         }
27         int mid=l+(r-l)/2;
28         if (yy1<=mid) update(lc,l,mid); else maintain(lc,l,mid);
29         if (yy2>mid) update(rc,mid+1,r); else maintain(rc,mid+1,r);
30     }
31     maintain(o,l,r);
32 }
33 void query(int o,int l,int r)
34 {
35     if (setv[o]>=0) fsum+=setv[o]*(min(r,yy2)-max(l,yy1)+1);
36     else if (yy1<=l&&yy2>=r) fsum+=sumv[o];
37     else {
38         int mid=l+(r-l)/2;
39         if (yy1<=mid) query(o*2,l,mid);
40         if (yy2>mid) query(o*2+1,mid+1,r);
41     }
42 }
43 int main()
44 {
45     int T,t,n,m,i;
46     scanf("%d",&T);
47     for (t=1;t<=T;t++)
48     {
49         scanf("%d%d",&n,&m);
50 /*        memset(setv,0,sizeof(setv));
51         memset(sumv,0,sizeof(sumv));*/
52         yy1=1; yy2=n; v=1;
53         update(1,1,n);
54         for (i=1;i<=m;i++)
55         {
56             scanf("%d%d%d",&yy1,&yy2,&v);
57             update(1,1,n);
58         }
59         fsum=0; yy1=1; yy2=n; query(1,1,n);
60         printf("Case %d: The total value of the hook is %d.\n",t,fsum);
61     }
62 }

http://acm.hdu.edu.cn/showproblem.php?pid=1698

posted on 2014-07-23 12:36  xiao_xin  阅读(148)  评论(0编辑  收藏  举报

导航