#include <bits/stdc++.h>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
#define int long long
const int N = (int) 2e5 + 5;
int n, q;
int a[N], res[N];
vector<pair<int, int>> Q[N];
struct fenwick {
int n;
vector<int> s;
fenwick(int n): n(n) {
s.resize(n);
}
void upd(int i, int x) {
for (; i <= n; i += i & -i) {
s[i - 1] += x;
}
}
int get(int i) {
int res = 0;
for (; i; i -= i & -i) {
res += s[i - 1];
}
return res;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= q; ++i) {
int t, l, r;
cin >> t >> l >> r;
Q[r].emplace_back(t, i);
Q[l - 1].emplace_back(t, -i);
}
fenwick ft1(n + 1), ft2(n + 1);
auto upd = [&](int i, int v) {
i++;
ft1.upd(i, v);
ft2.upd(i, -v * (i - 1));
};
auto get = [&](int i) {
i++;
return ft1.get(i) * i + ft2.get(i);
};
vector<int> st, pref;
st.emplace_back(-n);
pref.emplace_back(0);
for (int i = 1; i <= n; ++i) {
while (st.size() && st.back() > 0 && a[st.back()] <= a[i]) {
upd(i - st.back(), -a[st.back()]);
upd(i - st.end()[-2], a[st.back()]);
st.pop_back();
pref.pop_back();
}
upd(0, a[i]);
upd(i - st.back(), -a[i]);
pref.emplace_back(pref.back() + (i - st.back()) * a[i]);
st.emplace_back(i);
for (auto [t, id] : Q[i]) {
int val = get(t);
int p = lower_bound(st.begin(), st.end(), i - t) - st.begin();
val -= (st[p] + t - i) * a[st[p]] + pref.back() - pref[p];
if (id > 0) {
res[id] += val;
} else {
res[-id] -= val;
}
}
}
for (int i = 1; i <= q; ++i) {
cout << res[i] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6744 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6756 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6792 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6812 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6612 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
241 ms |
29728 KB |
Output is correct |
3 |
Correct |
244 ms |
29780 KB |
Output is correct |
4 |
Correct |
249 ms |
29776 KB |
Output is correct |
5 |
Correct |
261 ms |
29704 KB |
Output is correct |
6 |
Correct |
230 ms |
29264 KB |
Output is correct |
7 |
Correct |
266 ms |
30152 KB |
Output is correct |
8 |
Correct |
240 ms |
29776 KB |
Output is correct |
9 |
Correct |
242 ms |
30032 KB |
Output is correct |
10 |
Correct |
226 ms |
29776 KB |
Output is correct |
11 |
Correct |
248 ms |
30292 KB |
Output is correct |
12 |
Correct |
240 ms |
29524 KB |
Output is correct |
13 |
Correct |
233 ms |
29580 KB |
Output is correct |
14 |
Correct |
254 ms |
29448 KB |
Output is correct |
15 |
Correct |
239 ms |
29524 KB |
Output is correct |
16 |
Correct |
228 ms |
29576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
243 ms |
29124 KB |
Output is correct |
3 |
Correct |
254 ms |
28644 KB |
Output is correct |
4 |
Correct |
233 ms |
29264 KB |
Output is correct |
5 |
Correct |
244 ms |
28752 KB |
Output is correct |
6 |
Correct |
236 ms |
28788 KB |
Output is correct |
7 |
Correct |
246 ms |
28752 KB |
Output is correct |
8 |
Correct |
244 ms |
29264 KB |
Output is correct |
9 |
Correct |
238 ms |
28752 KB |
Output is correct |
10 |
Correct |
230 ms |
28496 KB |
Output is correct |
11 |
Correct |
237 ms |
29264 KB |
Output is correct |
12 |
Correct |
228 ms |
28680 KB |
Output is correct |
13 |
Correct |
243 ms |
29488 KB |
Output is correct |
14 |
Correct |
245 ms |
28748 KB |
Output is correct |
15 |
Correct |
239 ms |
29088 KB |
Output is correct |
16 |
Correct |
227 ms |
28588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
26192 KB |
Output is correct |
2 |
Correct |
185 ms |
26192 KB |
Output is correct |
3 |
Correct |
203 ms |
26708 KB |
Output is correct |
4 |
Correct |
211 ms |
26704 KB |
Output is correct |
5 |
Correct |
199 ms |
26704 KB |
Output is correct |
6 |
Correct |
197 ms |
26072 KB |
Output is correct |
7 |
Correct |
215 ms |
26400 KB |
Output is correct |
8 |
Correct |
201 ms |
26524 KB |
Output is correct |
9 |
Correct |
195 ms |
26500 KB |
Output is correct |
10 |
Correct |
183 ms |
26436 KB |
Output is correct |
11 |
Correct |
196 ms |
26700 KB |
Output is correct |
12 |
Correct |
201 ms |
26632 KB |
Output is correct |
13 |
Correct |
208 ms |
26516 KB |
Output is correct |
14 |
Correct |
196 ms |
26732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6744 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6756 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6792 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
1 ms |
6812 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
1 ms |
6612 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
241 ms |
29728 KB |
Output is correct |
34 |
Correct |
244 ms |
29780 KB |
Output is correct |
35 |
Correct |
249 ms |
29776 KB |
Output is correct |
36 |
Correct |
261 ms |
29704 KB |
Output is correct |
37 |
Correct |
230 ms |
29264 KB |
Output is correct |
38 |
Correct |
266 ms |
30152 KB |
Output is correct |
39 |
Correct |
240 ms |
29776 KB |
Output is correct |
40 |
Correct |
242 ms |
30032 KB |
Output is correct |
41 |
Correct |
226 ms |
29776 KB |
Output is correct |
42 |
Correct |
248 ms |
30292 KB |
Output is correct |
43 |
Correct |
240 ms |
29524 KB |
Output is correct |
44 |
Correct |
233 ms |
29580 KB |
Output is correct |
45 |
Correct |
254 ms |
29448 KB |
Output is correct |
46 |
Correct |
239 ms |
29524 KB |
Output is correct |
47 |
Correct |
228 ms |
29576 KB |
Output is correct |
48 |
Correct |
243 ms |
29124 KB |
Output is correct |
49 |
Correct |
254 ms |
28644 KB |
Output is correct |
50 |
Correct |
233 ms |
29264 KB |
Output is correct |
51 |
Correct |
244 ms |
28752 KB |
Output is correct |
52 |
Correct |
236 ms |
28788 KB |
Output is correct |
53 |
Correct |
246 ms |
28752 KB |
Output is correct |
54 |
Correct |
244 ms |
29264 KB |
Output is correct |
55 |
Correct |
238 ms |
28752 KB |
Output is correct |
56 |
Correct |
230 ms |
28496 KB |
Output is correct |
57 |
Correct |
237 ms |
29264 KB |
Output is correct |
58 |
Correct |
228 ms |
28680 KB |
Output is correct |
59 |
Correct |
243 ms |
29488 KB |
Output is correct |
60 |
Correct |
245 ms |
28748 KB |
Output is correct |
61 |
Correct |
239 ms |
29088 KB |
Output is correct |
62 |
Correct |
227 ms |
28588 KB |
Output is correct |
63 |
Correct |
198 ms |
26192 KB |
Output is correct |
64 |
Correct |
185 ms |
26192 KB |
Output is correct |
65 |
Correct |
203 ms |
26708 KB |
Output is correct |
66 |
Correct |
211 ms |
26704 KB |
Output is correct |
67 |
Correct |
199 ms |
26704 KB |
Output is correct |
68 |
Correct |
197 ms |
26072 KB |
Output is correct |
69 |
Correct |
215 ms |
26400 KB |
Output is correct |
70 |
Correct |
201 ms |
26524 KB |
Output is correct |
71 |
Correct |
195 ms |
26500 KB |
Output is correct |
72 |
Correct |
183 ms |
26436 KB |
Output is correct |
73 |
Correct |
196 ms |
26700 KB |
Output is correct |
74 |
Correct |
201 ms |
26632 KB |
Output is correct |
75 |
Correct |
208 ms |
26516 KB |
Output is correct |
76 |
Correct |
196 ms |
26732 KB |
Output is correct |
77 |
Correct |
241 ms |
29744 KB |
Output is correct |
78 |
Correct |
230 ms |
30224 KB |
Output is correct |
79 |
Correct |
249 ms |
29660 KB |
Output is correct |
80 |
Correct |
248 ms |
29800 KB |
Output is correct |
81 |
Correct |
235 ms |
29592 KB |
Output is correct |
82 |
Correct |
249 ms |
29704 KB |
Output is correct |
83 |
Correct |
240 ms |
29520 KB |
Output is correct |
84 |
Correct |
244 ms |
29724 KB |
Output is correct |
85 |
Correct |
247 ms |
29572 KB |
Output is correct |
86 |
Correct |
249 ms |
30036 KB |
Output is correct |
87 |
Correct |
252 ms |
30168 KB |
Output is correct |
88 |
Correct |
247 ms |
30088 KB |
Output is correct |
89 |
Correct |
230 ms |
29660 KB |
Output is correct |
90 |
Correct |
240 ms |
29828 KB |
Output is correct |
91 |
Correct |
228 ms |
29524 KB |
Output is correct |
92 |
Correct |
230 ms |
29520 KB |
Output is correct |
93 |
Correct |
241 ms |
29640 KB |
Output is correct |
94 |
Correct |
257 ms |
30248 KB |
Output is correct |
95 |
Correct |
249 ms |
30036 KB |
Output is correct |
96 |
Correct |
247 ms |
29776 KB |
Output is correct |
97 |
Correct |
271 ms |
30132 KB |
Output is correct |
98 |
Correct |
224 ms |
29264 KB |
Output is correct |
99 |
Correct |
258 ms |
29272 KB |
Output is correct |
100 |
Correct |
239 ms |
29568 KB |
Output is correct |
101 |
Correct |
240 ms |
29348 KB |
Output is correct |
102 |
Correct |
235 ms |
29916 KB |
Output is correct |