CF930E Coins Exhibition

题意:

标程:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int mod=1e9+7;
 4 const int N=400005;
 5 typedef long long ll;
 6 struct node{int l,r;}a[N],b[N];
 7 int k,n,m,p[N],rg[N][2],g[3],f[N][3],t; 
 8 int ksm(int x,int y)
 9 {
10     int res=1;
11     while (y){if (y&1) res=(ll)res*x%mod; x=(ll)x*x%mod;y>>=1;}
12     return res;
13 }
14 int main()
15 {
16     scanf("%d%d%d",&k,&n,&m);
17     for (int i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r),p[++t]=--a[i].l,p[++t]=a[i].r;
18     for (int i=1;i<=m;i++) scanf("%d%d",&b[i].l,&b[i].r),p[++t]=--b[i].l,p[++t]=b[i].r;
19     p[++t]=0;p[++t]=k;
20     sort(p+1,p+t+1);t=unique(p+1,p+t+1)-p-1;
21     for (int i=1;i<=t;i++) rg[i][0]=rg[i][1]=t+1;
22     for (int i=1;i<=n;i++)
23     {
24        a[i].l=lower_bound(p+1,p+t+1,a[i].l)-p;
25         a[i].r=lower_bound(p+1,p+t+1,a[i].r)-p;
26         rg[a[i].l][0]=min(rg[a[i].l][0],a[i].r);
27    }
28     for (int i=1;i<=m;i++)
29     {
30         b[i].l=lower_bound(p+1,p+t+1,b[i].l)-p;
31         b[i].r=lower_bound(p+1,p+t+1,b[i].r)-p;
32         rg[b[i].l][1]=min(rg[b[i].l][1],b[i].r);
33     }
34     for (int i=t-1;i>=0;i--) rg[i][0]=min(rg[i][0],rg[i+1][0]),rg[i][1]=min(rg[i][1],rg[i+1][1]);
35    f[t][0]=f[t][1]=f[t][2]=1;
36     for (int i=t-1;i>=1;i--)
37    {
38        g[2]=(ll)f[i+1][2]*(ksm(2,p[i+1]-p[i])-2+mod)%mod;
39        for (int j=0;j<=1;j++) g[j]=((ll)f[i+1][j]-f[rg[i][j]][j]+mod)%mod;
40        f[i][0]=((ll)f[i+1][0]+g[1]+g[2])%mod;
41         f[i][1]=((ll)f[i+1][1]+g[0]+g[2])%mod;
42         f[i][2]=((ll)g[0]+g[1]+g[2])%mod; 
43     }
44     printf("%d\n",f[1][2]);
45     return 0;
46 }

易错点:1.离散化数组的大小需要注意,此题应该开4倍。

猜你喜欢

转载自www.cnblogs.com/Scx117/p/9073764.html