Task: | Kyselyt |
Sender: | Olli |
Submission time: | 2020-10-17 10:16:49 +0300 |
Language: | C++ (C++11) |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 12 |
#2 | ACCEPTED | 34 |
#3 | ACCEPTED | 54 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
#2 | ACCEPTED | 0.38 s | 2, 3 | details |
#3 | ACCEPTED | 0.36 s | 3 | details |
#4 | ACCEPTED | 0.49 s | 3 | details |
#5 | ACCEPTED | 0.34 s | 3 | details |
Code
#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> using namespace std; typedef pair<int, int> pii; typedef long long ll; const int N = 2e5 + 5; const int M = 25; const int INF = 1e9 + 7; ll t[N]; ll ne[N]; ll f[N][M]; ll h[N][M]; ll s[N]; int main() { iostream::sync_with_stdio(false); cin.tie(0); int n, Q; cin >> n >> Q; for(int i = 1; i <= n; ++i) { cin >> t[i]; s[i] = s[i-1] + t[i]; } s[n+1] = s[n]; stack<int> q; ne[n+1] = n+1; t[n+1] = INF; q.push(n+1); for(int i = n; i >= 1; --i) { while(t[i] > t[q.top()]) { q.pop(); } ne[i] = q.top(); q.push(i); } for(int k = 0; k < M; ++k) { for(int i = 1; i <= n+1; ++i) { if(k == 0) { h[i][0] = ne[i]; } else { h[i][k] = h[h[i][k-1]][k-1]; } } } /* for(int k = 0; k <= 3; ++k) { for(int i = 1; i <= n; ++i) { cout << h[i][k] << " "; } cout << "\n"; } return 0; */ for(int k = 0; k < M; ++k) { for(int i = 1; i <= n; ++i) { if(k == 0) { f[i][0] = t[i] * (ne[i] - i - 1) - (s[ne[i] - 1] - s[i]); } else { f[i][k] = f[i][k-1] + f[h[i][k-1]][k-1]; } } } for(int i = 1; i <= Q; ++i) { int a, b; cin >> a >> b; ll ans = 0; for(int k = 24; k >= 0; --k) { if(h[a][k] > b) continue; ans += f[a][k]; a = h[a][k]; } ans += (b - a)*t[a] - (s[b] - s[a]); cout << ans << "\n"; } }
Test details
Test 1
Group: 1, 2, 3
Verdict: ACCEPTED
input |
---|
100 100 70 8 72 88 42 78 85 41 23 36 6... |
correct output |
---|
99 0 922 2579 1892 ... |
user output |
---|
99 0 922 2579 1892 ... Truncated |
Test 2
Group: 2, 3
Verdict: ACCEPTED
input |
---|
200000 200000 98 99 29 92 29 81 100 52 89 80... |
correct output |
---|
1497732 2810356 9532632 6655773 5403513 ... |
user output |
---|
1497732 2810356 9532632 6655773 5403513 ... Truncated |
Test 3
Group: 3
Verdict: ACCEPTED
input |
---|
200000 200000 818377786 934884634 816080381 ... |
correct output |
---|
86877225712611 94684086875470 92703793485296 38149694892093 61948503092286 ... |
user output |
---|
86877225712611 94684086875470 92703793485296 38149694892093 61948503092286 ... Truncated |
Test 4
Group: 3
Verdict: ACCEPTED
input |
---|
200000 200000 1 2 3 4 5 6 7 8 9 10 11 12 13 ... |
correct output |
---|
0 0 0 0 0 ... |
user output |
---|
0 0 0 0 0 ... Truncated |
Test 5
Group: 3
Verdict: ACCEPTED
input |
---|
200000 200000 200000 199999 199998 199997 19... |
correct output |
---|
15920862903 3193483321 18874982071 4846348926 3970697055 ... |
user output |
---|
15920862903 3193483321 18874982071 4846348926 3970697055 ... Truncated |