EntropyIncreaser

NOIP2017 phalanx2 FENWICK

Mar 26th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. #include <vector>
  4.  
  5. #define LOG(FMT...) // fprintf(stderr, FMT)
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10.  
  11. struct Phalanx {
  12.     struct Node {
  13.         int sum;
  14.         bool f;
  15.         Node *prt, *l, *r;
  16.     };
  17.    
  18.     Node* fw;
  19.     int len, dif;
  20.     ll fst;
  21.     vector<ll> ins;
  22.    
  23.     static Node* create(int sum) {
  24.         static Node pool[300010 * 80];
  25.         static Node* p = pool;
  26.         p->f = true;
  27.         p->sum = sum;
  28.         return p++;
  29.     }
  30.  
  31.     int lowBit(int k) {
  32.         return k & -k;
  33.     }
  34.  
  35.     ll rmv(int k) {
  36.         int id = 0;
  37.         Node* p = fw;
  38.         for (int l = 20; l >= 0; --l) {
  39.             if (p->sum == k && p->f) {
  40.                 id |= 1 << l;
  41.                 break;
  42.             }
  43.             if (p->sum < k) {
  44.                 id |= 1 << l;
  45.                 k -= p->sum;
  46.                 if (!p->r) {
  47.                     p->r = create(1 << (l - 1));
  48.                     p->r->prt = p->prt;
  49.                 }
  50.                 p = p->r;
  51.             } else {
  52.                 if (!p->l) {
  53.                     p->l = create(1 << (l - 1));
  54.                     p->l->prt = p;
  55.                 }
  56.                 p = p->l;
  57.             }
  58.         }
  59.         p->f = false;
  60.         while (p) {
  61.             --p->sum;
  62.             p = p->prt;
  63.         }
  64.         LOG("[LOG] %d %d\n", id, len);
  65.         if (id <= len)
  66.             return fst + id * (ll)dif;
  67.         return ins[id - len - 1];
  68.     }
  69.    
  70.     void push(ll v) {
  71.         ins.push_back(v);
  72.     }
  73. };
  74.  
  75. const int N = 300010;
  76.  
  77. int n, m, q;
  78.  
  79. Phalanx r[N], c;
  80.  
  81. int main() {
  82. //  freopen("phalanx1.in", "r", stdin);
  83. //  freopen("phalanx1.out", "w", stdout);
  84.     scanf("%d%d%d", &n, &m, &q);
  85.     for (int i = 1; i <= n; ++i) {
  86.         r[i].fst = (i - 1) * (ll)m;
  87.         r[i].len = m - 1;
  88.         r[i].dif = 1;
  89.         r[i].fw = r[i].create(1 << 20);
  90.     }
  91.     c.len = n;
  92.     c.dif = m;
  93.     c.fw = c.create(1 << 20);
  94.     while (q--) {
  95.         int x, y;
  96.         scanf("%d%d", &x, &y);
  97.         if (y == m) {
  98.             ll v = c.rmv(x);
  99.             printf("%lld\n", v);
  100.             c.push(v);
  101.         } else {
  102.             ll v = r[x].rmv(y);
  103.             printf("%lld\n", v);
  104.             r[x].push(c.rmv(x));
  105.             c.push(v);
  106.         }
  107.     }
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment