#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Op {
int limit;
int k;
int id;
};
bool operator < (Op a, Op b) {
return a.k < b.k;
}
vector<pair<ll, int>> treemxa;
vector<ll> a;
void buildtreemxa(int v, int tl, int tr) {
if (tl == tr) {
treemxa[v] = {a[tl], tl};
} else {
int tm = (tl + tr) / 2;
buildtreemxa(2 * v, tl, tm);
buildtreemxa(2 * v + 1, tm + 1, tr);
treemxa[v] = max(treemxa[2 * v], treemxa[2 * v + 1]);
}
}
pair<ll, int> getmxa(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl) {
return {-1LL, 0};
}
if (l <= tl && tr <= r) {
return treemxa[v];
}
int tm = (tl + tr) / 2;
return max(getmxa(2 * v, tl, tm, l, r), getmxa(2 * v + 1, tm + 1, tr, l, r));
}
int main() {
#ifndef ONPC
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
int n, q;
cin >> n >> q;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
treemxa.resize(4 * (n + 7));
buildtreemxa(1, 0, n - 1);
// l, r, t
vector<ll> prn(2 * q, 0);
vector<Op> ops;
for (int i = 0; i < q; i++) {
int l, r, t;
cin >> t >> l >> r;
l--, r--;
ops.push_back({l - 1, t, i});
ops.push_back({r, t, i + q});
}
// next bigger
vector<int> s(n, (int) 1e9 + 7), f(n, -((int) 1e9 + 7));
{
vector<int> stk;
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && a[stk.back()] < a[i]) {
stk.pop_back();
}
if (!stk.empty()) {
s[i] = stk.back() - 1;
}
stk.push_back(i);
}
}
{
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && a[stk.back()] <= a[i]) {
stk.pop_back();
}
if (!stk.empty()) {
f[i] = stk.back() + 1;
}
stk.push_back(i);
}
}
sort(ops.begin(), ops.end());
ll h = 0;
for (auto &it : ops) {
int limit = it.limit;
if (limit < 0) {
//cout << "skip\n";
continue;
}
int k = it.k;
int id = it.id;
ll sol = 0;
int cnt = 0;
auto kek = getmxa(1, 0, n - 1, max(0, limit - k), limit);
for (int i = 0; i <= limit; i++) {
// i <= rgh <= s[i]
// f[i] <= rgh - k <= i
// f[i] + k <= rgh <= i + k
int q = max({i, f[i] + k});
int w = min({s[i], i + k});
if (f[i] <= min(limit, s[i]) - k) {
if (w >= limit) { // only exactly once
cnt++;
sol += 1LL * a[i] * (limit - w);
assert(i == kek.second);
}
sol += 1LL * a[i] * (w - q + 1);
}
}
assert(cnt == 1);
prn[id] = sol;
}
for (int i = 0; i < q; i++) {
h = h * 777777 + prn[i];
h = h * 777777 + prn[i + q];
cout << prn[i + q] - prn[i] << "\n";
}
#ifdef ONPC
assert(h == 5799234416310137250);
cout << "h = " << h << "\n";
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1089 ms |
23348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1093 ms |
23336 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1089 ms |
23868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Execution timed out |
1089 ms |
23348 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |