#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 4e5 + 5;
ll a[mn], lb[mn], rb[mn], small[mn], big[mn], all[mn], ans[mn];
vector<pii> event[mn];
vector<tt> qry[mn];
struct BIT {
vector<ll> tr;
BIT (int sz) : tr(sz + 1) {}
int p (int k) { return k & -k; }
void update (int k, ll val) {
for (; k < tr.size(); k += p(k)) tr[k] += val;
}
ll preSum (int k, ll ans = 0) {
for (; k; k -= p(k)) ans += tr[k];
return ans;
}
int get (int k) { return tr[k]; }
} cntOne(mn), sumOne(mn), freqTwo(mn), sumTwo(mn), cntThree(mn), freqThree(mn), sumThree(mn), sumFreqThree(mn);
int cntGet (int k, int T) {
return cntOne.get(k) * (T + 1) + freqTwo.get(k) + freqThree.get(k) - cntThree.get(k) * T;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
for (int i = n + 1; i <= 2 * n; i++) cin >> a[i];
for (int i = 0; i < q; i++) {
int T, L, R; cin >> T >> L >> R;
qry[T].emplace_back(n + L - 1 - T, i, -1);
qry[T].emplace_back(n + R - T, i, 1);
}
a[0] = a[2 * n + 1] = INT_MAX;
stack<int> st;
st.push(0);
for (int i = 1; i <= 2 * n; i++) {
while (make_pair(a[st.top()], st.top()) <= make_pair(a[i], i)) st.pop();
lb[i] = st.top() + 1; st.push(i);
}
while (st.size()) st.pop(); st.push(2 * n + 1);
for (int i = 2 * n; i >= 1; i--) {
while (make_pair(a[st.top()], st.top()) <= make_pair(a[i], i)) st.pop();
rb[i] = st.top() - 1; st.push(i);
}
for (int i = 1; i <= 2 * n; i++) {
small[i] = i - lb[i] + 1, big[i] = rb[i] - i + 1;
all[i] = rb[i] - lb[i] + 1;
if (small[i] > big[i]) swap(small[i], big[i]);
event[small[i]].emplace_back(i, 1); // switch to small[i]
event[big[i]].emplace_back(i, 2); // switch to all[i] - T
event[all[i]].emplace_back(i, 3); // switch to 0
cntOne.update(i, 1);
sumOne.update(i, a[i]);
}
for (int T = 1; T <= n; T++) {
for (pii it : event[T]) {
int pos, type; tie(pos, type) = it;
if (type == 1) {
cntOne.update(pos, -1);
sumOne.update(pos, -a[pos]);
freqTwo.update(pos, small[pos]);
sumTwo.update(pos, a[pos] * small[pos]);
}
else if (type == 2) {
freqTwo.update(pos, -small[pos]);
sumTwo.update(pos, -a[pos] * small[pos]);
cntThree.update(pos, 1);
freqThree.update(pos, all[pos]);
sumThree.update(pos, a[pos]);
sumFreqThree.update(pos, a[pos] * all[pos]);
}
else if (type == 3) {
cntThree.update(pos, -1);
freqThree.update(pos, -all[pos]);
sumThree.update(pos, -a[pos]);
sumFreqThree.update(pos, -a[pos] * all[pos]);
}
}
for (tt it : qry[T]) {
int P, id, multi; tie(P, id, multi) = it;
int ub = 0, cur = 0;
for (int msk = 1 << 18; msk > 0; msk >>= 1)
if ((ub | msk) <= 2 * n && cur + cntGet(ub | msk, T) <= P)
ub |= msk, cur += cntGet(ub, T);
ll accumulated = sumOne.preSum(ub) * (T + 1) + sumTwo.preSum(ub) + sumFreqThree.preSum(ub) - sumThree.preSum(ub) * T;
if (ub < 2 * n) accumulated += a[ub + 1] * (P - cur);
ans[id] += accumulated * multi;
}
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
return 0;
}
Compilation message
ho_t5.cpp: In member function 'void BIT::update(int, ll)':
ho_t5.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (; k < tr.size(); k += p(k)) tr[k] += val;
| ~~^~~~~~~~~~~
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:62:5: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
62 | while (st.size()) st.pop(); st.push(2 * n + 1);
| ^~~~~
ho_t5.cpp:62:33: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
62 | while (st.size()) st.pop(); st.push(2 * n + 1);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
44376 KB |
Output is correct |
2 |
Correct |
22 ms |
44380 KB |
Output is correct |
3 |
Correct |
21 ms |
44380 KB |
Output is correct |
4 |
Correct |
20 ms |
44308 KB |
Output is correct |
5 |
Correct |
18 ms |
44380 KB |
Output is correct |
6 |
Correct |
20 ms |
44376 KB |
Output is correct |
7 |
Correct |
19 ms |
44380 KB |
Output is correct |
8 |
Correct |
20 ms |
44380 KB |
Output is correct |
9 |
Correct |
19 ms |
44380 KB |
Output is correct |
10 |
Correct |
19 ms |
44380 KB |
Output is correct |
11 |
Correct |
19 ms |
44380 KB |
Output is correct |
12 |
Correct |
18 ms |
44380 KB |
Output is correct |
13 |
Correct |
20 ms |
44380 KB |
Output is correct |
14 |
Correct |
20 ms |
44376 KB |
Output is correct |
15 |
Correct |
19 ms |
44200 KB |
Output is correct |
16 |
Correct |
19 ms |
44344 KB |
Output is correct |
17 |
Correct |
20 ms |
44380 KB |
Output is correct |
18 |
Correct |
26 ms |
44196 KB |
Output is correct |
19 |
Correct |
20 ms |
44376 KB |
Output is correct |
20 |
Correct |
19 ms |
44380 KB |
Output is correct |
21 |
Correct |
20 ms |
44376 KB |
Output is correct |
22 |
Correct |
19 ms |
44380 KB |
Output is correct |
23 |
Correct |
19 ms |
44400 KB |
Output is correct |
24 |
Correct |
20 ms |
44380 KB |
Output is correct |
25 |
Correct |
19 ms |
44632 KB |
Output is correct |
26 |
Correct |
20 ms |
44380 KB |
Output is correct |
27 |
Correct |
21 ms |
44380 KB |
Output is correct |
28 |
Correct |
18 ms |
44228 KB |
Output is correct |
29 |
Correct |
20 ms |
44380 KB |
Output is correct |
30 |
Correct |
20 ms |
44632 KB |
Output is correct |
31 |
Correct |
20 ms |
44380 KB |
Output is correct |
32 |
Correct |
19 ms |
44428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
44376 KB |
Output is correct |
2 |
Correct |
468 ms |
89788 KB |
Output is correct |
3 |
Correct |
462 ms |
89572 KB |
Output is correct |
4 |
Correct |
485 ms |
89784 KB |
Output is correct |
5 |
Correct |
472 ms |
90556 KB |
Output is correct |
6 |
Correct |
477 ms |
89532 KB |
Output is correct |
7 |
Correct |
478 ms |
90204 KB |
Output is correct |
8 |
Correct |
451 ms |
90548 KB |
Output is correct |
9 |
Correct |
446 ms |
90436 KB |
Output is correct |
10 |
Correct |
440 ms |
89400 KB |
Output is correct |
11 |
Correct |
475 ms |
90476 KB |
Output is correct |
12 |
Correct |
510 ms |
89096 KB |
Output is correct |
13 |
Correct |
498 ms |
90040 KB |
Output is correct |
14 |
Correct |
459 ms |
89780 KB |
Output is correct |
15 |
Correct |
448 ms |
90036 KB |
Output is correct |
16 |
Correct |
451 ms |
89528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
44376 KB |
Output is correct |
2 |
Correct |
504 ms |
90276 KB |
Output is correct |
3 |
Correct |
483 ms |
89364 KB |
Output is correct |
4 |
Correct |
486 ms |
91312 KB |
Output is correct |
5 |
Correct |
484 ms |
89788 KB |
Output is correct |
6 |
Correct |
475 ms |
90152 KB |
Output is correct |
7 |
Correct |
481 ms |
90556 KB |
Output is correct |
8 |
Correct |
489 ms |
90180 KB |
Output is correct |
9 |
Correct |
497 ms |
89708 KB |
Output is correct |
10 |
Correct |
468 ms |
89276 KB |
Output is correct |
11 |
Correct |
500 ms |
91320 KB |
Output is correct |
12 |
Correct |
508 ms |
90704 KB |
Output is correct |
13 |
Correct |
515 ms |
90984 KB |
Output is correct |
14 |
Correct |
492 ms |
89784 KB |
Output is correct |
15 |
Correct |
490 ms |
90984 KB |
Output is correct |
16 |
Correct |
479 ms |
90556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
546 ms |
90376 KB |
Output is correct |
2 |
Correct |
562 ms |
90812 KB |
Output is correct |
3 |
Correct |
584 ms |
91776 KB |
Output is correct |
4 |
Correct |
544 ms |
90552 KB |
Output is correct |
5 |
Correct |
554 ms |
90680 KB |
Output is correct |
6 |
Correct |
534 ms |
90808 KB |
Output is correct |
7 |
Correct |
585 ms |
91576 KB |
Output is correct |
8 |
Correct |
573 ms |
91320 KB |
Output is correct |
9 |
Correct |
558 ms |
90812 KB |
Output is correct |
10 |
Correct |
546 ms |
90972 KB |
Output is correct |
11 |
Correct |
579 ms |
90808 KB |
Output is correct |
12 |
Correct |
605 ms |
91068 KB |
Output is correct |
13 |
Correct |
554 ms |
90704 KB |
Output is correct |
14 |
Correct |
578 ms |
90968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
44376 KB |
Output is correct |
2 |
Correct |
22 ms |
44380 KB |
Output is correct |
3 |
Correct |
21 ms |
44380 KB |
Output is correct |
4 |
Correct |
20 ms |
44308 KB |
Output is correct |
5 |
Correct |
18 ms |
44380 KB |
Output is correct |
6 |
Correct |
20 ms |
44376 KB |
Output is correct |
7 |
Correct |
19 ms |
44380 KB |
Output is correct |
8 |
Correct |
20 ms |
44380 KB |
Output is correct |
9 |
Correct |
19 ms |
44380 KB |
Output is correct |
10 |
Correct |
19 ms |
44380 KB |
Output is correct |
11 |
Correct |
19 ms |
44380 KB |
Output is correct |
12 |
Correct |
18 ms |
44380 KB |
Output is correct |
13 |
Correct |
20 ms |
44380 KB |
Output is correct |
14 |
Correct |
20 ms |
44376 KB |
Output is correct |
15 |
Correct |
19 ms |
44200 KB |
Output is correct |
16 |
Correct |
19 ms |
44344 KB |
Output is correct |
17 |
Correct |
20 ms |
44380 KB |
Output is correct |
18 |
Correct |
26 ms |
44196 KB |
Output is correct |
19 |
Correct |
20 ms |
44376 KB |
Output is correct |
20 |
Correct |
19 ms |
44380 KB |
Output is correct |
21 |
Correct |
20 ms |
44376 KB |
Output is correct |
22 |
Correct |
19 ms |
44380 KB |
Output is correct |
23 |
Correct |
19 ms |
44400 KB |
Output is correct |
24 |
Correct |
20 ms |
44380 KB |
Output is correct |
25 |
Correct |
19 ms |
44632 KB |
Output is correct |
26 |
Correct |
20 ms |
44380 KB |
Output is correct |
27 |
Correct |
21 ms |
44380 KB |
Output is correct |
28 |
Correct |
18 ms |
44228 KB |
Output is correct |
29 |
Correct |
20 ms |
44380 KB |
Output is correct |
30 |
Correct |
20 ms |
44632 KB |
Output is correct |
31 |
Correct |
20 ms |
44380 KB |
Output is correct |
32 |
Correct |
19 ms |
44428 KB |
Output is correct |
33 |
Correct |
468 ms |
89788 KB |
Output is correct |
34 |
Correct |
462 ms |
89572 KB |
Output is correct |
35 |
Correct |
485 ms |
89784 KB |
Output is correct |
36 |
Correct |
472 ms |
90556 KB |
Output is correct |
37 |
Correct |
477 ms |
89532 KB |
Output is correct |
38 |
Correct |
478 ms |
90204 KB |
Output is correct |
39 |
Correct |
451 ms |
90548 KB |
Output is correct |
40 |
Correct |
446 ms |
90436 KB |
Output is correct |
41 |
Correct |
440 ms |
89400 KB |
Output is correct |
42 |
Correct |
475 ms |
90476 KB |
Output is correct |
43 |
Correct |
510 ms |
89096 KB |
Output is correct |
44 |
Correct |
498 ms |
90040 KB |
Output is correct |
45 |
Correct |
459 ms |
89780 KB |
Output is correct |
46 |
Correct |
448 ms |
90036 KB |
Output is correct |
47 |
Correct |
451 ms |
89528 KB |
Output is correct |
48 |
Correct |
504 ms |
90276 KB |
Output is correct |
49 |
Correct |
483 ms |
89364 KB |
Output is correct |
50 |
Correct |
486 ms |
91312 KB |
Output is correct |
51 |
Correct |
484 ms |
89788 KB |
Output is correct |
52 |
Correct |
475 ms |
90152 KB |
Output is correct |
53 |
Correct |
481 ms |
90556 KB |
Output is correct |
54 |
Correct |
489 ms |
90180 KB |
Output is correct |
55 |
Correct |
497 ms |
89708 KB |
Output is correct |
56 |
Correct |
468 ms |
89276 KB |
Output is correct |
57 |
Correct |
500 ms |
91320 KB |
Output is correct |
58 |
Correct |
508 ms |
90704 KB |
Output is correct |
59 |
Correct |
515 ms |
90984 KB |
Output is correct |
60 |
Correct |
492 ms |
89784 KB |
Output is correct |
61 |
Correct |
490 ms |
90984 KB |
Output is correct |
62 |
Correct |
479 ms |
90556 KB |
Output is correct |
63 |
Correct |
546 ms |
90376 KB |
Output is correct |
64 |
Correct |
562 ms |
90812 KB |
Output is correct |
65 |
Correct |
584 ms |
91776 KB |
Output is correct |
66 |
Correct |
544 ms |
90552 KB |
Output is correct |
67 |
Correct |
554 ms |
90680 KB |
Output is correct |
68 |
Correct |
534 ms |
90808 KB |
Output is correct |
69 |
Correct |
585 ms |
91576 KB |
Output is correct |
70 |
Correct |
573 ms |
91320 KB |
Output is correct |
71 |
Correct |
558 ms |
90812 KB |
Output is correct |
72 |
Correct |
546 ms |
90972 KB |
Output is correct |
73 |
Correct |
579 ms |
90808 KB |
Output is correct |
74 |
Correct |
605 ms |
91068 KB |
Output is correct |
75 |
Correct |
554 ms |
90704 KB |
Output is correct |
76 |
Correct |
578 ms |
90968 KB |
Output is correct |
77 |
Correct |
493 ms |
91064 KB |
Output is correct |
78 |
Correct |
508 ms |
91580 KB |
Output is correct |
79 |
Correct |
500 ms |
91768 KB |
Output is correct |
80 |
Correct |
482 ms |
91040 KB |
Output is correct |
81 |
Correct |
499 ms |
90616 KB |
Output is correct |
82 |
Correct |
499 ms |
91336 KB |
Output is correct |
83 |
Correct |
495 ms |
91064 KB |
Output is correct |
84 |
Correct |
488 ms |
90820 KB |
Output is correct |
85 |
Correct |
490 ms |
91996 KB |
Output is correct |
86 |
Correct |
498 ms |
90864 KB |
Output is correct |
87 |
Correct |
478 ms |
92600 KB |
Output is correct |
88 |
Correct |
536 ms |
92608 KB |
Output is correct |
89 |
Correct |
489 ms |
90808 KB |
Output is correct |
90 |
Correct |
470 ms |
92348 KB |
Output is correct |
91 |
Correct |
475 ms |
91296 KB |
Output is correct |
92 |
Correct |
490 ms |
90784 KB |
Output is correct |
93 |
Correct |
474 ms |
91436 KB |
Output is correct |
94 |
Correct |
490 ms |
92784 KB |
Output is correct |
95 |
Correct |
480 ms |
92600 KB |
Output is correct |
96 |
Correct |
476 ms |
91576 KB |
Output is correct |
97 |
Correct |
464 ms |
91580 KB |
Output is correct |
98 |
Correct |
508 ms |
90820 KB |
Output is correct |
99 |
Correct |
515 ms |
91080 KB |
Output is correct |
100 |
Correct |
511 ms |
91576 KB |
Output is correct |
101 |
Correct |
513 ms |
90884 KB |
Output is correct |
102 |
Correct |
533 ms |
92132 KB |
Output is correct |