[USACO18JAN] Lifeguards S (线段树:扫描线面积)

扫描线裸题没什么好说的

注意空间不要开小了!!!

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define N 100100
 5 #define ll long long
 6 using namespace std;
 7 
 8 int n,ctx;
 9 int cnt[N<<3];
10 ll a[N<<1],sum[N<<3];
11 struct node{
12     ll l,r;
13     int la,ra;
14 }sc[N<<1];
15 void pushup(int l,int r,int rt)
16 {
17     if(cnt[rt]>0) sum[rt]=a[r+1]-a[l];
18     else if(l==r) sum[rt]=0;
19     else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
20 }
21 void update(int L,int R,int l,int r,int rt,int w)
22 {
23     if(L<=l&&r<=R) 
24     {
25         cnt[rt]+=w;
26         pushup(l,r,rt);
27         return;
28     }
29     int mid=(l+r)>>1;
30     if(L<=mid) update(L,R,l,mid,rt<<1,w);
31     if(R>mid) update(L,R,mid+1,r,rt<<1|1,w);
32     pushup(l,r,rt);
33 }
34 
35 int main()
36 {
37     //freopen("testdata.in","r",stdin);
38     scanf("%d",&n);
39     for(int i=1;i<=n;i++)
40     {
41         scanf("%lld%lld",&sc[i].l,&sc[i].r);
42         if(sc[i].l>sc[i].r) swap(sc[i].l,sc[i].r);
43         a[++ctx]=sc[i].l,a[++ctx]=sc[i].r;
44     }
45     sort(a+1,a+ctx+1);
46     int sz=unique(a+1,a+ctx+1)-(a+1);
47     for(int i=1;i<=n;i++)
48     {
49         sc[i].la=lower_bound(a+1,a+sz+1,sc[i].l)-a;
50         sc[i].ra=lower_bound(a+1,a+sz+1,sc[i].r)-a;
51         update(sc[i].la,sc[i].ra-1,1,sz,1,1);
52     }
53     ll ret=0;
54     for(int i=1;i<=n;i++)
55     {
56         update(sc[i].la,sc[i].ra-1,1,sz,1,-1);
57         ret=max(ret,sum[1]);
58         update(sc[i].la,sc[i].ra-1,1,sz,1,1);
59     }
60     printf("%lld\n",ret);
61     return 0;
62 }

猜你喜欢

转载自www.cnblogs.com/guapisolo/p/9697018.html
今日推荐