답안 #412152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412152 2021-05-26T14:57:47 Z ritul_kr_singh Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
1832 ms 61176 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sp << ' ' <<
#define nl << '\n'
#include "bubblesort2.h"

struct FenwickTree{
	vector<int> a; int n, s;
	FenwickTree(int N){ a.resize((n=N)+1); }
	void add(int i, int v){
		for(++i; i<=n; i+=i&-i) a[i] += v;
	}
	int get(int i){
		for(s=0; i>=1; i-=i&-i) s += a[i];
		return s;
	}
	int get(int l, int r){ return get(r+1) - get(l); }
	int lower(int v){
		int i = 0;
		for(s=1<<20; s; s/=2) if(i+s<=n && a[i+s]<v) v -= a[i+=s];
		return i;
	}
};

const int INF = 1e7;

struct SegmentTree{
	vector<int> a, b; int sz = 1;
	const int ID = -INF;
	SegmentTree(vector<int> &v){
		while(sz < (int)v.size()) sz += sz;
		a.assign(2*sz, ID);
		b.resize(2*sz);
		build(v, 0, 0, sz);
	}
	void build(vector<int> &v, int x, int lx, int rx){
		if(rx-lx==1){
			if(lx < (int)v.size()) a[x] = v[lx];
			return;
		}
		int mx = (lx + rx) / 2;
		build(v, 2*x+1, lx, mx);
		build(v, 2*x+2, mx, rx);
		a[x] = max(a[2*x+1], a[2*x+2]);
	}
	void update(int i, int v, int x, int lx, int rx){
		if(rx-lx==1) return void(a[x] = v);
		int mx = (lx + rx) / 2; v -= b[x];
		if(i < mx) update(i, v, 2*x+1, lx, mx);
		else update(i, v, 2*x+2, mx, rx);
		a[x] = b[x] + max(a[2*x+1], a[2*x+2]);
	}
	void update(int i, int v){ update(i, v, 0, 0, sz); }
	void rangeAdd(int l, int r, int v, int x, int lx, int rx){
		if(r<=lx || rx<=l) return;
		if(l<=lx && rx<=r){
			a[x] += v;
			b[x] += v;
			return;
		}
		int mx = (lx + rx) / 2;
		rangeAdd(l, r, v, 2*x+1, lx, mx);
		rangeAdd(l, r, v, 2*x+2, mx, rx);
		a[x] = b[x] + max(a[2*x+1], a[2*x+2]);
	}
	void rangeAdd(int l, int r, int v){ rangeAdd(l, r+1, v, 0, 0, sz); }
	int query(){ return a[0]; }
};

