Submission #1124925

#TimeUsernameProblemLanguageResultExecution timeMemory
1124925fryingducBubble Sort 2 (JOI18_bubblesort2)C++20
100 / 100
1883 ms65148 KiB
#include "bits/stdc++.h"
using namespace std;

#ifdef duc_debug
#include "bits/debug.h"
#else
#include "bubblesort2.h"
#define debug(...)
#endif

const int maxn = 1e6 + 6;
const int inf = 1e9;
const int offset = 5e7;
int n, q, a[maxn];
int qx[maxn], qv[maxn];
int ra[maxn], rq[maxn];

pair<int, int> cur_val[maxn];

long long res[maxn];

struct segment_tree {
  int n;
  vector<int> tree, lazy;
  segment_tree() {}
  segment_tree(int n) : n(n), tree(n * 4 + 6), lazy(n * 4 + 6) {}
  void down(int ind, int l, int r) {
    tree[ind] += lazy[ind];
    if(l != r) {
      lazy[ind << 1] += lazy[ind];
      lazy[ind << 1 | 1] += lazy[ind];
    }
    lazy[ind] = 0;
  }
  void update(int ind, int l, int r, int x, int y, int val) {
    if(lazy[ind]) down(ind, l, r);
    if(l > y || r < x) return;
    if(x <= l and r <= y) {
      lazy[ind] += val;
      down(ind, l, r);
      return;
    }
    int mid = (l + r) >> 1;
    update(ind << 1, l, mid, x, y, val);
    update(ind << 1 | 1, mid + 1, r, x, y, val);
    tree[ind] = max(tree[ind << 1], tree[ind << 1 | 1]);
  }
  int get(int ind, int l, int r, int x, int y) {
    if(lazy[ind]) down(ind, l, r);
    if(l > y || r < x) return -2e9;
    if(x <= l and r <= y) {
      return tree[ind];
    }
    int mid = (l + r) >> 1;
    return max(get(ind << 1, l, mid, x, y), get(ind << 1 | 1, mid + 1, r, x, y));
  }
  void update(int x, int y, int val) {
    if(x > y) return;
    update(1, 1, n, x, y, val);
  }
  int get(int x, int y) {
    if(x > y) return -1e9;
    return get(1, 1, n, x, y);
  }
} st;
int bit[maxn];
int m;
void update(int i, int val) {
  for(; i <= m; i += i & (-i)) {
    bit[i] += val;
  }
}
int get(int i) {
  int ans = 0;
  for(; i > 0; i -= i & (-i)) {
    ans += bit[i];
  }
  return ans;
}
vector<int> countScans(vector<int> A, vector<int> X, vector<int> V) {
  n = (int)A.size();
  q = (int)X.size();
  vector<pair<int, int>> vec;
  for(int i = 1; i <= n; ++i) {
    a[i] = A[i - 1];
    vec.push_back(make_pair(a[i], i));
  }
  for(int i = 1; i <= q; ++i) {
    qx[i] = X[i - 1] + 1, qv[i] = V[i - 1];
    vec.push_back(make_pair(qv[i], qx[i]));
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  m = (int)vec.size();
  for(int i = 1; i <= n; ++i) {
    a[i] = lower_bound(vec.begin(), vec.end(), make_pair(a[i], i)) - vec.begin() + 1;
    update(a[i], 1);
  }
  for(int i = 1; i <= q; ++i) {
    qv[i] = lower_bound(vec.begin(), vec.end(), make_pair(qv[i], qx[i])) - vec.begin() + 1;
  }
  st = segment_tree(m);
  st.update(1, m, -offset);
  for(int i = n; i; --i) {
    st.update(a[i], a[i], i - get(a[i] - 1) - 1 + offset);
  }
  debug(st.get(1, m));
  vector<int> ret;
  for(int i = 1; i <= q; ++i) {
    int id = qx[i];
    
    st.update(a[id] + 1, m, 1);
    
    st.update(a[id], a[id], -offset);
    update(a[id], -1);
    
    update(qv[i], 1);
    int oe = get(qv[i] - 1);
    st.update(qv[i], qv[i], (id - oe - 1) - st.get(qv[i], qv[i]));
    
    st.update(qv[i] + 1, m, -1);
    
//    for(int j = 1; j <= m; ++j) {
//      cout << st.get(j, j) << " \n"[j == m];
//    }
    
    ret.push_back(st.get(1, m));
    a[id] = qv[i];
  }
  return ret;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...