看到题就先容斥。然后容斥系数太难算了就寄了,大概要分好几种情况讨论,于是就弃了。
不容斥也能做。考虑限制将串划分成了若干段,然后一段一段 dp.
有没有什么好的方法描述这个性质?这里考虑的是,如果强制 至少出现一个 ,那么就让 及以后的点,让它们往前找第一个 的位置要 . 也类似计算一个这个东西。
然后 dp, 分别表示,第 个端点结尾的这一段,全选 ,全选 ,既有 又有 的方案数。即有转移 .
而 的转移就是枚举上一个 在哪一段里面,那么这个就是一个后缀的 和 的和。前缀和优化转移即可。 的转移也是类似的。
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?