답안 #502656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
502656 2022-01-06T11:50:16 Z banterbry Pilot (NOI19_pilot) C++17
100 / 100
481 ms 56048 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define fi first
#define si second
#define ar array
typedef pair<int,int> pi;
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg>
void __f(string name, Arg arg) {
	cerr << name << " = " << arg << endl;
}
template <typename Head, typename... Tail>
void __f(string names, Head head, Tail... tail) {
	string cur = "";
	for (auto ch: names){if(ch==','){break;}else{cur+=ch;}}
	string nxt = names.substr(cur.size()+2);
	cerr << cur << " = " << head << ", ";
	__f(nxt, tail...);
}

struct DSU {
    vector<int> e;
    DSU(int N) { e = vector<int>(N, -1); }
    int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }  
    bool same_set(int a, int b) { return get(a) == get(b); }    
    int size(int x) { return -e[get(x)]; }
    bool unite(int x, int y) {  // union by size
        x = get(x), y = get(y);
        if (x == y) return false;
        if (e[x] > e[y]) swap(x, y);
        e[x] += e[y]; e[y] = x;
        return true;
    }
};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int N, Q;
	cin >> N >> Q;
	vector<pi> A(N), qq(Q);
	vector<ll> ans(Q), flag(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i].fi;
		A[i].si = i;
	}
	for (int i = 0; i < Q; ++i) {
		cin >> qq[i].fi;
		qq[i].si = i;
	}
	DSU dsu(N);
	sort(A.begin(), A.end());
	sort(qq.begin(), qq.end());
	ll idx = 0, sum = 0;
	for (auto i: qq) {
		ll ret = 0;
		while (idx < N && A[idx].fi <= i.fi) {
			ll pos = A[idx].si, lx = 0, rx = 0;
			if (pos > 0 && flag[pos - 1]) {
				lx = dsu.size(pos - 1);
				dsu.unite(pos, pos - 1);
			}
			if (pos < N - 1 && flag[pos + 1]) {
				rx = dsu.size(pos + 1);
				dsu.unite(pos, pos + 1);	
			}
			ret += lx + rx + lx * rx + 1;
			flag[pos] = 1;
			++idx;
		}
		ans[i.si] = ret + sum;
		sum += ret;
	}
	for (auto i: ans) cout << i << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 312 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 312 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 308 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 316 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 312 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 308 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 316 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 2712 KB Output is correct
2 Correct 20 ms 2888 KB Output is correct
3 Correct 18 ms 2676 KB Output is correct
4 Correct 17 ms 2616 KB Output is correct
5 Correct 17 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 5640 KB Output is correct
2 Correct 34 ms 5700 KB Output is correct
3 Correct 33 ms 5616 KB Output is correct
4 Correct 32 ms 5808 KB Output is correct
5 Correct 33 ms 5640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5788 KB Output is correct
2 Correct 32 ms 5756 KB Output is correct
3 Correct 36 ms 5792 KB Output is correct
4 Correct 33 ms 5828 KB Output is correct
5 Correct 35 ms 6032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 19 ms 2712 KB Output is correct
12 Correct 20 ms 2888 KB Output is correct
13 Correct 18 ms 2676 KB Output is correct
14 Correct 17 ms 2616 KB Output is correct
15 Correct 17 ms 2636 KB Output is correct
16 Correct 16 ms 2628 KB Output is correct
17 Correct 17 ms 2884 KB Output is correct
18 Correct 16 ms 2892 KB Output is correct
19 Correct 16 ms 2616 KB Output is correct
20 Correct 16 ms 2732 KB Output is correct
21 Correct 18 ms 2620 KB Output is correct
22 Correct 16 ms 2660 KB Output is correct
23 Correct 22 ms 2700 KB Output is correct
24 Correct 16 ms 2632 KB Output is correct
25 Correct 16 ms 2696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 312 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 308 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 316 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 19 ms 2712 KB Output is correct
27 Correct 20 ms 2888 KB Output is correct
28 Correct 18 ms 2676 KB Output is correct
29 Correct 17 ms 2616 KB Output is correct
30 Correct 17 ms 2636 KB Output is correct
31 Correct 35 ms 5640 KB Output is correct
32 Correct 34 ms 5700 KB Output is correct
33 Correct 33 ms 5616 KB Output is correct
34 Correct 32 ms 5808 KB Output is correct
35 Correct 33 ms 5640 KB Output is correct
36 Correct 34 ms 5788 KB Output is correct
37 Correct 32 ms 5756 KB Output is correct
38 Correct 36 ms 5792 KB Output is correct
39 Correct 33 ms 5828 KB Output is correct
40 Correct 35 ms 6032 KB Output is correct
41 Correct 16 ms 2628 KB Output is correct
42 Correct 17 ms 2884 KB Output is correct
43 Correct 16 ms 2892 KB Output is correct
44 Correct 16 ms 2616 KB Output is correct
45 Correct 16 ms 2732 KB Output is correct
46 Correct 18 ms 2620 KB Output is correct
47 Correct 16 ms 2660 KB Output is correct
48 Correct 22 ms 2700 KB Output is correct
49 Correct 16 ms 2632 KB Output is correct
50 Correct 16 ms 2696 KB Output is correct
51 Correct 42 ms 5628 KB Output is correct
52 Correct 40 ms 5572 KB Output is correct
53 Correct 41 ms 5572 KB Output is correct
54 Correct 41 ms 5572 KB Output is correct
55 Correct 41 ms 5620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 312 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 312 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 308 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 316 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 19 ms 2712 KB Output is correct
27 Correct 20 ms 2888 KB Output is correct
28 Correct 18 ms 2676 KB Output is correct
29 Correct 17 ms 2616 KB Output is correct
30 Correct 17 ms 2636 KB Output is correct
31 Correct 35 ms 5640 KB Output is correct
32 Correct 34 ms 5700 KB Output is correct
33 Correct 33 ms 5616 KB Output is correct
34 Correct 32 ms 5808 KB Output is correct
35 Correct 33 ms 5640 KB Output is correct
36 Correct 34 ms 5788 KB Output is correct
37 Correct 32 ms 5756 KB Output is correct
38 Correct 36 ms 5792 KB Output is correct
39 Correct 33 ms 5828 KB Output is correct
40 Correct 35 ms 6032 KB Output is correct
41 Correct 16 ms 2628 KB Output is correct
42 Correct 17 ms 2884 KB Output is correct
43 Correct 16 ms 2892 KB Output is correct
44 Correct 16 ms 2616 KB Output is correct
45 Correct 16 ms 2732 KB Output is correct
46 Correct 18 ms 2620 KB Output is correct
47 Correct 16 ms 2660 KB Output is correct
48 Correct 22 ms 2700 KB Output is correct
49 Correct 16 ms 2632 KB Output is correct
50 Correct 16 ms 2696 KB Output is correct
51 Correct 42 ms 5628 KB Output is correct
52 Correct 40 ms 5572 KB Output is correct
53 Correct 41 ms 5572 KB Output is correct
54 Correct 41 ms 5572 KB Output is correct
55 Correct 41 ms 5620 KB Output is correct
56 Correct 467 ms 54808 KB Output is correct
57 Correct 468 ms 54208 KB Output is correct
58 Correct 441 ms 51400 KB Output is correct
59 Correct 449 ms 52604 KB Output is correct
60 Correct 481 ms 56048 KB Output is correct