#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define task "A"
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e5 + 5;
struct FenwickTree
{
ll a[N];
int n;
FenwickTree()
{
memset(a, 0, sizeof a);
}
void Clear()
{
memset(a, 0, sizeof a);
}
void Update(int p, ll v)
{
for (; p <= n; p += p & -p)
a[p] += v;
}
ll Get(int p)
{
ll ans(0);
for (; p > 0; p -= p & -p)
ans += a[p];
return ans;
}
ll Get(int l, int r)
{
return Get(r) - Get(l - 1);
}
} f;
int n, pre[N], suf[N], q;
ll a[N], ans[N];
vector<pair<pair<int, int>, int>> que[N];
vector<pair<int, ll>> upd[N], upp[N];
void Read()
{
cin >> n >> q;
f.n = n;
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;
que[t].push_back({{l, r}, i});
}
}
void Solve()
{
vector<int> s;
s.reserve(n);
for (int i = 1; i <= n; ++i)
{
while (s.size() && a[s.back()] < a[i])
s.pop_back();
pre[i] = s.empty() ? 0 : s.back();
s.push_back(i);
}
s.clear();
for (int i = n; i; --i)
{
while (s.size() && a[s.back()] <= a[i])
s.pop_back();
suf[i] = s.empty() ? n + 1 : s.back();
s.push_back(i);
}
for (int i = 1; i <= n; ++i)
{
if (pre[i] == 0 || suf[i] - i < i - pre[i])
for (int j = i; j < suf[i]; ++j)
{
upd[j - i].push_back({j, a[i]});
if (pre[i])
upd[j - pre[i]].push_back({j, -a[i]});
}
else
for (int j = pre[i] + 1; j <= i; ++j)
{
upp[i - j].push_back({j, a[i]});
upp[suf[i] - j].push_back({j, -a[i]});
}
}
for (int i = 0; i <= n; ++i)
{
//cout << i << ": ";
for (auto j : upd[i])
f.Update(j.first, j.second);
//for (int i = 1; i <= n; ++i)
// cout << f.Get(i, i) << " ";
//cout << "\n";
for (auto j : que[i])
ans[j.second] += f.Get(j.first.first, j.first.second);
}
f.Clear();
for (int i = 0; i <= n; ++i)
{
for (auto j : upp[i])
f.Update(j.first, j.second);
for (auto j : que[i])
ans[j.second] += f.Get(j.first.first - i, j.first.second - i);
}
for (int i = 1; i <= q; ++i)
cout << ans[i] << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(task ".INP", "r"))
{
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
Read();
Solve();
}
Compilation message
ho_t5.cpp: In function 'int32_t main()':
ho_t5.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
124 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
125 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
12 ms |
16056 KB |
Output is correct |
3 |
Correct |
12 ms |
16108 KB |
Output is correct |
4 |
Correct |
12 ms |
16108 KB |
Output is correct |
5 |
Correct |
11 ms |
16108 KB |
Output is correct |
6 |
Correct |
12 ms |
16108 KB |
Output is correct |
7 |
Correct |
11 ms |
16108 KB |
Output is correct |
8 |
Correct |
12 ms |
16108 KB |
Output is correct |
9 |
Correct |
11 ms |
16108 KB |
Output is correct |
10 |
Correct |
12 ms |
16108 KB |
Output is correct |
11 |
Correct |
11 ms |
16108 KB |
Output is correct |
12 |
Correct |
12 ms |
16108 KB |
Output is correct |
13 |
Correct |
11 ms |
16108 KB |
Output is correct |
14 |
Correct |
12 ms |
16128 KB |
Output is correct |
15 |
Correct |
12 ms |
16108 KB |
Output is correct |
16 |
Correct |
12 ms |
16236 KB |
Output is correct |
17 |
Correct |
12 ms |
16108 KB |
Output is correct |
18 |
Correct |
11 ms |
16108 KB |
Output is correct |
19 |
Correct |
12 ms |
16108 KB |
Output is correct |
20 |
Correct |
12 ms |
16108 KB |
Output is correct |
21 |
Correct |
11 ms |
16108 KB |
Output is correct |
22 |
Correct |
11 ms |
16108 KB |
Output is correct |
23 |
Correct |
12 ms |
16108 KB |
Output is correct |
24 |
Correct |
11 ms |
16108 KB |
Output is correct |
25 |
Correct |
14 ms |
16108 KB |
Output is correct |
26 |
Correct |
11 ms |
16108 KB |
Output is correct |
27 |
Correct |
12 ms |
16108 KB |
Output is correct |
28 |
Correct |
12 ms |
16108 KB |
Output is correct |
29 |
Correct |
11 ms |
16108 KB |
Output is correct |
30 |
Correct |
11 ms |
16108 KB |
Output is correct |
31 |
Correct |
11 ms |
16108 KB |
Output is correct |
32 |
Correct |
11 ms |
16108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
293 ms |
79384 KB |
Output is correct |
3 |
Correct |
329 ms |
84696 KB |
Output is correct |
4 |
Correct |
300 ms |
80984 KB |
Output is correct |
5 |
Correct |
312 ms |
84440 KB |
Output is correct |
6 |
Correct |
310 ms |
82264 KB |
Output is correct |
7 |
Correct |
293 ms |
80088 KB |
Output is correct |
8 |
Correct |
335 ms |
82536 KB |
Output is correct |
9 |
Correct |
304 ms |
82264 KB |
Output is correct |
10 |
Correct |
305 ms |
78936 KB |
Output is correct |
11 |
Correct |
300 ms |
80344 KB |
Output is correct |
12 |
Correct |
293 ms |
80856 KB |
Output is correct |
13 |
Correct |
307 ms |
79832 KB |
Output is correct |
14 |
Correct |
301 ms |
80728 KB |
Output is correct |
15 |
Correct |
314 ms |
80948 KB |
Output is correct |
16 |
Correct |
318 ms |
85440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
368 ms |
83268 KB |
Output is correct |
3 |
Correct |
385 ms |
82372 KB |
Output is correct |
4 |
Correct |
388 ms |
86980 KB |
Output is correct |
5 |
Correct |
361 ms |
77892 KB |
Output is correct |
6 |
Correct |
360 ms |
80324 KB |
Output is correct |
7 |
Correct |
379 ms |
80324 KB |
Output is correct |
8 |
Correct |
366 ms |
79812 KB |
Output is correct |
9 |
Correct |
359 ms |
78660 KB |
Output is correct |
10 |
Correct |
387 ms |
82372 KB |
Output is correct |
11 |
Correct |
378 ms |
84212 KB |
Output is correct |
12 |
Correct |
375 ms |
82632 KB |
Output is correct |
13 |
Correct |
380 ms |
82884 KB |
Output is correct |
14 |
Correct |
376 ms |
76100 KB |
Output is correct |
15 |
Correct |
397 ms |
81732 KB |
Output is correct |
16 |
Correct |
368 ms |
82500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
252 ms |
48836 KB |
Output is correct |
2 |
Correct |
241 ms |
48852 KB |
Output is correct |
3 |
Correct |
249 ms |
49744 KB |
Output is correct |
4 |
Correct |
251 ms |
48836 KB |
Output is correct |
5 |
Correct |
250 ms |
48848 KB |
Output is correct |
6 |
Correct |
235 ms |
49108 KB |
Output is correct |
7 |
Correct |
248 ms |
49604 KB |
Output is correct |
8 |
Correct |
249 ms |
49424 KB |
Output is correct |
9 |
Correct |
250 ms |
48972 KB |
Output is correct |
10 |
Correct |
247 ms |
49348 KB |
Output is correct |
11 |
Correct |
248 ms |
49216 KB |
Output is correct |
12 |
Correct |
249 ms |
49364 KB |
Output is correct |
13 |
Correct |
250 ms |
49092 KB |
Output is correct |
14 |
Correct |
252 ms |
49096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
15980 KB |
Output is correct |
2 |
Correct |
12 ms |
16056 KB |
Output is correct |
3 |
Correct |
12 ms |
16108 KB |
Output is correct |
4 |
Correct |
12 ms |
16108 KB |
Output is correct |
5 |
Correct |
11 ms |
16108 KB |
Output is correct |
6 |
Correct |
12 ms |
16108 KB |
Output is correct |
7 |
Correct |
11 ms |
16108 KB |
Output is correct |
8 |
Correct |
12 ms |
16108 KB |
Output is correct |
9 |
Correct |
11 ms |
16108 KB |
Output is correct |
10 |
Correct |
12 ms |
16108 KB |
Output is correct |
11 |
Correct |
11 ms |
16108 KB |
Output is correct |
12 |
Correct |
12 ms |
16108 KB |
Output is correct |
13 |
Correct |
11 ms |
16108 KB |
Output is correct |
14 |
Correct |
12 ms |
16128 KB |
Output is correct |
15 |
Correct |
12 ms |
16108 KB |
Output is correct |
16 |
Correct |
12 ms |
16236 KB |
Output is correct |
17 |
Correct |
12 ms |
16108 KB |
Output is correct |
18 |
Correct |
11 ms |
16108 KB |
Output is correct |
19 |
Correct |
12 ms |
16108 KB |
Output is correct |
20 |
Correct |
12 ms |
16108 KB |
Output is correct |
21 |
Correct |
11 ms |
16108 KB |
Output is correct |
22 |
Correct |
11 ms |
16108 KB |
Output is correct |
23 |
Correct |
12 ms |
16108 KB |
Output is correct |
24 |
Correct |
11 ms |
16108 KB |
Output is correct |
25 |
Correct |
14 ms |
16108 KB |
Output is correct |
26 |
Correct |
11 ms |
16108 KB |
Output is correct |
27 |
Correct |
12 ms |
16108 KB |
Output is correct |
28 |
Correct |
12 ms |
16108 KB |
Output is correct |
29 |
Correct |
11 ms |
16108 KB |
Output is correct |
30 |
Correct |
11 ms |
16108 KB |
Output is correct |
31 |
Correct |
11 ms |
16108 KB |
Output is correct |
32 |
Correct |
11 ms |
16108 KB |
Output is correct |
33 |
Correct |
293 ms |
79384 KB |
Output is correct |
34 |
Correct |
329 ms |
84696 KB |
Output is correct |
35 |
Correct |
300 ms |
80984 KB |
Output is correct |
36 |
Correct |
312 ms |
84440 KB |
Output is correct |
37 |
Correct |
310 ms |
82264 KB |
Output is correct |
38 |
Correct |
293 ms |
80088 KB |
Output is correct |
39 |
Correct |
335 ms |
82536 KB |
Output is correct |
40 |
Correct |
304 ms |
82264 KB |
Output is correct |
41 |
Correct |
305 ms |
78936 KB |
Output is correct |
42 |
Correct |
300 ms |
80344 KB |
Output is correct |
43 |
Correct |
293 ms |
80856 KB |
Output is correct |
44 |
Correct |
307 ms |
79832 KB |
Output is correct |
45 |
Correct |
301 ms |
80728 KB |
Output is correct |
46 |
Correct |
314 ms |
80948 KB |
Output is correct |
47 |
Correct |
318 ms |
85440 KB |
Output is correct |
48 |
Correct |
368 ms |
83268 KB |
Output is correct |
49 |
Correct |
385 ms |
82372 KB |
Output is correct |
50 |
Correct |
388 ms |
86980 KB |
Output is correct |
51 |
Correct |
361 ms |
77892 KB |
Output is correct |
52 |
Correct |
360 ms |
80324 KB |
Output is correct |
53 |
Correct |
379 ms |
80324 KB |
Output is correct |
54 |
Correct |
366 ms |
79812 KB |
Output is correct |
55 |
Correct |
359 ms |
78660 KB |
Output is correct |
56 |
Correct |
387 ms |
82372 KB |
Output is correct |
57 |
Correct |
378 ms |
84212 KB |
Output is correct |
58 |
Correct |
375 ms |
82632 KB |
Output is correct |
59 |
Correct |
380 ms |
82884 KB |
Output is correct |
60 |
Correct |
376 ms |
76100 KB |
Output is correct |
61 |
Correct |
397 ms |
81732 KB |
Output is correct |
62 |
Correct |
368 ms |
82500 KB |
Output is correct |
63 |
Correct |
252 ms |
48836 KB |
Output is correct |
64 |
Correct |
241 ms |
48852 KB |
Output is correct |
65 |
Correct |
249 ms |
49744 KB |
Output is correct |
66 |
Correct |
251 ms |
48836 KB |
Output is correct |
67 |
Correct |
250 ms |
48848 KB |
Output is correct |
68 |
Correct |
235 ms |
49108 KB |
Output is correct |
69 |
Correct |
248 ms |
49604 KB |
Output is correct |
70 |
Correct |
249 ms |
49424 KB |
Output is correct |
71 |
Correct |
250 ms |
48972 KB |
Output is correct |
72 |
Correct |
247 ms |
49348 KB |
Output is correct |
73 |
Correct |
248 ms |
49216 KB |
Output is correct |
74 |
Correct |
249 ms |
49364 KB |
Output is correct |
75 |
Correct |
250 ms |
49092 KB |
Output is correct |
76 |
Correct |
252 ms |
49096 KB |
Output is correct |
77 |
Correct |
382 ms |
83524 KB |
Output is correct |
78 |
Correct |
394 ms |
86380 KB |
Output is correct |
79 |
Correct |
415 ms |
85188 KB |
Output is correct |
80 |
Correct |
397 ms |
84292 KB |
Output is correct |
81 |
Correct |
401 ms |
84420 KB |
Output is correct |
82 |
Correct |
379 ms |
84676 KB |
Output is correct |
83 |
Correct |
389 ms |
83012 KB |
Output is correct |
84 |
Correct |
363 ms |
80336 KB |
Output is correct |
85 |
Correct |
397 ms |
83268 KB |
Output is correct |
86 |
Correct |
409 ms |
89172 KB |
Output is correct |
87 |
Correct |
343 ms |
84532 KB |
Output is correct |
88 |
Correct |
313 ms |
78788 KB |
Output is correct |
89 |
Correct |
328 ms |
78020 KB |
Output is correct |
90 |
Correct |
330 ms |
83140 KB |
Output is correct |
91 |
Correct |
385 ms |
83524 KB |
Output is correct |
92 |
Correct |
334 ms |
84328 KB |
Output is correct |
93 |
Correct |
324 ms |
85060 KB |
Output is correct |
94 |
Correct |
352 ms |
83268 KB |
Output is correct |
95 |
Correct |
328 ms |
83652 KB |
Output is correct |
96 |
Correct |
349 ms |
86724 KB |
Output is correct |
97 |
Correct |
335 ms |
84036 KB |
Output is correct |
98 |
Correct |
336 ms |
81864 KB |
Output is correct |
99 |
Correct |
336 ms |
83652 KB |
Output is correct |
100 |
Correct |
377 ms |
85648 KB |
Output is correct |
101 |
Correct |
338 ms |
81220 KB |
Output is correct |
102 |
Correct |
357 ms |
81048 KB |
Output is correct |