vector<int> countScans(vector<int> A, vector<int> X, vector<int> V){
	vector<array<int, 2>> p(A.size()+X.size());

	for(int i=0; i<(int)A.size(); ++i) p[i] = {A[i], i};
	for(int i=0; i<(int)X.size(); ++i) p[i+A.size()] = {V[i], X[i]};
	sort(p.begin(), p.end());

	vector<int> seg(p.size(), -INF), pos(A.size()), res(X.size());
	FenwickTree f(p.size());

	for(int i=0; i<(int)A.size(); ++i){
		array<int, 2> search = {A[i], i};
		pos[i] = lower_bound(p.begin(), p.end(), search) - p.begin();
		f.add(pos[i], 1);
	}
	for(int i=0; i<(int)A.size(); ++i) seg[pos[i]] = i - f.get(0, pos[i] - 1);

	SegmentTree st(seg);

	for(int i=0; i<(int)X.size(); ++i){
		array<int, 2> search = {V[i], X[i]};
		int j = lower_bound(p.begin(), p.end(), search) - p.begin();

		if(pos[X[i]] < j) st.rangeAdd(pos[X[i]], j, 1);
		else st.rangeAdd(j, pos[X[i]], -1);

		f.add(pos[X[i]], -1);
		f.add(j, 1);
		st.update(pos[X[i]], st.ID);
		st.update(pos[X[i]] = j, X[i] - f.get(0, j-1));
		
		res[i] = st.query();
	}

	return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 304 KB Output is correct
2 Correct 2 ms 300 KB Output is correct
3 Correct 4 ms 436 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 4 ms 460 KB Output is correct
6 Correct 4 ms 460 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
9 Correct 5 ms 460 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 4 ms 428 KB Output is correct
14 Correct 4 ms 460 KB Output is correct
15 Correct 4 ms 460 KB Output is correct
16 Correct 4 ms 460 KB Output is correct
17 Correct 4 ms 428 KB Output is correct
18 Correct 4 ms 476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 304 KB Output is correct
2 Correct 2 ms 300 KB Output is correct
3 Correct 4 ms 436 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 4 ms 460 KB Output is correct
6 Correct 4 ms 460 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
9 Correct 5 ms 460 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 4 ms 428 KB Output is correct
14 Correct 4 ms 460 KB Output is correct
15 Correct 4 ms 460 KB Output is correct
16 Correct 4 ms 460 KB Output is correct
17 Correct 4 ms 428 KB Output is correct
18 Correct 4 ms 476 KB Output is correct
19 Correct 14 ms 1100 KB Output is correct
20 Correct 15 ms 1216 KB Output is correct
21 Correct 15 ms 1196 KB Output is correct
22 Correct 16 ms 1216 KB Output is correct
23 Correct 15 ms 1100 KB Output is correct
24 Correct 15 ms 1228 KB Output is correct
25 Correct 15 ms 1204 KB Output is correct
26 Correct 15 ms 1100 KB Output is correct
27 Correct 19 ms 1180 KB Output is correct
28 Correct 15 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 1740 KB Output is correct
2 Correct 58 ms 3584 KB Output is correct
3 Correct 110 ms 6084 KB Output is correct
4 Correct 108 ms 6056 KB Output is correct
5 Correct 107 ms 6060 KB Output is correct
6 Correct 110 ms 6032 KB Output is correct
7 Correct 105 ms 5956 KB Output is correct
8 Correct 105 ms 6032 KB Output is correct
9 Correct 108 ms 6048 KB Output is correct
10 Correct 93 ms 6140 KB Output is correct
11 Correct 95 ms 6100 KB Output is correct
12 Correct 94 ms 6144 KB Output is correct
13 Correct 92 ms 6084 KB Output is correct
14 Correct 95 ms 6112 KB Output is correct
15 Correct 104 ms 6124 KB Output is correct
16 Correct 93 ms 6120 KB Output is correct
17 Correct 93 ms 6088 KB Output is correct
18 Correct 93 ms 6120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 304 KB Output is correct
2 Correct 2 ms 300 KB Output is correct
3 Correct 4 ms 436 KB Output is correct
4 Correct 4 ms 460 KB Output is correct
5 Correct 4 ms 460 KB Output is correct
6 Correct 4 ms 460 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 4 ms 460 KB Output is correct
9 Correct 5 ms 460 KB Output is correct
10 Correct 4 ms 460 KB Output is correct
11 Correct 4 ms 460 KB Output is correct
12 Correct 4 ms 460 KB Output is correct
13 Correct 4 ms 428 KB Output is correct
14 Correct 4 ms 460 KB Output is correct
15 Correct 4 ms 460 KB Output is correct
16 Correct 4 ms 460 KB Output is correct
17 Correct 4 ms 428 KB Output is correct
18 Correct 4 ms 476 KB Output is correct
19 Correct 14 ms 1100 KB Output is correct
20 Correct 15 ms 1216 KB Output is correct
21 Correct 15 ms 1196 KB Output is correct
22 Correct 16 ms 1216 KB Output is correct
23 Correct 15 ms 1100 KB Output is correct
24 Correct 15 ms 1228 KB Output is correct
25 Correct 15 ms 1204 KB Output is correct
26 Correct 15 ms 1100 KB Output is correct
27 Correct 19 ms 1180 KB Output is correct
28 Correct 15 ms 1100 KB Output is correct
29 Correct 16 ms 1740 KB Output is correct
30 Correct 58 ms 3584 KB Output is correct
31 Correct 110 ms 6084 KB Output is correct
32 Correct 108 ms 6056 KB Output is correct
33 Correct 107 ms 6060 KB Output is correct
34 Correct 110 ms 6032 KB Output is correct
35 Correct 105 ms 5956 KB Output is correct
36 Correct 105 ms 6032 KB Output is correct
37 Correct 108 ms 6048 KB Output is correct
38 Correct 93 ms 6140 KB Output is correct
39 Correct 95 ms 6100 KB Output is correct
40 Correct 94 ms 6144 KB Output is correct
41 Correct 92 ms 6084 KB Output is correct
42 Correct 95 ms 6112 KB Output is correct
43 Correct 104 ms 6124 KB Output is correct
44 Correct 93 ms 6120 KB Output is correct
45 Correct 93 ms 6088 KB Output is correct
46 Correct 93 ms 6120 KB Output is correct
47 Correct 392 ms 21376 KB Output is correct
48 Correct 1630 ms 57380 KB Output is correct
49 Correct 1765 ms 61056 KB Output is correct
50 Correct 1824 ms 61124 KB Output is correct
51 Correct 1826 ms 60932 KB Output is correct
52 Correct 1832 ms 60936 KB Output is correct
53 Correct 1750 ms 60968 KB Output is correct
54 Correct 1652 ms 61088 KB Output is correct
55 Correct 1720 ms 61176 KB Output is correct
56 Correct 1636 ms 61168 KB Output is correct
57 Correct 1666 ms 61132 KB Output is correct
58 Correct 1779 ms 61084 KB Output is correct
59 Correct 1692 ms 59752 KB Output is correct
60 Correct 1590 ms 59844 KB Output is correct
61 Correct 1614 ms 59756 KB Output is correct
62 Correct 1582 ms 59716 KB Output is correct
63 Correct 1576 ms 59532 KB Output is correct
64 Correct 1563 ms 59616 KB Output is correct
65 Correct 1482 ms 59468 KB Output is correct
66 Correct 1493 ms 59468 KB Output is correct
67 Correct 1458 ms 59524 KB Output is correct