Submission #1054486

# Submission time Handle Problem Language Result Execution time Memory
1054486 2024-08-12T10:15:14 Z NeroZein Bubble Sort 2 (JOI18_bubblesort2) C++17
Compilation error
0 ms 0 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif

const int N = 1e6 + 6;

int tree[N];
int seg[N * 2];
int lazy[N * 2];
set<int> occ[N];

void push(int nd, int l, int r) {
  if (!lazy[nd]) {
    return;
  }
  seg[nd] += lazy[nd];
  if (l != r) {
    int mid = (l + r) >> 1;
    int rs = nd + ((mid - l + 1) << 1);
    lazy[nd + 1] += lazy[nd];
    lazy[rs] += lazy[nd];
  }
  lazy[nd] = 0;
}

void upd(int nd, int l, int r, int s, int e, int v) {
  push(nd, l, r); 
  if (l >= s && r <= e) {
    lazy[nd] = v;
    push(nd, l, r); 
    return; 
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1); 
  if (mid >= e) {
    upd(nd + 1, l, mid, s, e, v);
    push(rs, mid + 1, r); 
  } else {
    if (mid < s) {
      push(nd + 1, l, mid); 
      upd(rs, mid + 1, r, s, e, v); 
    } else {
      upd(nd + 1, l, mid, s, e, v);
      upd(rs, mid + 1, r, s, e, v);       
    }
  }
  seg[nd] = max(seg[nd + 1], seg[rs]); 
}

void upd(int nd, int l, int r, int p, int v) {
  if (l == r) {
    seg[nd] = v;
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (p <= mid) {
    upd(nd + 1, l, mid, p, v); 
  } else {
    upd(rs, mid + 1, r, p, v); 
  }
  seg[nd] = max(seg[nd + 1], seg[rs]); 
}

void upd(int id, int v) {
  id++;
  while (id < N) {
    tree[id] += v;
    id += id & -id; 
  }
}

int qry(int id) {
  id++; 
  int ret = 0;
  while (id > 0) {
    ret += tree[id];
    id -= id & -id; 
  }
  return ret;
}
 
vector<int> countScans(vector<int> a, vector<int> x, vector<int> v) {
  int n = (int) a.size();
  int q = (int) x.size();
  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    mp[a[i]];
  }
  for (int i = 0; i < q; ++i) {
    mp[v[i]];
  }
  int cnt = 0; 
  for (auto& [val, cval] : mp) {
    cval = cnt++; 
  }
  for (int i = 0; i < n; ++i) {
    a[i] = mp[a[i]];
  }
  for (int i = 0; i < q; ++i) {
    v[i] = mp[v[i]];
  }
  for (int i = 0; i < n; ++i) {
    occ[a[i]].insert(i); 
    upd(a[i], 1); 
  }
  for (int i = 0; i < n; ++i) {
    int lst = *occ[a[i]].rbegin();
    if (i == lst) {
      int smaller = qry(a[i]);
      upd(0, 0, cnt, a[i], lst + 1 - smaller);
    }
  } 
  vector<int> ret(q);
  for (int i = 0; i < q; ++i) {
    upd(a[x[i]], -1);
    upd(0, 0, cnt, a[x[i]], cnt - 1, 1);
    upd(v[i], 1);
    upd(0, 0, cnt, v[i], cnt - 1, -1); 
    occ[a[x[i]]].erase(x[i]);
    if (!occ[a[x[i]]].empty()) {
      int smaller = qry(a[x[i]]);
      int lst = *occ[a[x[i]]].rbegin();
      upd(0, 0, cnt, a[x[i]], lst + 1 - smaller);      
    }
    occ[v[i]].insert(x[i]);
    int smaller = qry(v[i]);
    int lst = *occ[v[i]].rbegin();
    upd(0, 0, cnt, v[i], lst + 1 - smaller);
    a[x[i]] = v[i];
    ret[i] = seg[0]; 
  }
  return ret; 
}
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q;
  vector<int> a(n); 
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<int> x(q), v(q); 
  for (int i = 0; i < q; ++i) {
    cin >> x[i] >> v[i];
  }
  vector<int> answers = countScans(a, x, v); 
  for (int i : answers) {
    cout << i << '\n';
  }
  return 0;
}

Compilation message

/usr/bin/ld: /tmp/cc7GHEWD.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccIrh4EB.o:bubblesort2.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status