Advertisement
lukadupli

Slicnost

Apr 26th, 2025
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define fi first
  4. #define se second
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. typedef pair<int, int> pii;
  10.  
  11. const int MAX = 1 << 17;
  12.  
  13. template<typename T> T spoji(T a, T b){
  14.     if(a.fi != b.fi) return max(a, b);
  15.     return {a.fi, a.se + b.se};
  16. }
  17. struct Node{
  18.     Node *lc = nullptr, *rc = nullptr;
  19.     pii val;
  20.     int lazy = 0;
  21.     void calc(){
  22.         val = spoji(lc->val, rc->val);
  23.     }
  24. };
  25. Node* build(int l = 0, int r = MAX){
  26.     Node* node = new Node;
  27.     if(r - l == 1){
  28.         node->val = {0, 1};
  29.         return node;
  30.     }
  31.     int m = (l + r) / 2;
  32.     node->lc = build(l, m);
  33.     node->rc = build(m, r);
  34.     node->calc();
  35.     return node;
  36. }
  37. Node* clone(Node* node){
  38.     Node* novi = new Node;
  39.     novi->lc = node->lc;
  40.     novi->rc = node->rc;
  41.     novi->val = node->val;
  42.     novi->lazy = node->lazy;
  43.     return novi;
  44. }
  45. void prop(Node* node, int l, int r){
  46.     if(!node->lazy || r - l == 1) return;
  47.  
  48.     node->lc = clone(node->lc);
  49.     node->lc->val.fi += node->lazy;
  50.     node->lc->lazy += node->lazy;
  51.  
  52.     node->rc = clone(node->rc);
  53.     node->rc->val.fi += node->lazy;
  54.     node->rc->lazy += node->lazy;
  55.  
  56.     node->lazy = 0;
  57. }
  58. Node* update(int lu, int ru, int val, Node* node, int l = 0, int r = MAX){
  59.     if(l >= lu && r <= ru){
  60.         Node* novi = clone(node);
  61.         novi->val.fi += val;
  62.         novi->lazy += val;
  63.         return novi;
  64.     }
  65.     if(l >= ru || r <= lu) return node;
  66.  
  67.     prop(node, l, r);
  68.  
  69.     int m = (l + r) / 2;
  70.     Node* novi = new Node;
  71.     novi->lc = update(lu, ru, val, node->lc, l, m);
  72.     novi->rc = update(lu, ru, val, node->rc, m, r);
  73.     novi->calc();
  74.  
  75.     return novi;
  76. }
  77. void print(Node* node, int l = 0, int r = MAX){
  78.     if(r - l == 1){
  79.         printf("(%d, %d) ", node->val.fi, node->val.se);
  80.         return;
  81.     }
  82.     prop(node, l, r);
  83.  
  84.     int m = (l + r)/2;
  85.     print(node->lc, l, m);
  86.     print(node->rc, m, r);
  87. }
  88.  
  89. struct Tour2{
  90.     pair<int, ll> nodes[2*MAX];
  91.  
  92.     void update(int ind, pii val, int node = 1, int l = 0, int r = MAX){
  93.         if(r - l == 1){
  94.             nodes[node] = val;
  95.             return;
  96.         }
  97.  
  98.         int m = (l + r) / 2;
  99.         if(ind < m) update(ind, val, 2*node, l, m);
  100.         else update(ind, val, 2*node + 1, m, r);
  101.  
  102.         nodes[node] = spoji(nodes[2*node], nodes[2*node + 1]);
  103.     }
  104. };
  105.  
  106. int n, k, q, pos[MAX];
  107. pii lrs[MAX];
  108.  
  109. Node *tours[MAX];
  110. Tour2 t2;
  111.  
  112. int main()
  113. {
  114.     ios_base::sync_with_stdio(0);
  115.     cin.tie(0);
  116.  
  117.     cin >> n >> k >> q;
  118.     for(int i = 0; i < n; i++){
  119.         int x; cin >> x;
  120.         pos[x] = i;
  121.     }
  122.     for(int i = 0; i < n; i++){
  123.         int x; cin >> x;
  124.         int l = max(i - k + 1, 0), r = min(i, n - k) + 1;
  125.  
  126.         lrs[pos[x]] = {l, r};
  127.     }
  128.  
  129.     tours[0] = build();
  130.     for(int i = 0; i < k - 1; i++){
  131.         tours[0] = update(lrs[i].fi, lrs[i].se, 1, tours[0]);
  132.     }
  133.  
  134.     for(int i = k - 1; i < n; i++){
  135.         tours[i - k + 1] = update(lrs[i].fi, lrs[i].se, 1, tours[i - k + 1]);
  136.         t2.update(i - k + 1, tours[i - k + 1]->val);
  137.         tours[i - k + 2] = update(lrs[i - k + 1].fi, lrs[i - k + 1].se, -1, tours[i - k + 1]);
  138.     }
  139.     cout << t2.nodes[1].fi << ' ' << t2.nodes[1].se << '\n';
  140.  
  141.     while(q--){
  142.         int x;
  143.         cin >> x;
  144.         x--;
  145.         int a = x - k + 1, b = x + 1;
  146.  
  147.         if(a >= 0){
  148.             tours[a] = update(lrs[x].fi, lrs[x].se, -1, tours[a]);
  149.             tours[a] = update(lrs[x + 1].fi, lrs[x + 1].se, 1, tours[a]);
  150.             t2.update(a, tours[a]->val);
  151.         }
  152.         if(b <= n - k){
  153.             tours[b] = update(lrs[x + 1].fi, lrs[x + 1].se, -1, tours[b]);
  154.             tours[b] = update(lrs[x].fi, lrs[x].se, 1, tours[b]);
  155.             t2.update(b, tours[b]->val);
  156.         }
  157.  
  158.         swap(lrs[x], lrs[x + 1]);
  159.         cout << t2.nodes[1].fi << ' ' << t2.nodes[1].se << '\n';
  160.     }
  161.  
  162.     return 0;
  163. }
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement