#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2e5 + 7;
int n, q, a[N], l[N], r[N];
vector <int> d[N];
vector <ii> mem[N], meml[N];
void add(int l, int r, int i, int x) {
mem[l].app(mp(i, x));
mem[r + 1].app(mp(i, -x));
}
void addl(int l, int r, int i, int x) {
meml[l].app(mp(i, x));
meml[r + 1].app(mp(i, -x));
}
int link_l[N], link_r[N];
int ans[N];
struct Fen {
int f[N];
void clear() {
for (int i = 0; i < N; ++i) f[i] = 0;
}
void add(int i, int x) {
for (; i < N; i |= i + 1)
f[i] += x;
}
int get(int i) {
int ans = 0;
for (; i >= 0; i &= i + 1, --i) ans += f[i];
return ans;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
} f, fl;
int pref_mx[N], pref_sum[N];
//bigger left, not less right
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> q;
for (int i = 0; i < n; ++i) {
cin >> a[i];
pref_mx[i + 1] = max(pref_mx[i], a[i]);
pref_sum[i + 1] = pref_sum[i] + pref_mx[i + 1];
}
for (int i = 0; i < q; ++i) {
int t;
cin >> t >> l[i] >> r[i];
--l[i]; --r[i];
d[t + 1].app(i);
}
vector <int> s;
for (int i = 0; i < n; ++i) {
while (s.size() && a[s.back()] <= a[i]) {
link_r[s.back()] = i;
s.pop_back();
}
s.app(i);
}
for (int e : s)
link_r[e] = n;
s.clear();
for (int i = n - 1; i >= 0; --i) {
while (s.size() && a[s.back()] < a[i]) {
link_l[s.back()] = i;
s.pop_back();
}
s.app(i);
}
for (int e : s)
link_l[e] = -1;
s.clear();
for (int i = 0; i < n; ++i) {
if (link_r[i] - i < i - link_l[i]) {
for (int j = i; j < link_r[i]; ++j) {
int len1 = j - i + 1;
int len2 = j - link_l[i];
add(len1, len2, j, a[i]);
}
}
else {
for (int j = link_l[i] + 1; j <= i; ++j) {
int len1 = i - j + 1;
int len2 = link_r[i] - j;
if (link_r[i] == n)
len2 = N - 2;
addl(len1, len2, j, a[i]);
}
}
}
f.clear();
fl.clear();
for (int len = 1; len < N; ++len) {
for (auto e : mem[len])
f.add(e.f, e.s);
for (auto e : meml[len]) {
fl.add(e.f, e.s);
}
for (auto e : d[len]) {
ans[e] += f.get(l[e], r[e]);
for (int i = l[e]; i <= r[e]; ++i)
if (i < len - 1)
ans[e] += pref_mx[i + 1];
else
ans[e] += fl.get(i - len + 1, i - len + 1);
/*
if (l[e] + 1 < len) {
int t = min(len, r[e] + 1);
ans[e] += pref_sum[t] - pref_sum[l[e]];
l[e] = t;
}
if (l[e] <= r[e])
ans[e] += fl.get(l[e] - len + 1, r[e] - len + 1);
*/
}
}
for (int i = 0; i < q; ++i)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
16 ms |
17656 KB |
Output is correct |
3 |
Correct |
16 ms |
17656 KB |
Output is correct |
4 |
Correct |
16 ms |
17656 KB |
Output is correct |
5 |
Correct |
17 ms |
17656 KB |
Output is correct |
6 |
Correct |
17 ms |
17656 KB |
Output is correct |
7 |
Correct |
18 ms |
17656 KB |
Output is correct |
8 |
Correct |
16 ms |
17656 KB |
Output is correct |
9 |
Correct |
16 ms |
17656 KB |
Output is correct |
10 |
Correct |
17 ms |
17656 KB |
Output is correct |
11 |
Correct |
16 ms |
17656 KB |
Output is correct |
12 |
Correct |
17 ms |
17656 KB |
Output is correct |
13 |
Correct |
16 ms |
17656 KB |
Output is correct |
14 |
Correct |
16 ms |
17656 KB |
Output is correct |
15 |
Correct |
46 ms |
17660 KB |
Output is correct |
16 |
Correct |
16 ms |
17656 KB |
Output is correct |
17 |
Correct |
16 ms |
17656 KB |
Output is correct |
18 |
Correct |
16 ms |
17656 KB |
Output is correct |
19 |
Correct |
17 ms |
17656 KB |
Output is correct |
20 |
Correct |
16 ms |
17656 KB |
Output is correct |
21 |
Correct |
17 ms |
17656 KB |
Output is correct |
22 |
Correct |
16 ms |
17656 KB |
Output is correct |
23 |
Correct |
16 ms |
17656 KB |
Output is correct |
24 |
Correct |
18 ms |
17704 KB |
Output is correct |
25 |
Correct |
16 ms |
17656 KB |
Output is correct |
26 |
Correct |
17 ms |
17656 KB |
Output is correct |
27 |
Correct |
16 ms |
17656 KB |
Output is correct |
28 |
Correct |
17 ms |
17656 KB |
Output is correct |
29 |
Correct |
17 ms |
17656 KB |
Output is correct |
30 |
Correct |
17 ms |
17656 KB |
Output is correct |
31 |
Correct |
16 ms |
17656 KB |
Output is correct |
32 |
Correct |
17 ms |
17656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Execution timed out |
1101 ms |
81112 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
377 ms |
84532 KB |
Output is correct |
3 |
Correct |
366 ms |
82760 KB |
Output is correct |
4 |
Correct |
387 ms |
87632 KB |
Output is correct |
5 |
Correct |
373 ms |
84300 KB |
Output is correct |
6 |
Correct |
374 ms |
88524 KB |
Output is correct |
7 |
Correct |
382 ms |
89168 KB |
Output is correct |
8 |
Correct |
381 ms |
86040 KB |
Output is correct |
9 |
Correct |
374 ms |
85196 KB |
Output is correct |
10 |
Correct |
384 ms |
90268 KB |
Output is correct |
11 |
Correct |
373 ms |
87236 KB |
Output is correct |
12 |
Correct |
375 ms |
86100 KB |
Output is correct |
13 |
Correct |
374 ms |
86860 KB |
Output is correct |
14 |
Correct |
370 ms |
85200 KB |
Output is correct |
15 |
Correct |
388 ms |
88384 KB |
Output is correct |
16 |
Correct |
369 ms |
86228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1098 ms |
46936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
16 ms |
17656 KB |
Output is correct |
3 |
Correct |
16 ms |
17656 KB |
Output is correct |
4 |
Correct |
16 ms |
17656 KB |
Output is correct |
5 |
Correct |
17 ms |
17656 KB |
Output is correct |
6 |
Correct |
17 ms |
17656 KB |
Output is correct |
7 |
Correct |
18 ms |
17656 KB |
Output is correct |
8 |
Correct |
16 ms |
17656 KB |
Output is correct |
9 |
Correct |
16 ms |
17656 KB |
Output is correct |
10 |
Correct |
17 ms |
17656 KB |
Output is correct |
11 |
Correct |
16 ms |
17656 KB |
Output is correct |
12 |
Correct |
17 ms |
17656 KB |
Output is correct |
13 |
Correct |
16 ms |
17656 KB |
Output is correct |
14 |
Correct |
16 ms |
17656 KB |
Output is correct |
15 |
Correct |
46 ms |
17660 KB |
Output is correct |
16 |
Correct |
16 ms |
17656 KB |
Output is correct |
17 |
Correct |
16 ms |
17656 KB |
Output is correct |
18 |
Correct |
16 ms |
17656 KB |
Output is correct |
19 |
Correct |
17 ms |
17656 KB |
Output is correct |
20 |
Correct |
16 ms |
17656 KB |
Output is correct |
21 |
Correct |
17 ms |
17656 KB |
Output is correct |
22 |
Correct |
16 ms |
17656 KB |
Output is correct |
23 |
Correct |
16 ms |
17656 KB |
Output is correct |
24 |
Correct |
18 ms |
17704 KB |
Output is correct |
25 |
Correct |
16 ms |
17656 KB |
Output is correct |
26 |
Correct |
17 ms |
17656 KB |
Output is correct |
27 |
Correct |
16 ms |
17656 KB |
Output is correct |
28 |
Correct |
17 ms |
17656 KB |
Output is correct |
29 |
Correct |
17 ms |
17656 KB |
Output is correct |
30 |
Correct |
17 ms |
17656 KB |
Output is correct |
31 |
Correct |
16 ms |
17656 KB |
Output is correct |
32 |
Correct |
17 ms |
17656 KB |
Output is correct |
33 |
Execution timed out |
1098 ms |
85528 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |