Created
September 15, 2019 14:31
-
-
Save snuke/84ca11fc3f1ab109a3d30a006afcfa1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
現在伸びてる最中のindexのsetを管理します。1文字伸ばしたときに消える集合を高速に計算したい。 | |
1文字伸ばしたときには、集合の中で最も長いやつの一致長をwとすると、wより大きいのは愚直に消して、wより小さいのは 長さwのときに消した集合を覚えておけば分かる 。 | |
消す回数が高々N回であることに注意するといろいろを愚直にやっていいことが分かる。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define fi first | |
#define se second | |
#define rep(i,n) for(int i = 0; i < (n); ++i) | |
#define rrep(i,n) for(int i = 1; i <= (n); ++i) | |
#define drep(i,n) for(int i = (n)-1; i >= 0; --i) | |
#define srep(i,s,t) for (int i = s; i < t; ++i) | |
#define rng(a) a.begin(),a.end() | |
#define rrng(a) a.rbegin(),a.rend() | |
#define maxs(x,y) (x = max(x,y)) | |
#define mins(x,y) (x = min(x,y)) | |
#define limit(x,l,r) max(l,min(x,r)) | |
#define lims(x,l,r) (x = max(l,min(x,r))) | |
#define isin(x,l,r) ((l) <= (x) && (x) < (r)) | |
#define pb push_back | |
#define eb emplace_back | |
#define sz(x) (int)(x).size() | |
#define pcnt __builtin_popcountll | |
#define uni(x) x.erase(unique(rng(x)),x.end()) | |
#define snuke srand((unsigned)clock()+(unsigned)time(NULL)); | |
#define show(x) cout<<#x<<" = "<<x<<endl; | |
#define PQ(T) priority_queue<T,v(T),greater<T> > | |
#define bn(x) ((1<<x)-1) | |
#define dup(x,y) (((x)+(y)-1)/(y)) | |
#define newline puts("") | |
#define v(T) vector<T> | |
#define vv(T) v(v(T)) | |
using namespace std; | |
typedef long long int ll; | |
typedef unsigned uint; | |
typedef unsigned long long ull; | |
typedef pair<int,int> P; | |
typedef tuple<int,int,int> T; | |
typedef vector<int> vi; | |
typedef vector<vi> vvi; | |
typedef vector<ll> vl; | |
typedef vector<P> vp; | |
typedef vector<T> vt; | |
inline int in() { int x; scanf("%d",&x); return x;} | |
template<typename T>inline istream& operator>>(istream&i,v(T)&v) | |
{rep(j,sz(v))i>>v[j];return i;} | |
template<typename T>string join(const v(T)&v) | |
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);} | |
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v) | |
{if(sz(v))o<<join(v);return o;} | |
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v) | |
{return i>>v.fi>>v.se;} | |
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v) | |
{return o<<v.fi<<","<<v.se;} | |
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;} | |
const double eps = 1e-10; | |
const ll LINF = 1001002003004005006ll; | |
const int INF = 1001001001; | |
#define dame { puts("-1"); return 0;} | |
#define yn {puts("YES");}else{puts("NO");} | |
const int MX = 300005; | |
char command[10]; | |
// Binary Indexed Tree | |
struct bit { | |
int n; vector<int> d; | |
int tot; | |
bit() {} | |
bit(int mx): n(mx), d(mx), tot(0) {} | |
void add(int i, int x) { | |
tot += x; | |
for (++i;i<n;i+=i&-i) d[i] += x; | |
} | |
int sum(int i) { | |
int x = 0; | |
for (++i;i;i-=i&-i) x += d[i]; | |
return x; | |
} | |
}; | |
// | |
set<int> t; | |
vvi del_history(MX); | |
bit live(MX), dead(MX); | |
void del(int len, int x) { | |
del_history[len].pb(x); | |
t.erase(x); | |
live.add(x, -1); | |
dead.add(len-x-1, 1); | |
} | |
int main() { | |
int q; | |
scanf("%d",&q); | |
string s; | |
int ans = 0; | |
rep(qi,q) { | |
scanf("%s",command); | |
if (command[0] == 'a') { | |
char c; | |
scanf(" %c",&c); | |
c = (int(c-'a')+ans)%26+'a'; | |
s += c; | |
if (sz(s) == 1) continue; | |
int i = sz(s)-1; | |
int len = sz(s); | |
if (s.back() == s[0]) { | |
t.insert(i); | |
live.add(i,1); | |
} | |
while (sz(t)) { // 最大を見つけるまでは愚直にdelete | |
int x = *(t.begin()); | |
if (s[len-x-1] == s.back()) break; | |
del(len,x); | |
} | |
if (sz(t)) { // 最大(w)未満は過去の記録を見てdelete | |
int w = len-*(t.begin()); | |
for (int j : del_history[w]) { | |
int x = j-w+len; | |
del(len,x); | |
} | |
} | |
} else { | |
int p; | |
scanf("%d",&p); | |
p = (p-1+ans)%sz(s)+1; | |
ans = dead.tot-dead.sum(p-1); | |
ans += live.sum(sz(s)-p); | |
ans++; | |
printf("%d\n",ans); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment