BZOJ 4896: [Thu Summer Camp2016]补退选
trie树+vector+二分
别忘了abs(ans)
#include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<cmath> #define mp make_pair #define pr pair<int,int> #define sc second using namespace std; int n,cnt,ch[6000005][11],sz[6000005],Max[6000005]; vector<pr> stack[6000005]; char s[65]; int main(){ scanf("%d",&n); int ans=0; for (int ti=1; ti<=n; ti++){ int cas; scanf("%d%s",&cas,s); int len=strlen(s),x=0; if (cas==1){ for (int i=0; i<len; i++){ if (!ch[x][s[i]-'a']) ch[x][s[i]-'a']=++cnt; x=ch[x][s[i]-'a']; sz[x]++; if (sz[x]>Max[x]){ Max[x]=sz[x]; stack[x].push_back(mp(Max[x],ti)); } } } else if (cas==2){ for (int i=0; i<len; i++){ x=ch[x][s[i]-'a']; sz[x]--; } } else{ for (int i=0; i<len; i++) x=ch[x][s[i]-'a']; int a,b,c; scanf("%d%d%d",&a,&b,&c); int lim=(1ll*a*abs(ans)+b)%c; lim++; if (lim>Max[x]) ans=-1; else{ int id=lower_bound(stack[x].begin(),stack[x].end(),mp(lim,0))-stack[x].begin(); ans=stack[x][id].sc; } printf("%d\n",ans); } } return 0; }