codeforces 138E. Hellish Constraints
来源:互联网 发布:简单软件 编辑:程序博客网 时间:2024/06/05 03:30
先考虑只有一个限制时,右端点扫过去,维护每个左端点是否满足限制,
A[i]=0表示i开头的后缀不满足,A[i]=1表示i开头的后缀满足
那么当新加入一个字母到后缀时,若他不是当前限制的字母,则对所有左端点的答案都无影响,若他是,它会使那些之前含L-1个限制字母的后缀变得合法,含R个的变得不合法,
记录一下这个字母w第i次出现的位置las[w][i],
将这个字母加入,就是对las[now-L]+1~las[now-L]赋为合法,las[now-R-1]+1~las[now-R-1]赋为不合法,
注意到每个位置至多被赋值合法1次,不合法一次,对于一个限制,扫过去是O(n)的
再把k个限制一起考虑就行了,总复杂度O(nk)
code:
#include<set>#include<map>#include<deque>#include<queue>#include<stack>#include<cmath>#include<ctime>#include<bitset>#include<string>#include<vector>#include<cstdio>#include<cstdlib>#include<cstring>#include<climits>#include<complex>#include<iostream>#include<algorithm>#define ll long long#define inf 1e9using namespace std;const int maxn = 110000;const int maxk = 1100;const int maxv = 26;char str[maxn],s[110];int n,m,L,R;int las[maxv][maxn],col[maxv];int f[maxn];struct node{ int l,r; node(){} node(const int _l,const int _r){l=_l;r=_r;}}a[maxv][maxk]; int cnt[maxv];ll ans;int main(){ scanf("%s",str); n=strlen(str); scanf("%d%d%d",&m,&L,&R); int ev=0; for(int i=1;i<=m;i++) { int l,r; scanf("%s%d%d",s,&l,&r); const int w=s[0]-'a'; a[w][++cnt[w]]=node(l,r); if(l==0) ev++; } for(int i=0;i<26;i++) las[i][col[i]=0]=-1; int now=0; for(int i=0;i<n;i++) { f[i]=ev; if(f[i]>=L&&f[i]<=R) now++; const int w=str[i]-'a'; las[w][++col[w]]=i; for(int j=1;j<=cnt[w];j++) { if(col[w]>=a[w][j].l) { for(int k=las[w][max(0,col[w]-a[w][j].l)]+1;k<=las[w][col[w]-a[w][j].l+1];k++) { f[k]++; if(f[k]==L) now++; if(f[k]==R+1) now--; } } if(col[w]>a[w][j].r) { for(int k=las[w][col[w]-a[w][j].r-1]+1;k<=las[w][col[w]-a[w][j].r];k++) { f[k]--; if(f[k]==R) now++; if(f[k]==L-1) now--; } } } ans+=(ll)now; } printf("%I64d\n",ans); return 0;}
阅读全文
0 0
- codeforces 138E. Hellish Constraints
- codeforces 138E. Hellish Constraints
- CONSTRAINTS
- constraints
- Constraints
- codeforces 163E e-Government
- 【Codeforces 163E】E-Government
- Codeforces 472G Design Tutorial: Increase the Constraints(分块+FFT)
- Codeforces 78E Evacuation
- 【dp】codeforces 83E
- Codeforces 124 E
- Codeforces 231E
- Codeforces 231E - Cactus
- Codeforces #163 Div2 E
- codeForces 35E
- Codeforces 35E
- Playlist codeforces 268E
- Codeforces 148E(Porcelain)
- Leetcode练习 #9 Palindrome Number
- 写一点Linux知识(准备长期更新)
- css3选择器(全)
- 在AsyncTask中使用自定义loadingDialog时报错:DecorView not attached to window manager
- Spring中配置事物注解的方式: @Transactional
- codeforces 138E. Hellish Constraints
- hiho一下 第168周 扩展二进制数
- 阿里云 互联网 产品 总结
- opencv开发笔记(五):滑动条控制图像的融合
- switch语句的用法
- leetcode 328. Odd Even Linked List
- IO分类
- 一句代码搞定 UIButton 的图片与文字布局
- Freesshd Permission denied (publickey).