Submission #1109763

# Submission time Handle Problem Language Result Execution time Memory
1109763 2024-11-07T14:02:29 Z VinhLuu Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
1631 ms 87280 KB
#include <bits/stdc++.h>
#define ll long long
#include "bubblesort2.h"
using namespace std;

const int N = 1e6 + 5;
const int oo = -1e9;

int n, m, q, a[N], _x[N], _y[N], kq[N], pos[N];

int st[N << 2], lz[N << 2], le[N], ri[N];

void build(int i,int l,int r){
  if(l == r){
    st[i] = oo;
    return;
  }
  int mid = (l + r) / 2;
  build(i << 1, l, mid);
  build(i << 1|1, mid + 1, r);
  st[i] = max(st[i << 1], st[i << 1|1]);
}

void dolz(int i){
  if(!lz[i]) return;
  int x = lz[i]; lz[i] = 0;
  lz[i << 1] += x;
  lz[i << 1|1] += x;
  st[i << 1] += x;
  st[i << 1|1] += x;
}

void update(int i,int l,int r,int u,int v,int x){
  if(l > r || r < u || v < l) return;
  if(u <= l && r <= v){
    st[i] += x;
    lz[i] += x;
    return;
  }
  dolz(i);
  int mid = (l + r) / 2;
  update(i << 1, l, mid, u, v, x);
  update(i << 1|1, mid + 1, r, u, v, x);
  st[i] = max(st[i << 1], st[i << 1|1]);
}

void change(int i,int l,int r,int u,int val){
  if(l > r || r < u || u < l) return;
  if(l == r){
    st[i] = val;
    lz[i] = 0;
    return;
  }
  dolz(i);
  int mid = (l + r) / 2;
  change(i << 1, l, mid, u, val);
  change(i << 1|1, mid + 1, r, u, val);
  st[i] = max(st[i << 1], st[i << 1|1]);
}

int b[N], T[N];

void inc(int x,int val){
  for(int i = x; i <= m; i += i & (-i)) T[i] += val;
}

int query(int x){
  int ret = 0;
  for(int i = x; i; i -= i & (-i)) ret += T[i];
  return ret;
}

int c[N];

std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V){
  n = A.size();
  for(int i = 0; i < n; i ++){
    a[i + 1] = A[i];
    c[++m] = i + 1;
  }

  q = X.size();

  for(int k = 0; k < q; k ++){
    _x[k + 1] = X[k] + 1, _y[k + 1] = V[k];
    c[++m] = n + k + 1;
    a[n + k + 1] = _y[k + 1];
  }

  sort(c + 1, c + m + 1, [&](int x,int y){return a[x] < a[y];});

  int last = 1;

  for(int i = 1; i <= m; i ++){
    if(a[c[i]] != a[c[i - 1]]) last = i;
    le[i] = last;
    pos[c[i]] = i;
  }
  last = m;
  for(int i = m; i >= 1; i --){
    if(a[c[i]] != a[c[i + 1]]) last = i;
    ri[i] = last;
  }

//  for(int i = 1; i <= m; i ++) cerr << i << " " << pos[i] << " " << le[pos[i]] << " " << ri[pos[i]] << " k\n";

  build(1, 1, m);
  for(int i = 1; i <= n; i ++){
    change(1, 1, m, pos[i], i);
    inc(pos[i], 1);
  }
  for(int i = 1; i <= n; i ++){
    update(1, 1, m, le[pos[i]], m, -1);
    b[i] = i;
  }

  for(int k = 1; k <= q; k ++){
    int x = _x[k];
    int y = _y[k];
    int tmp = pos[n + k];
    update(1, 1, m, le[pos[b[x]]], m, +1);
    change(1, 1, m, pos[b[x]], oo);
    inc(pos[b[x]], -1);
    inc(tmp, 1);
    update(1, 1, m, le[tmp], m, -1);
    change(1, 1, m, tmp, x - query(ri[tmp]));

    b[x] = n + k;
    kq[k] = st[1];
  }

	std::vector<int> answer(q);
	for(int j = 0; j < q; j ++) {
		answer[j] = kq[j + 1];
	}
	return answer;
}

