Advertisement
Guest User

Sol

a guest
Nov 19th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define fori(i, l, r) for(int i = (int) (l); i <= (int) (r); i++)
  6. #define ford(i, r, l) for(int i = (int) (r); i >= (int) (l); i--)
  7. #define ii pair<int, int>
  8. #define fi first
  9. #define se second
  10. #define vi vector<int>
  11. #define eb emplace_back
  12. #define em emplace
  13. #define pb push_back
  14. #define all(x) begin(x), end(x)
  15. #define sp ' '
  16. #define endl '\n'
  17. #define int long long
  18. #define ld long double
  19.  
  20. const int maxn = 3e5 + 5;
  21. int q;
  22. int type[maxn];
  23. char c[maxn];
  24. string s;
  25. string t;
  26. int pref[maxn];
  27. int sp_pref[maxn][20];
  28. int sum[maxn];
  29. int mx[maxn];
  30. int cnt[maxn];
  31. vector<int> anss;
  32. int ans;
  33.  
  34. signed main() {
  35.     cin >> q;
  36.     fori(i, 1, q) {
  37.         cin >> type[i] >> c[i];
  38.         if(type[i] == 1) {
  39.             s.push_back(c[i]);
  40.         }
  41.         else {
  42.             t.push_back(c[i]);
  43.         }
  44.     }
  45.     int n = s.size();
  46.     int m = t.size();
  47.  
  48.    
  49.     pref[0] = 0;
  50.     fori(i, 1, m - 1) {
  51.         int cur = pref[i - 1];
  52.         while(t[cur] != t[i] && cur) cur = pref[cur - 1];
  53.         cur = cur + (t[cur] == t[i]);
  54.         pref[i] = cur;
  55.         sp_pref[i][0] = cur;
  56.         fori(bit, 1, 19) {
  57.             sp_pref[i][bit] = sp_pref[sp_pref[i][bit - 1] - 1][bit - 1];
  58.         }
  59.         sum[i] = sum[pref[i] - 1] + i;
  60.     }
  61.    
  62.     int cur = 0;
  63.     fori(i, 0, n - 1) {
  64.         if(cur == m) cur = pref[cur - 1];
  65.         while(t[cur] != s[i] && cur) cur = pref[cur - 1];
  66.         cur = cur + (t[cur] == s[i]);
  67.         mx[i] = cur;
  68.         ans = ans + sum[mx[i] - 1];
  69.         cnt[mx[i]]++;
  70.     }
  71.     anss.eb(ans);
  72.     ford(i, q, 2) {
  73.         if(type[i] == 1) {
  74.             int cur = mx[n - 1];
  75.             ford(bit, 19, 0) {
  76.                 if(sp_pref[cur - 1][bit] > m) cur = sp_pref[cur - 1][bit];
  77.             }
  78.             if(cur > m) cur = pref[cur - 1];
  79.             ans -= sum[cur - 1];
  80.             cnt[cur]--;
  81.             n--;
  82.         }
  83.         else {
  84.             ans -= cnt[m] * (m - 1);
  85.             cnt[pref[m - 1]] += cnt[m];
  86.             cnt[m] = 0;
  87.             m--;
  88.         }
  89.         anss.eb(ans);
  90.     }
  91.     reverse(begin(anss), end(anss));
  92.     for(int v: anss) cout << v << endl;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement