답안 #1054525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1054525 2024-08-12T10:28:49 Z NeroZein Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
2552 ms 164076 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) {
  push(nd, l, r); 
  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); 
    push(rs, mid + 1, r); 
  } else {
    upd(rs, mid + 1, r, p, v); 
    push(nd + 1, l, mid);
  }
  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);
    }
  } 
  for (int i = 0; i < cnt; ++i) {
    if (occ[i].empty()) {
      upd(0, 0, cnt, i, -N);
    }
  }
  vector<int> ret(q);
  for (int i = 0; i < q; ++i) {
    if (v[i] != a[x[i]]) {
      upd(a[x[i]], -1);
      upd(v[i], 1);
      upd(0, 0, cnt, a[x[i]], cnt - 1, 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();
        //debug(lst, smaller); 
        upd(0, 0, cnt, a[x[i]], lst + 1 - smaller);      
      } else {
        upd(0, 0, cnt, a[x[i]], -N); 
      }
      occ[v[i]].insert(x[i]);
      int smaller = qry(v[i]);
      int lst = *occ[v[i]].rbegin();
      //debug(smaller, lst);
      upd(0, 0, cnt, v[i], lst + 1 - smaller);
      a[x[i]] = v[i];
      debug(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;
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 53848 KB Output is correct
2 Correct 8 ms 54108 KB Output is correct
3 Correct 10 ms 54224 KB Output is correct
4 Correct 10 ms 54360 KB Output is correct
5 Correct 10 ms 54144 KB Output is correct
6 Correct 11 ms 54104 KB Output is correct
7 Correct 10 ms 54108 KB Output is correct
8 Correct 11 ms 54364 KB Output is correct
9 Correct 11 ms 54188 KB Output is correct
10 Correct 9 ms 54108 KB Output is correct
11 Correct 10 ms 54308 KB Output is correct
12 Correct 10 ms 54108 KB Output is correct
13 Correct 9 ms 54128 KB Output is correct
14 Correct 10 ms 54108 KB Output is correct
15 Correct 10 ms 54108 KB Output is correct
16 Correct 10 ms 54288 KB Output is correct
17 Correct 10 ms 54108 KB Output is correct
18 Correct 11 ms 54272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 53848 KB Output is correct
2 Correct 8 ms 54108 KB Output is correct
3 Correct 10 ms 54224 KB Output is correct
4 Correct 10 ms 54360 KB Output is correct
5 Correct 10 ms 54144 KB Output is correct
6 Correct 11 ms 54104 KB Output is correct
7 Correct 10 ms 54108 KB Output is correct
8 Correct 11 ms 54364 KB Output is correct
9 Correct 11 ms 54188 KB Output is correct
10 Correct 9 ms 54108 KB Output is correct
11 Correct 10 ms 54308 KB Output is correct
12 Correct 10 ms 54108 KB Output is correct
13 Correct 9 ms 54128 KB Output is correct
14 Correct 10 ms 54108 KB Output is correct
15 Correct 10 ms 54108 KB Output is correct
16 Correct 10 ms 54288 KB Output is correct
17 Correct 10 ms 54108 KB Output is correct
18 Correct 11 ms 54272 KB Output is correct
19 Correct 20 ms 55128 KB Output is correct
20 Correct 23 ms 55388 KB Output is correct
21 Correct 22 ms 55468 KB Output is correct
22 Correct 22 ms 55388 KB Output is correct
23 Correct 21 ms 55372 KB Output is correct
24 Correct 21 ms 55388 KB Output is correct
25 Correct 21 ms 55132 KB Output is correct
26 Correct 21 ms 55224 KB Output is correct
27 Correct 20 ms 55132 KB Output is correct
28 Correct 20 ms 55216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 55384 KB Output is correct
2 Correct 43 ms 56872 KB Output is correct
3 Correct 67 ms 58252 KB Output is correct
4 Correct 64 ms 58200 KB Output is correct
5 Correct 62 ms 58100 KB Output is correct
6 Correct 62 ms 58192 KB Output is correct
7 Correct 60 ms 58040 KB Output is correct
8 Correct 60 ms 58192 KB Output is correct
9 Correct 61 ms 58016 KB Output is correct
10 Correct 36 ms 58248 KB Output is correct
11 Correct 35 ms 58200 KB Output is correct
12 Correct 36 ms 58108 KB Output is correct
13 Correct 41 ms 58196 KB Output is correct
14 Correct 40 ms 58204 KB Output is correct
15 Correct 39 ms 58196 KB Output is correct
16 Correct 45 ms 58200 KB Output is correct
17 Correct 45 ms 58192 KB Output is correct
18 Correct 44 ms 58196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 53848 KB Output is correct
2 Correct 8 ms 54108 KB Output is correct
3 Correct 10 ms 54224 KB Output is correct
4 Correct 10 ms 54360 KB Output is correct
5 Correct 10 ms 54144 KB Output is correct
6 Correct 11 ms 54104 KB Output is correct
7 Correct 10 ms 54108 KB Output is correct
8 Correct 11 ms 54364 KB Output is correct
9 Correct 11 ms 54188 KB Output is correct
10 Correct 9 ms 54108 KB Output is correct
11 Correct 10 ms 54308 KB Output is correct
12 Correct 10 ms 54108 KB Output is correct
13 Correct 9 ms 54128 KB Output is correct
14 Correct 10 ms 54108 KB Output is correct
15 Correct 10 ms 54108 KB Output is correct
16 Correct 10 ms 54288 KB Output is correct
17 Correct 10 ms 54108 KB Output is correct
18 Correct 11 ms 54272 KB Output is correct
19 Correct 20 ms 55128 KB Output is correct
20 Correct 23 ms 55388 KB Output is correct
21 Correct 22 ms 55468 KB Output is correct
22 Correct 22 ms 55388 KB Output is correct
23 Correct 21 ms 55372 KB Output is correct
24 Correct 21 ms 55388 KB Output is correct
25 Correct 21 ms 55132 KB Output is correct
26 Correct 21 ms 55224 KB Output is correct
27 Correct 20 ms 55132 KB Output is correct
28 Correct 20 ms 55216 KB Output is correct
29 Correct 15 ms 55384 KB Output is correct
30 Correct 43 ms 56872 KB Output is correct
31 Correct 67 ms 58252 KB Output is correct
32 Correct 64 ms 58200 KB Output is correct
33 Correct 62 ms 58100 KB Output is correct
34 Correct 62 ms 58192 KB Output is correct
35 Correct 60 ms 58040 KB Output is correct
36 Correct 60 ms 58192 KB Output is correct
37 Correct 61 ms 58016 KB Output is correct
38 Correct 36 ms 58248 KB Output is correct
39 Correct 35 ms 58200 KB Output is correct
40 Correct 36 ms 58108 KB Output is correct
41 Correct 41 ms 58196 KB Output is correct
42 Correct 40 ms 58204 KB Output is correct
43 Correct 39 ms 58196 KB Output is correct
44 Correct 45 ms 58200 KB Output is correct
45 Correct 45 ms 58192 KB Output is correct
46 Correct 44 ms 58196 KB Output is correct
47 Correct 465 ms 86492 KB Output is correct
48 Correct 1982 ms 153656 KB Output is correct
49 Correct 2209 ms 163916 KB Output is correct
50 Correct 2231 ms 164076 KB Output is correct
51 Correct 2225 ms 163916 KB Output is correct
52 Correct 2552 ms 163904 KB Output is correct
53 Correct 2277 ms 163792 KB Output is correct
54 Correct 2061 ms 163924 KB Output is correct
55 Correct 2180 ms 163876 KB Output is correct
56 Correct 2347 ms 163924 KB Output is correct
57 Correct 2455 ms 163924 KB Output is correct
58 Correct 2198 ms 164056 KB Output is correct
59 Correct 1839 ms 155352 KB Output is correct
60 Correct 1754 ms 155468 KB Output is correct
61 Correct 1832 ms 155308 KB Output is correct
62 Correct 1681 ms 150948 KB Output is correct
63 Correct 1763 ms 151708 KB Output is correct
64 Correct 1675 ms 151636 KB Output is correct
65 Correct 1538 ms 148144 KB Output is correct
66 Correct 1602 ms 147024 KB Output is correct
67 Correct 1572 ms 148144 KB Output is correct