답안 #367318

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
367318 2021-02-16T20:55:47 Z cheissmart Bubble Sort 2 (JOI18_bubblesort2) C++14
100 / 100
4173 ms 144712 KB
#include "bubblesort2.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e6 + 7;

struct node {
	int mx, add, lz;
	node(int x = 0) {
		mx = x;
		add = lz = 0;		
	}
} t[N * 4];

int n;

void apply(int v, int x) {
	t[v].mx += x;
	t[v].add += x;
	t[v].lz += x;
}

void push(int v) {
	apply(v * 2, t[v].lz);
	apply(v * 2 + 1, t[v].lz);
	t[v].lz = 0;
}

void pull(int v) {
	t[v].mx = max(t[v * 2].mx, t[v * 2 + 1].mx);
}

void build(int v = 1, int tl = 0, int tr = n) {
	if(tr - tl == 1) {
		t[v] = node(-INF);
		return;
	}
	int tm = (tl + tr) / 2;
	build(v * 2, tl, tm);
	build(v * 2 + 1, tm, tr);
	pull(v);
}

void add(int l, int r, int x, int v = 1, int tl = 0, int tr = n) {
	if(l <= tl && tr <= r) {
		apply(v, x);
		return;
	}
	push(v);
	int tm = (tl + tr) / 2;
	if(l < tm) add(l, r, x, v * 2, tl, tm);
	if(r > tm) add(l, r, x, v * 2 + 1, tm, tr);
	pull(v);
}

void set_val(int pos, int x, int v = 1, int tl = 0, int tr = n) {
	if(tr - tl == 1) {
		t[v].mx = x;
		return;
	}
	push(v);
	int tm = (tl + tr) / 2;
	if(pos < tm) set_val(pos, x, v * 2, tl, tm);
	else set_val(pos, x, v * 2 + 1, tm, tr);
	pull(v);
}

int get_add(int pos, int v = 1, int tl = 0, int tr = n) {
	if(tr - tl == 1)
		return t[v].add;
	push(v);
	int tm = (tl + tr) / 2;
	if(pos < tm) return get_add(pos, v * 2, tl, tm);
	else return get_add(pos, v * 2 + 1, tm, tr);
}

vi countScans(vi A, vi X, vi Y) {
	int Q = X.size(), N = A.size();
	V<pi> v;
	for(int i = 0; i < N; i++) v.EB(A[i], i);
	for(int i = 0; i < Q; i++) v.EB(Y[i], X[i]);
	map<pi, int> id;
	sort(ALL(v));
	v.resize(unique(ALL(v)) - v.begin());
	for(int i = 0; i < v.size(); i++)
		id[v[i]] = i;
	n = v.size();
	build();
	for(int i = 0; i < N; i++) {
		int x = id[{A[i], i}];
		A[i] = x;
		set_val(x, i);
	}
	for(int i = 0; i < N; i++)
		if(A[i] + 1 < n)
			add(A[i] + 1, n, -1);
	vi ans(Q);
	for(int i = 0; i < Q; i++) {
		int pos = X[i], val = Y[i], old_val = A[pos];
		val = id[{val, pos}];
		A[pos] = val;
		// old_val -> val
		if(old_val + 1 < n)
			add(old_val + 1, n, 1);
		set_val(old_val, -INF);
		int added = get_add(val);
		set_val(val, added + pos);
		if(val + 1 < n)
			add(val + 1, n, -1);
		ans[i] = t[1].mx;
	}
	return ans;
}


Compilation message

bubblesort2.cpp: In function 'vi countScans(vi, vi, vi)':
bubblesort2.cpp:98:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |  for(int i = 0; i < v.size(); i++)
      |                 ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 47340 KB Output is correct
