Advertisement
Guest User

Можешь почитать, если интересно

a guest
Sep 23rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define all(x) begin(x), end(x)
  5.  
  6. const int N     = 2.1e5;
  7. const int K     = 30;
  8. const int H     = N + 310;
  9. // int overflow?
  10.  
  11. struct fenw {
  12.     int f[N], ind[H], val[H], s;
  13.     fenw() {
  14.         fill(all(f), 0);
  15.         s = 0;
  16.     }
  17.     void add(int i, int d) {
  18.         for (; i < N; i |= i + 1) {
  19.             assert(s < H); /// TL
  20.             ind[s] = i;
  21.             val[s] = f[i];
  22.             ++s;
  23.             f[i] += d;
  24.         }
  25.     }
  26.     int prefix(int i) {
  27.         int su = 0;
  28.         for (; i >= 0; i = (i & (i + 1)) - 1) {
  29.             su += f[i];
  30.         }
  31.         return su;
  32.     }
  33.     void back_to(int t) {
  34.         assert(t >= 0);
  35.         while (s > t) {
  36.             --s;
  37.             f[ind[s]] = val[s];
  38.         }
  39.         assert(s == t);
  40.     }
  41.     int segment(int l, int r) {
  42.         assert(l - 1 <= r);
  43.         return prefix(r) - prefix(l - 1);
  44.     }
  45.     void clear() {
  46.         fill(all(f), 0);
  47.         s = 0;
  48.     }
  49. };
  50.  
  51. vector<int> g[N], vip[N];
  52. int n, m, q, query[N];
  53. int used[N], ff;
  54. int value[N];
  55. ll a[N], b[N];
  56. fenw ab[K + 1], on[K + 1];
  57.  
  58. void solve_block(int lq, int rq) {
  59.     ff = 0;
  60.     fill(all(used), 0);
  61.     for (int i = lq; i < rq; ++i) {
  62.         used[query[i]] = ++ff;
  63.     }
  64.     assert(ff <= K);
  65.     fill(all(a), 0);
  66.     fill(all(b), 0);
  67.     for (int i = 0; i < n; ++i) {
  68.         vip[i].clear();
  69.         for (int j : g[i]) {
  70.             assert(value[i] != value[j]);
  71.             if (value[j] < value[i]) {
  72.                 ++a[i];
  73.             } else {
  74.                 ++b[i];
  75.             }
  76.             if (used[j]) {
  77.                 vip[i].push_back(j);
  78.             }
  79.         }
  80.     }
  81.     ll cur_answer = 0;
  82.     for (int i = 0; i < n; ++i) {
  83.         cur_answer += a[i] * b[i];
  84.         if (!used[i]) {
  85.             continue;
  86.         }
  87.         int v = used[i];
  88.         for (int j : g[i]) {
  89.             assert(value[j] < N);
  90.             on[v].add(value[j], 1);
  91.             ab[v].add(value[j], a[j] - b[j]);
  92.         }
  93.     }
  94.     if (lq == 0) {
  95.         cout << cur_answer << "\n";
  96.     }
  97.     for (int id = lq; id < rq; ++id) {
  98.         int i = query[id];
  99.         assert(used[i]);
  100.         for (int j : vip[i]) {
  101.             assert(used[j]);
  102.             int v = used[j];
  103.             on[v].add(value[j], -1);
  104.             ab[v].add(value[j], -(a[j] - b[j]));
  105.         }
  106.        
  107.         cur_answer -= a[i] * b[i];
  108.         int old = value[i];
  109.         int cur = n + id;
  110.         assert(old != cur);
  111.        
  112.         if (old < cur) {
  113.             assert(used[i]);
  114.             int v = used[i];
  115.             cur_answer += ab[v].segment(old + 1, cur - 1) - on[v].segment(old + 1, cur - 1);
  116.         } else {
  117.             assert(false);
  118.         }
  119.        
  120.         value[i] = n + id;
  121.         a[i] = on[used[i]].prefix(value[i] - 1);
  122.         assert(a[i] == on[used[i]].prefix(value[i]));
  123.         b[i] = ll(g[i].size()) - a[i];
  124.         cur_answer += a[i] * b[i];
  125.        
  126.         for (int j : vip[i]) {
  127.             assert(used[j]);
  128.             int v = used[j];
  129.             on[v].add(value[j], +1);
  130.             ab[v].add(value[j], +(a[j] - b[j]));
  131.             a[j] = on[v].prefix(value[j] - 1);      /// TL
  132.             assert(a[j] == on[v].prefix(value[j])); /// TL
  133.             b[j] = ll(g[j].size()) - a[j];
  134.         }
  135.        
  136.         cout << cur_answer << "\n";
  137.     }
  138.    
  139.     for (int i = 0; i <= K; ++i) {
  140.         ab[i].back_to(0);
  141.         on[i].back_to(0);
  142.     }
  143. }
  144.  
  145. int main() {
  146. #ifdef LC
  147.     assert(freopen("input.txt", "r", stdin));
  148. #endif
  149.     ios::sync_with_stdio(0);
  150.     cin.tie(0);
  151.     cin >> n >> m;
  152.     for (int v, u, i = 0; i < m; ++i) {
  153.         cin >> v >> u;
  154.         --v;
  155.         --u;
  156.         g[v].push_back(u);
  157.         g[u].push_back(v);
  158.     }
  159.     cin >> q;
  160.     for (int i = 0; i < n; ++i) {
  161.         cin >> query[i];
  162.         --query[i];
  163.     }
  164.     iota(value, value + n, 0);
  165.     for (int i = 0; i < q; i += K) {
  166.         solve_block(i, min(q, i + K));
  167.     }
  168.     cerr << 1000 * clock() / CLOCKS_PER_SEC << endl;
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement