Task: | Key cutting |
Sender: | Barren plateau |
Submission time: | 2024-11-16 13:04:27 +0200 |
Language: | C++ (C++17) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.00 s | details |
#2 | ACCEPTED | 0.00 s | details |
#3 | ACCEPTED | 0.00 s | details |
#4 | ACCEPTED | 0.00 s | details |
#5 | ACCEPTED | 0.00 s | details |
#6 | ACCEPTED | 0.00 s | details |
#7 | ACCEPTED | 0.00 s | details |
#8 | ACCEPTED | 0.00 s | details |
#9 | ACCEPTED | 0.04 s | details |
#10 | ACCEPTED | 0.04 s | details |
#11 | ACCEPTED | 0.04 s | details |
#12 | ACCEPTED | 0.04 s | details |
#13 | ACCEPTED | 0.04 s | details |
#14 | ACCEPTED | 0.04 s | details |
Code
#include <bits/stdc++.h> using namespace std; using Z = long long int; Z n; vector<Z> X; vector<pair<Z, Z>> A[20]; pair<Z, Z> q(Z a, Z b) { if(a > b) throw 5; Z len = b - a + 1; Z bit = 63 - __builtin_clzll(len); if(!(2 * (Z{1} << bit) >= len)) throw 5; if(!((Z{1} << bit) <= len)) throw 5; if(a + len - (Z{1} << bit) >= n) throw 5; return min(A[bit][a], A[bit][a + len - (Z{1} << bit)]); }; Z count(Z a, Z b, Z d) { if(a > b) { return 0; } Z ret = 0; auto [val, pos] = q(a, b); if(pos < a) throw 5; if(pos > b) throw 5; if(val < d) throw 5; if(val > d) { ++ret; } ret += count(a, pos - 1, val); ret += count(pos + 1, b, val); return ret; } int main() { cin.sync_with_stdio(false); cin.tie(nullptr); cin >> n; X.resize(n); for(Z i = 0; i < n; ++i) { cin >> X[i]; } A[0].resize(n); for(Z i = 0; i < n; ++i) { A[0][i] = {X[i], i}; } for(Z b = 1; b < 20; ++b) { A[b].resize(n); for(Z i = 0; i < n; ++i) { A[b][i] = A[b - 1][i]; Z j = i + (Z{1} << (b - 1)); if(j < n) { A[b][i] = min(A[b][i], A[b - 1][j]); } } } cout << count(0, n - 1, 0) << "\n"; return 0; }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
3 1 2 1 |
correct output |
---|
2 |
user output |
---|
2 |
Test 2
Verdict: ACCEPTED
input |
---|
1 0 |
correct output |
---|
0 |
user output |
---|
0 |
Test 3
Verdict: ACCEPTED
input |
---|
1 9 |
correct output |
---|
1 |
user output |
---|
1 |
Test 4
Verdict: ACCEPTED
input |
---|
100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 5
Verdict: ACCEPTED
input |
---|
100 0 0 0 1 1 0 0 0 0 1 1 1 0 1 1 ... |
correct output |
---|
25 |
user output |
---|
25 |
Test 6
Verdict: ACCEPTED
input |
---|
100 2 1 2 1 2 0 0 0 1 1 2 2 1 2 2 ... |
correct output |
---|
41 |
user output |
---|
41 |
Test 7
Verdict: ACCEPTED
input |
---|
100 36 5 10 37 94 59 20 31 64 2 58... |
correct output |
---|
99 |
user output |
---|
99 |
Test 8
Verdict: ACCEPTED
input |
---|
100 228768416 32415139 952687252 6... |
correct output |
---|
100 |
user output |
---|
100 |
Test 9
Verdict: ACCEPTED
input |
---|
100000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... |
correct output |
---|
0 |
user output |
---|
0 |
Test 10
Verdict: ACCEPTED
input |
---|
100000 1 1 1 0 0 1 0 0 0 0 1 0 1 0 0 ... |
correct output |
---|
24965 |
user output |
---|
24965 |
Test 11
Verdict: ACCEPTED
input |
---|
100000 2 1 2 2 2 2 2 1 1 0 1 1 0 1 1 ... |
correct output |
---|
38968 |
user output |
---|
38968 |
Test 12
Verdict: ACCEPTED
input |
---|
100000 4 4 5 4 4 5 0 2 2 1 4 4 1 0 5 ... |
correct output |
---|
59156 |
user output |
---|
59156 |
Test 13
Verdict: ACCEPTED
input |
---|
100000 18 5 6 16 8 10 1 7 4 15 5 9 19... |
correct output |
---|
82598 |
user output |
---|
82598 |
Test 14
Verdict: ACCEPTED
input |
---|
100000 33 37 37 86 42 38 18 10 77 57 ... |
correct output |
---|
94897 |
user output |
---|
94897 |