//#define LOCAL

#ifdef LOCAL

int _n, _q;

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }

	cin >> _n >> _q;

	std::vector<int> A(_n);
  for(int i = 0; i < _n; i ++)
    cin >> A[i];

	std::vector<int> X(_q),V(_q);
	for(int j = 0; j < _q ; j++){
		cin >> X[j] >> V[j];
	}


	std::vector<int> res=countScans(A,X,V);

	for(int j=0;j<int(res.size());j++)
		cout << res[j] << "\n";
}

/*
4 2
1 2 3 4
0 3
2 1
*/

#endif // LOCAL

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:119:9: warning: unused variable 'y' [-Wunused-variable]
  119 |     int y = _y[k];
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 20816 KB Output is correct
3 Correct 6 ms 21072 KB Output is correct
4 Correct 6 ms 21072 KB Output is correct
5 Correct 6 ms 21040 KB Output is correct
6 Correct 6 ms 21084 KB Output is correct
7 Correct 5 ms 20940 KB Output is correct
8 Correct 5 ms 21072 KB Output is correct
9 Correct 6 ms 21072 KB Output is correct
10 Correct 6 ms 21108 KB Output is correct
11 Correct 6 ms 21152 KB Output is correct
12 Correct 6 ms 21072 KB Output is correct
13 Correct 7 ms 21072 KB Output is correct
14 Correct 6 ms 21072 KB Output is correct
15 Correct 6 ms 21072 KB Output is correct
16 Correct 5 ms 21072 KB Output is correct
17 Correct 5 ms 21072 KB Output is correct
18 Correct 5 ms 21072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 20816 KB Output is correct
3 Correct 6 ms 21072 KB Output is correct
4 Correct 6 ms 21072 KB Output is correct
5 Correct 6 ms 21040 KB Output is correct
6 Correct 6 ms 21084 KB Output is correct
7 Correct 5 ms 20940 KB Output is correct
8 Correct 5 ms 21072 KB Output is correct
9 Correct 6 ms 21072 KB Output is correct
10 Correct 6 ms 21108 KB Output is correct
11 Correct 6 ms 21152 KB Output is correct
12 Correct 6 ms 21072 KB Output is correct
13 Correct 7 ms 21072 KB Output is correct
14 Correct 6 ms 21072 KB Output is correct
15 Correct 6 ms 21072 KB Output is correct
16 Correct 5 ms 21072 KB Output is correct
17 Correct 5 ms 21072 KB Output is correct
18 Correct 5 ms 21072 KB Output is correct
19 Correct 14 ms 21324 KB Output is correct
20 Correct 20 ms 21576 KB Output is correct
21 Correct 14 ms 21584 KB Output is correct
22 Correct 18 ms 21584 KB Output is correct
23 Correct 20 ms 21584 KB Output is correct
24 Correct 15 ms 21328 KB Output is correct
25 Correct 14 ms 21328 KB Output is correct
26 Correct 14 ms 21328 KB Output is correct
27 Correct 15 ms 21328 KB Output is correct
28 Correct 15 ms 21500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 21636 KB Output is correct
2 Correct 50 ms 24776 KB Output is correct
3 Correct 93 ms 28000 KB Output is correct
4 Correct 80 ms 27984 KB Output is correct
5 Correct 84 ms 28180 KB Output is correct
6 Correct 77 ms 27976 KB Output is correct
7 Correct 81 ms 28132 KB Output is correct
8 Correct 77 ms 27976 KB Output is correct
9 Correct 86 ms 27980 KB Output is correct
10 Correct 71 ms 28236 KB Output is correct
11 Correct 75 ms 28232 KB Output is correct
12 Correct 78 ms 28232 KB Output is correct
13 Correct 78 ms 28232 KB Output is correct
14 Correct 70 ms 28048 KB Output is correct
15 Correct 78 ms 28252 KB Output is correct
16 Correct 79 ms 27976 KB Output is correct
17 Correct 70 ms 28244 KB Output is correct
18 Correct 70 ms 28232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 20816 KB Output is correct
3 Correct 6 ms 21072 KB Output is correct
4 Correct 6 ms 21072 KB Output is correct
5 Correct 6 ms 21040 KB Output is correct
6 Correct 6 ms 21084 KB Output is correct
7 Correct 5 ms 20940 KB Output is correct
8 Correct 5 ms 21072 KB Output is correct
9 Correct 6 ms 21072 KB Output is correct
10 Correct 6 ms 21108 KB Output is correct
11 Correct 6 ms 21152 KB Output is correct
12 Correct 6 ms 21072 KB Output is correct
13 Correct 7 ms 21072 KB Output is correct
14 Correct 6 ms 21072 KB Output is correct
15 Correct 6 ms 21072 KB Output is correct
16 Correct 5 ms 21072 KB Output is correct
17 Correct 5 ms 21072 KB Output is correct
18 Correct 5 ms 21072 KB Output is correct
19 Correct 14 ms 21324 KB Output is correct
20 Correct 20 ms 21576 KB Output is correct
21 Correct 14 ms 21584 KB Output is correct
22 Correct 18 ms 21584 KB Output is correct
23 Correct 20 ms 21584 KB Output is correct
24 Correct 15 ms 21328 KB Output is correct
25 Correct 14 ms 21328 KB Output is correct
26 Correct 14 ms 21328 KB Output is correct
27 Correct 15 ms 21328 KB Output is correct
28 Correct 15 ms 21500 KB Output is correct
29 Correct 17 ms 21636 KB Output is correct
30 Correct 50 ms 24776 KB Output is correct
31 Correct 93 ms 28000 KB Output is correct
32 Correct 80 ms 27984 KB Output is correct
33 Correct 84 ms 28180 KB Output is correct
34 Correct 77 ms 27976 KB Output is correct
35 Correct 81 ms 28132 KB Output is correct
36 Correct 77 ms 27976 KB Output is correct
37 Correct 86 ms 27980 KB Output is correct
38 Correct 71 ms 28236 KB Output is correct
39 Correct 75 ms 28232 KB Output is correct
40 Correct 78 ms 28232 KB Output is correct
41 Correct 78 ms 28232 KB Output is correct
42 Correct 70 ms 28048 KB Output is correct
43 Correct 78 ms 28252 KB Output is correct
44 Correct 79 ms 27976 KB Output is correct
45 Correct 70 ms 28244 KB Output is correct
46 Correct 70 ms 28232 KB Output is correct
47 Correct 307 ms 50648 KB Output is correct
48 Correct 1236 ms 84808 KB Output is correct
49 Correct 1378 ms 87056 KB Output is correct
50 Correct 1500 ms 87056 KB Output is correct
51 Correct 1631 ms 87152 KB Output is correct
52 Correct 1550 ms 87060 KB Output is correct
53 Correct 1524 ms 87044 KB Output is correct
54 Correct 1423 ms 87196 KB Output is correct
55 Correct 1558 ms 87244 KB Output is correct
56 Correct 1316 ms 87280 KB Output is correct
57 Correct 1446 ms 87188 KB Output is correct
58 Correct 1361 ms 87248 KB Output is correct
59 Correct 1307 ms 85872 KB Output is correct
60 Correct 1229 ms 85816 KB Output is correct
61 Correct 1336 ms 85840 KB Output is correct
62 Correct 1273 ms 85832 KB Output is correct
63 Correct 1211 ms 85724 KB Output is correct
64 Correct 1090 ms 85728 KB Output is correct
65 Correct 1177 ms 85848 KB Output is correct
66 Correct 1280 ms 75080 KB Output is correct
67 Correct 1163 ms 85584 KB Output is correct