Task: | Kyselyt |
Sender: | jnalanko |
Submission time: | 2020-10-17 01:32:41 +0300 |
Language: | C++ (C++17) |
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.50 s | 2, 3 | details |
#3 | ACCEPTED | 0.53 s | 3 | details |
#4 | ACCEPTED | 0.51 s | 3 | details |
#5 | ACCEPTED | 0.51 s | 3 | details |
Code
#include <iostream> #include <vector> #include <utility> #include <algorithm> #include <cmath> //#include "stdlib_printing.hh" using namespace std; typedef long long LL; struct RMQ{ // Range maximum query LL inf = 1e18; vector<vector<LL> > X; // X[i][j] = argmax v[j..j+2^i) vector<LL> v; RMQ(const vector<LL>& v) : v(v){ LL n = v.size(); for(LL L = 1; L <= n; L *= 2){ vector<LL> R(n); // R[i] = argmax v[i..i+L) if(L == 1){ for(LL i = 0; i < n; i++) R[i] = i; } else{ for(LL i = 0; i < n; i++){ LL a = v[X.back()[i]]; LL b = (i + L/2 < n) ? v[X.back()[i+L/2]] : -inf; if(a > b) R[i] = X.back()[i]; else R[i] = X.back()[i+L/2]; } } X.push_back(R); } } LL get(LL l, LL r){ if(l == r) return l; LL p = log2(r-l+1); LL L = pow(2,p); if(v[X[p][l]] > v[X[p][r-L+1]]) return X[p][l]; else return X[p][r-L+1]; } }; void solve(vector<LL>& v, vector<LL>& S, vector<LL>& C, RMQ& rmq, LL l, LL r){ LL k = rmq.get(l,r); cout << S[l] - S[k] + v[k] * (r-k+1) - (C[r+1] - C[l]) << "\n"; } int main(int argc, char** argv){ LL n,q; cin >> n >> q; vector<LL> v(n); for(LL i = 0; i < n; i++) cin >> v[i]; vector<LL> C(n+1); // C[i] = sum A[0..) for(LL i = 0; i < n; i++) C[i+1] = C[i] + v[i]; RMQ rmq(v); vector<LL> S(n); vector<pair<LL, LL> > suf; // Value, run length LL sum = 0; for(LL i = n-1; i >= 0; i--){ LL deleted = 0; while(suf.size() > 0 && v[i] > suf.back().first){ sum -= suf.back().first * suf.back().second; deleted += suf.back().second; suf.pop_back(); } suf.push_back({v[i], deleted+1}); sum += (deleted+1)*v[i]; S[i] = sum; } while(q--){ LL l,r; cin >> l >> r; l--; r--; solve(v,S,C,rmq,l,r); } }
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 |