2 Correct 33 ms 47488 KB Output is correct
3 Correct 39 ms 47744 KB Output is correct
4 Correct 40 ms 47616 KB Output is correct
5 Correct 37 ms 47724 KB Output is correct
6 Correct 36 ms 47596 KB Output is correct
7 Correct 36 ms 47596 KB Output is correct
8 Correct 36 ms 47596 KB Output is correct
9 Correct 37 ms 47596 KB Output is correct
10 Correct 36 ms 47724 KB Output is correct
11 Correct 37 ms 47596 KB Output is correct
12 Correct 37 ms 47596 KB Output is correct
13 Correct 36 ms 47596 KB Output is correct
14 Correct 36 ms 47724 KB Output is correct
15 Correct 36 ms 47596 KB Output is correct
16 Correct 36 ms 47628 KB Output is correct
17 Correct 41 ms 47616 KB Output is correct
18 Correct 35 ms 47596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 47340 KB Output is correct
2 Correct 33 ms 47488 KB Output is correct
3 Correct 39 ms 47744 KB Output is correct
4 Correct 40 ms 47616 KB Output is correct
5 Correct 37 ms 47724 KB Output is correct
6 Correct 36 ms 47596 KB Output is correct
7 Correct 36 ms 47596 KB Output is correct
8 Correct 36 ms 47596 KB Output is correct
9 Correct 37 ms 47596 KB Output is correct
10 Correct 36 ms 47724 KB Output is correct
11 Correct 37 ms 47596 KB Output is correct
12 Correct 37 ms 47596 KB Output is correct
13 Correct 36 ms 47596 KB Output is correct
14 Correct 36 ms 47724 KB Output is correct
15 Correct 36 ms 47596 KB Output is correct
16 Correct 36 ms 47628 KB Output is correct
17 Correct 41 ms 47616 KB Output is correct
18 Correct 35 ms 47596 KB Output is correct
19 Correct 53 ms 48620 KB Output is correct
20 Correct 60 ms 48876 KB Output is correct
21 Correct 57 ms 48876 KB Output is correct
22 Correct 59 ms 48876 KB Output is correct
23 Correct 56 ms 48620 KB Output is correct
24 Correct 54 ms 48620 KB Output is correct
25 Correct 56 ms 48620 KB Output is correct
26 Correct 54 ms 48620 KB Output is correct
27 Correct 55 ms 48492 KB Output is correct
28 Correct 54 ms 48492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 49660 KB Output is correct
2 Correct 153 ms 52580 KB Output is correct
3 Correct 270 ms 56288 KB Output is correct
4 Correct 276 ms 56288 KB Output is correct
5 Correct 282 ms 56260 KB Output is correct
6 Correct 298 ms 56160 KB Output is correct
7 Correct 271 ms 56032 KB Output is correct
8 Correct 268 ms 56160 KB Output is correct
9 Correct 259 ms 56160 KB Output is correct
10 Correct 193 ms 53600 KB Output is correct
11 Correct 202 ms 53472 KB Output is correct
12 Correct 205 ms 53472 KB Output is correct
13 Correct 193 ms 53600 KB Output is correct
14 Correct 186 ms 53472 KB Output is correct
15 Correct 194 ms 53472 KB Output is correct
16 Correct 179 ms 53544 KB Output is correct
17 Correct 181 ms 53472 KB Output is correct
18 Correct 187 ms 53600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 47340 KB Output is correct
2 Correct 33 ms 47488 KB Output is correct
3 Correct 39 ms 47744 KB Output is correct
4 Correct 40 ms 47616 KB Output is correct
5 Correct 37 ms 47724 KB Output is correct
6 Correct 36 ms 47596 KB Output is correct
7 Correct 36 ms 47596 KB Output is correct
8 Correct 36 ms 47596 KB Output is correct
9 Correct 37 ms 47596 KB Output is correct
10 Correct 36 ms 47724 KB Output is correct
11 Correct 37 ms 47596 KB Output is correct
12 Correct 37 ms 47596 KB Output is correct
13 Correct 36 ms 47596 KB Output is correct
14 Correct 36 ms 47724 KB Output is correct
15 Correct 36 ms 47596 KB Output is correct
16 Correct 36 ms 47628 KB Output is correct
17 Correct 41 ms 47616 KB Output is correct
18 Correct 35 ms 47596 KB Output is correct
19 Correct 53 ms 48620 KB Output is correct
20 Correct 60 ms 48876 KB Output is correct
21 Correct 57 ms 48876 KB Output is correct
22 Correct 59 ms 48876 KB Output is correct
23 Correct 56 ms 48620 KB Output is correct
24 Correct 54 ms 48620 KB Output is correct
25 Correct 56 ms 48620 KB Output is correct
26 Correct 54 ms 48620 KB Output is correct
27 Correct 55 ms 48492 KB Output is correct
28 Correct 54 ms 48492 KB Output is correct
29 Correct 70 ms 49660 KB Output is correct
30 Correct 153 ms 52580 KB Output is correct
31 Correct 270 ms 56288 KB Output is correct
32 Correct 276 ms 56288 KB Output is correct
33 Correct 282 ms 56260 KB Output is correct
34 Correct 298 ms 56160 KB Output is correct
35 Correct 271 ms 56032 KB Output is correct
36 Correct 268 ms 56160 KB Output is correct
37 Correct 259 ms 56160 KB Output is correct
38 Correct 193 ms 53600 KB Output is correct
39 Correct 202 ms 53472 KB Output is correct
40 Correct 205 ms 53472 KB Output is correct
41 Correct 193 ms 53600 KB Output is correct
42 Correct 186 ms 53472 KB Output is correct
43 Correct 194 ms 53472 KB Output is correct
44 Correct 179 ms 53544 KB Output is correct
45 Correct 181 ms 53472 KB Output is correct
46 Correct 187 ms 53600 KB Output is correct
47 Correct 1004 ms 76048 KB Output is correct
48 Correct 3724 ms 136308 KB Output is correct
49 Correct 4147 ms 144492 KB Output is correct
50 Correct 4173 ms 144516 KB Output is correct
51 Correct 4147 ms 144392 KB Output is correct
52 Correct 4141 ms 144416 KB Output is correct
53 Correct 4160 ms 144336 KB Output is correct
54 Correct 3649 ms 144632 KB Output is correct
55 Correct 4003 ms 144712 KB Output is correct
56 Correct 3582 ms 144512 KB Output is correct
57 Correct 3831 ms 144524 KB Output is correct
58 Correct 3534 ms 144572 KB Output is correct
59 Correct 3323 ms 135468 KB Output is correct
60 Correct 3384 ms 135416 KB Output is correct
61 Correct 3346 ms 135368 KB Output is correct
62 Correct 3239 ms 131400 KB Output is correct
63 Correct 3238 ms 131476 KB Output is correct
64 Correct 3238 ms 131528 KB Output is correct
65 Correct 3177 ms 127404 KB Output is correct
66 Correct 3225 ms 127272 KB Output is correct
67 Correct 3248 ms 127280 KB Output is correct