Advertisement
ivnikkk

Untitled

Dec 29th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <vector>
  2. #include<iostream>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <string>
  8. #include <set>
  9. #include <deque>
  10. #include <queue>
  11. #include <map>
  12. #include <bitset>
  13. #include <random>
  14. #include <cassert>
  15. #include <unordered_map>
  16. #include <unordered_set>
  17. #include<math.h>
  18. using namespace std;
  19. typedef unsigned int ui;
  20. typedef long long             ll;
  21. typedef unsigned long long     ull;
  22. typedef long double            ld;
  23. #define endl              "\n"
  24. #define all(a)            a.begin(), a.end()
  25. #define allr(a)           a.rbegin(), a.rend()
  26. #define pb                push_back
  27. #define pikachu           push_back
  28. #define F                 first
  29. #define S                 second
  30. const ll MAXN = 1e5+1;
  31. struct segtree {
  32.     ll ind;
  33.     ll cnt[8];
  34. };
  35. segtree add[MAXN];
  36. segtree t[4 * MAXN];
  37. ll a[MAXN];
  38. ll n;
  39.  
  40. void build(ll v, ll tl, ll tr) {
  41.     if (tl == tr) {
  42.         t[v].cnt[a[tl]]++;
  43.         t[v].ind = a[tl];
  44.     }
  45.     else {
  46.         ll tm = (tl + tr) / 2;
  47.         build(v * 2, tl, tm);
  48.         build(v * 2 + 1, tm + 1, tr);
  49.         for (int i = 0; i <= 7; i++) {
  50.             t[v].cnt[i] = t[v * 2].cnt[i] + t[v * 2 + 1].cnt[i];
  51.         }
  52.     }
  53. }
  54.  
  55. void update(ll v, ll tl, ll tr, ll l, ll r, ll color) {
  56.     if (l > r)
  57.         return;
  58.     if (l == tl && tr == r) {
  59.         for (ll i = 0; i <= 8; i++) {
  60.             if (t[v].cnt[i]) {
  61.                 t[v].cnt[i]--;
  62.                 t[v].cnt[i ^ color]++;
  63.             }
  64.         }
  65.     }
  66.     else {
  67.         ll tm = (tl + tr) / 2;
  68.         update(v * 2, tl, tm, l, min(r, tm), color);
  69.         update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, color);
  70.         for (int i = 0; i <= 8; i++) {
  71.             t[v].cnt[i] = t[2 * v].cnt[i] + t[2 * v + 1].cnt[i];
  72.         }
  73.     }
  74. }
  75. ll quee(ll v, ll vl, ll vr, ll l, ll r,ll x) {
  76.     if (r < vl || vr < l)
  77.         return 0;
  78.     if (l <= vl && vr <= r)
  79.         return t[v].cnt[x];
  80.     ll vm = vl + (vr - vl) / 2;
  81.     ll  ql = quee(2 * v, vl, vm, l, r, x);
  82.     ll  qr = quee(2 * v + 1, vm + 1, vr, l, r, x);
  83.     return ql + qr;
  84. }
  85. ll ask(ll l, ll r, ll x) {
  86.     ll no = 1e9+9;
  87.     if (r == no || l == no) {
  88.         return no;
  89.     }
  90.     while (r - l > 1) {
  91.         ll mid = r + l;
  92.         mid /= 2;
  93.         if (quee(1, 0, n - 1, l, mid,x)) {
  94.             r = mid;
  95.         }
  96.         else if (quee(1, 0, n - 1, mid+1, r, x)) {
  97.             l = mid;
  98.         }
  99.         else {
  100.             return no;
  101.         }
  102.     }
  103.     if (r < l) {
  104.         return no;
  105.     }
  106.     return l;
  107. }
  108. void solve() {
  109.     ll n;
  110.     cin >> n;
  111.     ll q;
  112.     cin >> q;
  113.     for (ll i = 0; i < n; i++) {
  114.         cin >> a[i];
  115.     }
  116.     build(1, 0, n - 1);
  117.     for (ll i = 0; i < q; i++) {
  118.         string ques;
  119.         cin >> ques;
  120.         if (ques == "xor") {
  121.             ll l, r, Xor;
  122.             cin >> l >> r >> Xor;
  123.             update(1, 0, n - 1, --l, --r, Xor);
  124.         }
  125.         else {
  126.             ll l, r;
  127.             cin >> l >> r;
  128.             ll siz = r - l + 1;
  129.             r--, l--;
  130.             vector <vector<ll>> dp(siz+1, vector<ll>(8));
  131.             dp[1][0] = ask(l,r, 0);
  132.             for (int i = 1; i <= 7; i++) {
  133.                 dp[1][i] = min(dp[1][i - 1], ask(l, r, i-1));
  134.             }
  135.             ll ans = 1;
  136.             for (ll i = 2; i <= siz; i++) {
  137.                 for (ll j = 0; j <= 7; j++) {
  138.                     if(j>0)dp[i][j] = min(dp[i][j - 1], min(j,ask(dp[i-1][j-1], r, j - 1)));
  139.                     if (dp[i][j] != 1e9 + 9) {
  140.                         ans = max(ans, dp[i][j]);
  141.                     }
  142.                
  143.                 }
  144.             }
  145.             cout << ans << endl;
  146.         }
  147.     }
  148.  
  149. }
  150. signed main() {
  151.     ios_base::sync_with_stdio(false);
  152.     cin.tie(nullptr);
  153.     ll t = 1;
  154.     //cin >> t;
  155.     while (t--) {
  156.         solve();
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement