poj2528 线段树区间修改(加值)

 1 #include<stdio.h>
 2 #include<string.h>
 3 int addv[100005],minv[100005],fmin,y1,y2,v,l[20005],d[20005],len[20005];
 4 int min(int x,int y)
 5 {
 6     if (x<y) return x;
 7     return y;
 8 }
 9 void update(int o,int l,int r)
10 {
11     int lc=o*2,rc=o*2+1;
12     if (y1<=l&&y2>=r) addv[o]+=v;
13     else{
14         int mid=l+(r-l)/2;
15         if (y1<=mid) update(lc,l,mid);
16         if (y2>mid) update(rc,mid+1,r);
17     }
18     if (l!=r) minv[o]=min(minv[lc],minv[rc]);
19     minv[o]+=addv[o];
20 }
21 void query(int o,int l,int r,int add)
22 {
23     if (y1<=l&&y2>=r) fmin=min(fmin,minv[o]+add);
24     else{
25         int mid=l+(r-l)/2;
26         if (y1<=mid) query(o*2,l,mid,add+addv[o]);
27         if (y2>mid) query(o*2+1,mid+1,r,add+addv[o]);
28     }
29 }
30 void sort(int ll,int rr)
31 {
32     int i=ll,j=rr,x=l[(i+j)/2],y;
33     while (i<=j)
34     {
35         while (l[i]<x) i++;
36         while (x<l[j]) j--;
37         if (i<=j)
38         {
39             y=l[i]; l[i]=l[j]; l[j]=y;
40             y=d[i]; d[i]=d[j]; d[j]=y;
41             i++; j--;
42         }
43     }
44     if (ll<j) sort(ll,j);
45     if (i<rr) sort(i,rr);
46 }
47 int main()
48 {
49     int T,n,i,ans;
50     scanf("%d",&T);
51     while (T--)
52     {
53         scanf("%d",&n);
54         for (i=1;i<=n;i++)
55         {
56             d[2*i-1]=2*i-1; d[2*i]=2*i;
57             scanf("%d%d",&l[2*n-2*(i-1)-1],&l[2*n-2*(i-1)]);
58         }
59         sort(1,2*n);
60         len[d[1]]=1;
61         for (i=2;i<=2*n;i++)
62          if (l[i]!=l[i-1]) len[d[i]]=i;
63          else len[d[i]]=len[d[i-1]];
64         memset(addv,0,sizeof(addv));
65         memset(minv,0,sizeof(minv));
66         ans=0;
67         for (i=1;i<=n;i++)
68         {
69             fmin=0x3f3f3f3f; y1=len[2*i-1]; y2=len[2*i];
70             query(1,1,2*n,0);
71             if (fmin==0) ans++;
72             v=1; update(1,1,2*n);
73         }
74         printf("%d\n",ans);
75     }
76 }

http://poj.org/problem?id=2528

posted on 2014-07-22 23:39  xiao_xin  阅读(117)  评论(0编辑  收藏  举报

导航