Task: | Kyselyt |
Sender: | tykkipeli |
Submission time: | 2020-10-17 02:51:37 +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.24 s | 2, 3 | details |
#3 | ACCEPTED | 0.25 s | 3 | details |
#4 | ACCEPTED | 0.36 s | 3 | details |
#5 | ACCEPTED | 0.23 s | 3 | details |
Code
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll prefsum[1000000]; ll x[1000000]; int jump[1000000][20]; ll s[1000000][20]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> x[i]; prefsum[1] = x[1]; for (int i = 2; i <= n; i++) prefsum[i] = prefsum[i-1] + x[i]; vector<ll> pino; pino.push_back(1); for (int i = 2; i <= n; i++) { while (pino.size() && x[pino.back()] < x[i]) { jump[pino.back()][0] = i; s[pino.back()][0] = x[pino.back()]*(i-pino.back()) - (prefsum[i-1]-prefsum[pino.back()-1]); pino.pop_back(); } pino.push_back(i); } while (pino.size()) { jump[pino.back()][0] = n+1; pino.pop_back(); } for (int j = 0; j <= 18; j++) jump[n+1][j] = n+1; for (int j = 1; j <= 18; j++) { for (int i = 1; i <= n; i++) { if (jump[i][j-1] == n+1) jump[i][j] = n+1; else jump[i][j] = jump[jump[i][j-1]][j-1]; s[i][j] = s[i][j-1] + s[jump[i][j-1]][j-1]; } } /* cout << endl; for (int i = 0; i <= 5; i++) { for (int j = 1; j <= 10; j++) { cout << jump[j][i] << " "; } cout << endl; } cout << endl; */ for (int i = 0; i < q; i++) { int a,b; cin >> a >> b; ll ans = 0; int ind = a; while (ind <= b) { int pomppu = 0; while (jump[ind][pomppu] <= b) { pomppu++; } if (pomppu == 0) break; ans += s[ind][pomppu-1]; ind = jump[ind][pomppu-1]; } ans += x[ind]*(b-ind) - (prefsum[b] - prefsum[ind]); 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 |