Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define fori(i, l, r) for(int i = (int) (l); i <= (int) (r); i++)
- #define ford(i, r, l) for(int i = (int) (r); i >= (int) (l); i--)
- #define ii pair<int, int>
- #define fi first
- #define se second
- #define vi vector<int>
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define all(x) begin(x), end(x)
- #define sp ' '
- #define endl '\n'
- #define int long long
- #define ld long double
- const int maxn = 3e5 + 5;
- int q;
- int type[maxn];
- char c[maxn];
- string s;
- string t;
- int pref[maxn];
- int sp_pref[maxn][20];
- int sum[maxn];
- int mx[maxn];
- int cnt[maxn];
- vector<int> anss;
- int ans;
- signed main() {
- cin >> q;
- fori(i, 1, q) {
- cin >> type[i] >> c[i];
- if(type[i] == 1) {
- s.push_back(c[i]);
- }
- else {
- t.push_back(c[i]);
- }
- }
- int n = s.size();
- int m = t.size();
- pref[0] = 0;
- fori(i, 1, m - 1) {
- int cur = pref[i - 1];
- while(t[cur] != t[i] && cur) cur = pref[cur - 1];
- cur = cur + (t[cur] == t[i]);
- pref[i] = cur;
- sp_pref[i][0] = cur;
- fori(bit, 1, 19) {
- sp_pref[i][bit] = sp_pref[sp_pref[i][bit - 1] - 1][bit - 1];
- }
- sum[i] = sum[pref[i] - 1] + i;
- }
- int cur = 0;
- fori(i, 0, n - 1) {
- if(cur == m) cur = pref[cur - 1];
- while(t[cur] != s[i] && cur) cur = pref[cur - 1];
- cur = cur + (t[cur] == s[i]);
- mx[i] = cur;
- ans = ans + sum[mx[i] - 1];
- cnt[mx[i]]++;
- }
- anss.eb(ans);
- ford(i, q, 2) {
- if(type[i] == 1) {
- int cur = mx[n - 1];
- ford(bit, 19, 0) {
- if(sp_pref[cur - 1][bit] > m) cur = sp_pref[cur - 1][bit];
- }
- if(cur > m) cur = pref[cur - 1];
- ans -= sum[cur - 1];
- cnt[cur]--;
- n--;
- }
- else {
- ans -= cnt[m] * (m - 1);
- cnt[pref[m - 1]] += cnt[m];
- cnt[m] = 0;
- m--;
- }
- anss.eb(ans);
- }
- reverse(begin(anss), end(anss));
- for(int v: anss) cout << v << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement