#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#ifdef DEBUG
#define debug(args...) _debug(args)
void _debug(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
#else
#define debug(args...)
#endif
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 400025
#define BUF 200005
struct thing {
int l, r, i;
};
int n, q;
int s[MAXN];
int l[MAXN], r[MAXN];
vector<thing> queries[MAXN], del[MAXN];
ll ans[MAXN];
struct fw {
ll base[MAXN], mult[MAXN];
void nincre(int i, ll x, ll* fw) {
for (; i < MAXN; i += i & -i) fw[i] += x;
}
ll nqsum(int i, ll* fw) {
ll res = 0;
for (; i > 0; i -= i & -i) res += fw[i];
return res;
}
void incre(int i, ll x) {
nincre(i, (1 - i) * x, base);
nincre(i, x, mult);
}
void incre(int s, int e, ll x) {
s += BUF; e += BUF;
incre(s, x);
incre(e + 1, -x);
}
ll qsum(int i) {
return nqsum(i, base) + nqsum(i, mult) * i;
}
ll qsum(int s, int e) {
s += BUF; e += BUF;
return qsum(e) - qsum(s - 1);
}
} par, rect;
void tri(int l, int r, int x, bool todel = 0) {
if (l > r) return;
par.incre(l, n + 1, x);
rect.incre(r + 1, n + 1, x);
if (todel) {
int len = r - l + 1;
if (len <= n) del[len].pb({l, r, -x});
}
}
int main() {
scanf("%d%d", &n, &q);
REP (i, 1, n + 1) {
scanf("%d", &s[i]);
}
REP (i, 0, q) {
int t, l, r; scanf("%d%d%d", &t, &l, &r);
queries[t].pb({l, r, i});
}
vi stk;
REP (i, 1, n + 1) {
while (!stk.empty() && s[stk.back()] <= s[i]) {
stk.pop_back();
}
if (!stk.empty()) {
l[i] = stk.back();
} else {
l[i] = -n;
}
stk.pb(i);
}
stk.clear();
RREP (i, n, 1) {
while (!stk.empty() && s[stk.back()] < s[i]) {
stk.pop_back();
}
if (!stk.empty()) {
r[i] = stk.back();
} else {
r[i] = n + 1;
}
stk.pb(i);
}
REP (i, 1, n + 1) {
tri(l[i] + 1, r[i] - 1, s[i], 1);
tri(l[i] + 1, i - 1, -s[i], 1);
tri(i + 1, r[i] - 1, -s[i], 1);
}
REP (t, 1, n + 1) {
debug("%d\n", t);
#ifdef DEBUG
REP (i, 1, n + 1) {
debug(" %d", par.qsum(i - t, i - t) - rect.qsum(i, i));
}
debug("\n");
#endif
for (thing& o : del[t]) {
auto [l, r, v] = o;
tri(l, r, v);
}
for (thing &o : queries[t]) {
auto [l, r, i] = o;
ans[i] = par.qsum(l - t, r - t) - rect.qsum(l, r);
}
}
REP (i, 0, q) {
printf("%lld\n", ans[i]);
}
return 0;
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:94:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d", &s[i]);
| ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:97:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | int t, l, r; scanf("%d%d%d", &t, &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19132 KB |
Output is correct |
2 |
Correct |
11 ms |
19228 KB |
Output is correct |
3 |
Correct |
11 ms |
19148 KB |
Output is correct |
4 |
Correct |
11 ms |
19148 KB |
Output is correct |
5 |
Correct |
11 ms |
19148 KB |
Output is correct |
6 |
Correct |
11 ms |
19148 KB |
Output is correct |
7 |
Correct |
12 ms |
19152 KB |
Output is correct |
8 |
Correct |
12 ms |
19256 KB |
Output is correct |
9 |
Correct |
13 ms |
19248 KB |
Output is correct |
10 |
Correct |
11 ms |
19148 KB |
Output is correct |
11 |
Correct |
11 ms |
19148 KB |
Output is correct |
12 |
Correct |
13 ms |
19148 KB |
Output is correct |
13 |
Correct |
11 ms |
19148 KB |
Output is correct |
14 |
Correct |
11 ms |
19148 KB |
Output is correct |
15 |
Correct |
11 ms |
19148 KB |
Output is correct |
16 |
Correct |
12 ms |
19148 KB |
Output is correct |
17 |
Correct |
13 ms |
19240 KB |
Output is correct |
18 |
Correct |
11 ms |
19228 KB |
Output is correct |
19 |
Correct |
12 ms |
19160 KB |
Output is correct |
20 |
Correct |
11 ms |
19148 KB |
Output is correct |
21 |
Correct |
11 ms |
19172 KB |
Output is correct |
22 |
Correct |
11 ms |
19224 KB |
Output is correct |
23 |
Correct |
11 ms |
19224 KB |
Output is correct |
24 |
Correct |
13 ms |
19196 KB |
Output is correct |
25 |
Correct |
12 ms |
19148 KB |
Output is correct |
26 |
Correct |
11 ms |
19148 KB |
Output is correct |
27 |
Correct |
12 ms |
19148 KB |
Output is correct |
28 |
Correct |
11 ms |
19220 KB |
Output is correct |
29 |
Correct |
11 ms |
19224 KB |
Output is correct |
30 |
Correct |
11 ms |
19148 KB |
Output is correct |
31 |
Correct |
11 ms |
19228 KB |
Output is correct |
32 |
Correct |
12 ms |
19148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19132 KB |
Output is correct |
2 |
Correct |
269 ms |
41172 KB |
Output is correct |
3 |
Correct |
265 ms |
41068 KB |
Output is correct |
4 |
Correct |
317 ms |
41548 KB |
Output is correct |
5 |
Correct |
266 ms |
42876 KB |
Output is correct |
6 |
Correct |
304 ms |
41300 KB |
Output is correct |
7 |
Correct |
283 ms |
41520 KB |
Output is correct |
8 |
Correct |
255 ms |
44192 KB |
Output is correct |
9 |
Correct |
292 ms |
42688 KB |
Output is correct |
10 |
Correct |
281 ms |
41120 KB |
Output is correct |
11 |
Correct |
263 ms |
42804 KB |
Output is correct |
12 |
Correct |
256 ms |
41500 KB |
Output is correct |
13 |
Correct |
290 ms |
42928 KB |
Output is correct |
14 |
Correct |
279 ms |
42236 KB |
Output is correct |
15 |
Correct |
287 ms |
42240 KB |
Output is correct |
16 |
Correct |
283 ms |
41020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19132 KB |
Output is correct |
2 |
Correct |
333 ms |
42168 KB |
Output is correct |
3 |
Correct |
323 ms |
41760 KB |
Output is correct |
4 |
Correct |
337 ms |
43228 KB |
Output is correct |
5 |
Correct |
347 ms |
41980 KB |
Output is correct |
6 |
Correct |
312 ms |
42156 KB |
Output is correct |
7 |
Correct |
367 ms |
42140 KB |
Output is correct |
8 |
Correct |
330 ms |
42156 KB |
Output is correct |
9 |
Correct |
317 ms |
41904 KB |
Output is correct |
10 |
Correct |
330 ms |
41804 KB |
Output is correct |
11 |
Correct |
316 ms |
43264 KB |
Output is correct |
12 |
Correct |
348 ms |
43116 KB |
Output is correct |
13 |
Correct |
320 ms |
42360 KB |
Output is correct |
14 |
Correct |
303 ms |
42012 KB |
Output is correct |
15 |
Correct |
338 ms |
42288 KB |
Output is correct |
16 |
Correct |
323 ms |
42148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
38632 KB |
Output is correct |
2 |
Correct |
300 ms |
39172 KB |
Output is correct |
3 |
Correct |
296 ms |
39584 KB |
Output is correct |
4 |
Correct |
313 ms |
38704 KB |
Output is correct |
5 |
Correct |
292 ms |
38960 KB |
Output is correct |
6 |
Correct |
328 ms |
38900 KB |
Output is correct |
7 |
Correct |
299 ms |
39184 KB |
Output is correct |
8 |
Correct |
289 ms |
39588 KB |
Output is correct |
9 |
Correct |
286 ms |
38680 KB |
Output is correct |
10 |
Correct |
280 ms |
39224 KB |
Output is correct |
11 |
Correct |
299 ms |
39224 KB |
Output is correct |
12 |
Correct |
321 ms |
38936 KB |
Output is correct |
13 |
Correct |
291 ms |
39096 KB |
Output is correct |
14 |
Correct |
343 ms |
39188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19132 KB |
Output is correct |
2 |
Correct |
11 ms |
19228 KB |
Output is correct |
3 |
Correct |
11 ms |
19148 KB |
Output is correct |
4 |
Correct |
11 ms |
19148 KB |
Output is correct |
5 |
Correct |
11 ms |
19148 KB |
Output is correct |
6 |
Correct |
11 ms |
19148 KB |
Output is correct |
7 |
Correct |
12 ms |
19152 KB |
Output is correct |
8 |
Correct |
12 ms |
19256 KB |
Output is correct |
9 |
Correct |
13 ms |
19248 KB |
Output is correct |
10 |
Correct |
11 ms |
19148 KB |
Output is correct |
11 |
Correct |
11 ms |
19148 KB |
Output is correct |
12 |
Correct |
13 ms |
19148 KB |
Output is correct |
13 |
Correct |
11 ms |
19148 KB |
Output is correct |
14 |
Correct |
11 ms |
19148 KB |
Output is correct |
15 |
Correct |
11 ms |
19148 KB |
Output is correct |
16 |
Correct |
12 ms |
19148 KB |
Output is correct |
17 |
Correct |
13 ms |
19240 KB |
Output is correct |
18 |
Correct |
11 ms |
19228 KB |
Output is correct |
19 |
Correct |
12 ms |
19160 KB |
Output is correct |
20 |
Correct |
11 ms |
19148 KB |
Output is correct |
21 |
Correct |
11 ms |
19172 KB |
Output is correct |
22 |
Correct |
11 ms |
19224 KB |
Output is correct |
23 |
Correct |
11 ms |
19224 KB |
Output is correct |
24 |
Correct |
13 ms |
19196 KB |
Output is correct |
25 |
Correct |
12 ms |
19148 KB |
Output is correct |
26 |
Correct |
11 ms |
19148 KB |
Output is correct |
27 |
Correct |
12 ms |
19148 KB |
Output is correct |
28 |
Correct |
11 ms |
19220 KB |
Output is correct |
29 |
Correct |
11 ms |
19224 KB |
Output is correct |
30 |
Correct |
11 ms |
19148 KB |
Output is correct |
31 |
Correct |
11 ms |
19228 KB |
Output is correct |
32 |
Correct |
12 ms |
19148 KB |
Output is correct |
33 |
Correct |
269 ms |
41172 KB |
Output is correct |
34 |
Correct |
265 ms |
41068 KB |
Output is correct |
35 |
Correct |
317 ms |
41548 KB |
Output is correct |
36 |
Correct |
266 ms |
42876 KB |
Output is correct |
37 |
Correct |
304 ms |
41300 KB |
Output is correct |
38 |
Correct |
283 ms |
41520 KB |
Output is correct |
39 |
Correct |
255 ms |
44192 KB |
Output is correct |
40 |
Correct |
292 ms |
42688 KB |
Output is correct |
41 |
Correct |
281 ms |
41120 KB |
Output is correct |
42 |
Correct |
263 ms |
42804 KB |
Output is correct |
43 |
Correct |
256 ms |
41500 KB |
Output is correct |
44 |
Correct |
290 ms |
42928 KB |
Output is correct |
45 |
Correct |
279 ms |
42236 KB |
Output is correct |
46 |
Correct |
287 ms |
42240 KB |
Output is correct |
47 |
Correct |
283 ms |
41020 KB |
Output is correct |
48 |
Correct |
333 ms |
42168 KB |
Output is correct |
49 |
Correct |
323 ms |
41760 KB |
Output is correct |
50 |
Correct |
337 ms |
43228 KB |
Output is correct |
51 |
Correct |
347 ms |
41980 KB |
Output is correct |
52 |
Correct |
312 ms |
42156 KB |
Output is correct |
53 |
Correct |
367 ms |
42140 KB |
Output is correct |
54 |
Correct |
330 ms |
42156 KB |
Output is correct |
55 |
Correct |
317 ms |
41904 KB |
Output is correct |
56 |
Correct |
330 ms |
41804 KB |
Output is correct |
57 |
Correct |
316 ms |
43264 KB |
Output is correct |
58 |
Correct |
348 ms |
43116 KB |
Output is correct |
59 |
Correct |
320 ms |
42360 KB |
Output is correct |
60 |
Correct |
303 ms |
42012 KB |
Output is correct |
61 |
Correct |
338 ms |
42288 KB |
Output is correct |
62 |
Correct |
323 ms |
42148 KB |
Output is correct |
63 |
Correct |
297 ms |
38632 KB |
Output is correct |
64 |
Correct |
300 ms |
39172 KB |
Output is correct |
65 |
Correct |
296 ms |
39584 KB |
Output is correct |
66 |
Correct |
313 ms |
38704 KB |
Output is correct |
67 |
Correct |
292 ms |
38960 KB |
Output is correct |
68 |
Correct |
328 ms |
38900 KB |
Output is correct |
69 |
Correct |
299 ms |
39184 KB |
Output is correct |
70 |
Correct |
289 ms |
39588 KB |
Output is correct |
71 |
Correct |
286 ms |
38680 KB |
Output is correct |
72 |
Correct |
280 ms |
39224 KB |
Output is correct |
73 |
Correct |
299 ms |
39224 KB |
Output is correct |
74 |
Correct |
321 ms |
38936 KB |
Output is correct |
75 |
Correct |
291 ms |
39096 KB |
Output is correct |
76 |
Correct |
343 ms |
39188 KB |
Output is correct |
77 |
Correct |
358 ms |
42428 KB |
Output is correct |
78 |
Correct |
337 ms |
42820 KB |
Output is correct |
79 |
Correct |
324 ms |
42764 KB |
Output is correct |
80 |
Correct |
347 ms |
43200 KB |
Output is correct |
81 |
Correct |
348 ms |
43060 KB |
Output is correct |
82 |
Correct |
327 ms |
43324 KB |
Output is correct |
83 |
Correct |
327 ms |
43248 KB |
Output is correct |
84 |
Correct |
357 ms |
43224 KB |
Output is correct |
85 |
Correct |
319 ms |
44164 KB |
Output is correct |
86 |
Correct |
351 ms |
43328 KB |
Output is correct |
87 |
Correct |
295 ms |
43188 KB |
Output is correct |
88 |
Correct |
281 ms |
43796 KB |
Output is correct |
89 |
Correct |
290 ms |
42492 KB |
Output is correct |
90 |
Correct |
311 ms |
43608 KB |
Output is correct |
91 |
Correct |
284 ms |
42572 KB |
Output is correct |
92 |
Correct |
287 ms |
42524 KB |
Output is correct |
93 |
Correct |
286 ms |
42784 KB |
Output is correct |
94 |
Correct |
292 ms |
44064 KB |
Output is correct |
95 |
Correct |
286 ms |
43740 KB |
Output is correct |
96 |
Correct |
296 ms |
42800 KB |
Output is correct |
97 |
Correct |
284 ms |
42768 KB |
Output is correct |
98 |
Correct |
286 ms |
42504 KB |
Output is correct |
99 |
Correct |
296 ms |
42648 KB |
Output is correct |
100 |
Correct |
309 ms |
42788 KB |
Output is correct |
101 |
Correct |
309 ms |
42652 KB |
Output is correct |
102 |
Correct |
353 ms |
43824 KB |
Output is correct |