do_while_true

一言(ヒトコト)

「题解」Codeforces 930E Coins Exhibition

看到题就先容斥。然后容斥系数太难算了就寄了,大概要分好几种情况讨论,于是就弃了。

不容斥也能做。考虑限制将串划分成了若干段,然后一段一段 dp.

有没有什么好的方法描述这个性质?这里考虑的是,如果强制 \([l,r]\) 至少出现一个 \(1\),那么就让 \(r\) 及以后的点,让它们往前找第一个 \(1\) 的位置要 \(\geq l\)\(0\) 也类似计算一个这个东西。

然后 dp,\(f_i,g_i,h_i\) 分别表示,第 \(i\) 个端点结尾的这一段,全选 \(0\),全选 \(1\),既有 \(0\) 又有 \(1\) 的方案数。即有转移 \(h_i=(2^{len_i}-2)(f_{i-1}+g_{i-1}+h_{i-1})\)

\(f\) 的转移就是枚举上一个 \(0\) 在哪一段里面,那么这个就是一个后缀的 \(g\)\(h\) 的和。前缀和优化转移即可。\(g\) 的转移也是类似的。

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=1000000007;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return s;
}
const int N=2000010;
int k,n,m;
int p[N],tot,p1[N],p2[N];
int f[N],g[N],h[N],F[N],G[N],H[N];
pii a[N],b[N];
int pos(int x){
	return lower_bound(p+1,p+tot+1,x)-p;
}
int calc(int *a,int l,int r){
	return del(a[r],a[l]);
}
signed main(){
	#ifdef do_while_true
//		assert(freopen("data.in","r",stdin));
//		assert(freopen("data.out","w",stdout));
	#endif
	read(k,n,m);
	p[++tot]=k;p[++tot]=0;
	for(int i=1;i<=n;i++){
		read(a[i].fi,a[i].se);--a[i].fi;
		p[++tot]=a[i].fi,p[++tot]=a[i].se;
	}
	for(int i=1;i<=m;i++){
		read(b[i].fi,b[i].se);--b[i].fi;
		p[++tot]=b[i].fi,p[++tot]=b[i].se;
	}
	sort(p+1,p+tot+1);
	tot=unique(p+1,p+tot+1)-p-1;
	for(int i=1;i<=n;i++)
		cmax(p1[pos(a[i].se)],pos(a[i].fi));
	for(int i=1;i<=m;i++)
		cmax(p2[pos(b[i].se)],pos(b[i].fi));
	for(int i=1;i<=tot;i++)cmax(p1[i],p1[i-1]),cmax(p2[i],p2[i-1]);
//	for(int i=1;i<=tot;i++)cout << p1[i] << ' ' << p2[i] << '\n';
	h[1]=H[1]=1;
	for(int i=2;i<=tot;i++){
		f[i]=add(calc(G,p1[i],i-1),calc(H,p1[i],i-1));
		g[i]=add(calc(F,p2[i],i-1),calc(H,p2[i],i-1));
		h[i]=1ll*del(qpow(2,p[i]-p[i-1]),2)*add(add(f[i-1],g[i-1]),h[i-1])%mod;
		F[i]=add(F[i-1],f[i]);
		G[i]=add(G[i-1],g[i]);
		H[i]=add(H[i-1],h[i]);
//		cout << f[i] << ' ' << g[i] << ' ' << h[i] << '\n';
	}
	cout << add(add(f[tot],g[tot]),h[tot]) << '\n';
    #ifdef do_while_true
		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}
posted @ 2022-11-02 10:47  do_while_true  阅读(31)  评论(0编辑  收藏  举报