#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> ii;
typedef pair<int,ii> bro;
class LE {
private:
vi nx,pr,se;
int n;
public:
LE() {
}
LE(int sz) {
n = sz;
nx.assign(n,0);
pr.assign(n,0);
se.assign(n,1);
for(int i=0;i<n;i++) {
if(i > 0) {
pr[i] = i-1;
}
if(i < n-1) {
nx[i] = i+1;
}
}
nx[n-1] = pr[0] = -1;
}
int nex(int u) {
return nx[u];
}
int pre(int u) {
return pr[u];
}
int rem(int u) {
//cout << "e " << u << '\n';
int res = pr[u];
//cout << pr[u] << " -> " << nx[u] << '\n';
if(pr[u] != -1) {
nx[pr[u]] = nx[u];
}
if(nx[u] != -1) {
pr[nx[u]] = pr[u];
}
nx[u] = pr[u] = -1;
se[u] = 0;
return res;
}
int has(int u) {
return se[u];
}
};
class ST {
private:
vl st;
int n,h;
public:
ST() {
}
ST(int sz) {
n = 1;
h = 1;
while(n < sz) {n<<=1;h++;}
st.assign(2*n,0);
}
void up(int p, ll val) {
p += n;
st[p] += val;
while(p > 1) {
st[p>>1] = st[p]+st[p^1];
p >>= 1;
}
}
ll get(int l, int r) {
ll res = 0;
for(l+=n,r+=n;l<r;l>>=1,r>>=1) {
if(l&1) {
res += st[l];
l++;
}
if(r&1) {
--r;
res += st[r];
}
}
return res;
}
ll kth(ll num) {
ll p = 1;
for(int i=1;i<h;i++) {
p <<= 1;
if(st[p] < num) {
num -= st[p];
p++;
}
}
return p-n;
}
};
LE les,leg;
ST sa,sb;
vl w,fr;
vi act,mo;
vector<ii> so;
bool proc(ii& seg) {
if(seg.second == seg.first) {return false;}
int ra = seg.first, rb = seg.second;
sa.up(ra,1);
sb.up(ra,w[ra]);
fr[ra]++;
sa.up(rb,-1);
sb.up(rb,-w[rb]);
fr[rb]--;
if(fr[rb] == 0) {
seg.second = les.rem(rb);
}
return true;
}
void merge() {
vi nu;
for(int i=0;i<mo.size();i++) {
if(!leg.has(mo[i])) {continue;}
int id = mo[i];
int re = so[id].second;
int reg = leg.nex(id);
while(reg != -1 && w[reg] <= w[re]) {
leg.rem(reg);
so[id].second = so[reg].second;
reg = leg.nex(id);
re = so[id].second;
}
if(so[id].second != so[id].first) {
act.push_back(id);
}
}
mo.clear();
}
void succ() {
vi nu;
for(int i=0;i<act.size();i++) {
int u = act[i];
if(!leg.has(u)) {continue;}
proc(so[u]);
int nt = leg.nex(u);
if(w[so[u].second] >= w[nt]) {
mo.push_back(u);
} else if(so[u].first != so[u].second) {
nu.push_back(u);
}
}
act = nu;
}
ll ksu(int p) {
if(p == 0) {return 0;}
int num = sa.kth(p);
ll res = sb.get(0,num);
ll re = p-sa.get(0,num);
res += w[num]*re;
return res;
}
const int MN = 200200;
vector<bro> qv[MN];
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int n,q;
cin >> n >> q;
les = LE(n);
leg = LE(n);
sa = ST(n);
sb = ST(n);
fr.assign(n,1);
for(int i=0;i<n;i++) {
ll t;
cin >> t;
w.push_back(t);
so.push_back({i,i});
mo.push_back(i);
sa.up(i,1);
sb.up(i,w[i]);
}
for(int i=0;i<q;i++) {
int a,b,c;
cin >> a >> b >> c;
b--;
qv[a].push_back({i,{b,c}});
}
vl res(q,0);
merge();
for(int i=0;i<=n;i++) {
/*
for(int j=0;j<n;j++) {
int u = sa.kth(j+1);
cout << w[u] << " ";
}
cout << '\n';
*/
for(int j=0;j<qv[i].size();j++) {
int idx = qv[i][j].first;
ii co = qv[i][j].second;
res[idx] = ksu(co.second)-ksu(co.first);
}
succ();
merge();
}
for(int i=0;i<q;i++) {
cout << res[i] << '\n';
}
}
Compilation message
ho_t5.cpp: In function 'void merge()':
ho_t5.cpp:122:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<mo.size();i++) {
~^~~~~~~~~~
ho_t5.cpp: In function 'void succ()':
ho_t5.cpp:141:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<act.size();i++) {
~^~~~~~~~~~~
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:198:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<qv[i].size();j++) {
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
8 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
7 ms |
5120 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
7 ms |
5120 KB |
Output is correct |
29 |
Correct |
8 ms |
5120 KB |
Output is correct |
30 |
Correct |
8 ms |
5120 KB |
Output is correct |
31 |
Correct |
7 ms |
5120 KB |
Output is correct |
32 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
473 ms |
34904 KB |
Output is correct |
3 |
Correct |
479 ms |
34756 KB |
Output is correct |
4 |
Correct |
495 ms |
35492 KB |
Output is correct |
5 |
Correct |
481 ms |
34668 KB |
Output is correct |
6 |
Correct |
479 ms |
34596 KB |
Output is correct |
7 |
Correct |
483 ms |
35748 KB |
Output is correct |
8 |
Correct |
501 ms |
34856 KB |
Output is correct |
9 |
Correct |
472 ms |
35632 KB |
Output is correct |
10 |
Correct |
475 ms |
34404 KB |
Output is correct |
11 |
Correct |
505 ms |
35620 KB |
Output is correct |
12 |
Correct |
459 ms |
34468 KB |
Output is correct |
13 |
Correct |
502 ms |
34624 KB |
Output is correct |
14 |
Correct |
492 ms |
34596 KB |
Output is correct |
15 |
Correct |
508 ms |
34724 KB |
Output is correct |
16 |
Correct |
529 ms |
34612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
564 ms |
35764 KB |
Output is correct |
3 |
Correct |
505 ms |
35116 KB |
Output is correct |
4 |
Correct |
546 ms |
35880 KB |
Output is correct |
5 |
Correct |
509 ms |
35108 KB |
Output is correct |
6 |
Correct |
550 ms |
35368 KB |
Output is correct |
7 |
Correct |
539 ms |
35752 KB |
Output is correct |
8 |
Correct |
560 ms |
35308 KB |
Output is correct |
9 |
Correct |
558 ms |
35112 KB |
Output is correct |
10 |
Correct |
549 ms |
34984 KB |
Output is correct |
11 |
Correct |
557 ms |
35752 KB |
Output is correct |
12 |
Correct |
547 ms |
35624 KB |
Output is correct |
13 |
Correct |
554 ms |
35624 KB |
Output is correct |
14 |
Correct |
538 ms |
34956 KB |
Output is correct |
15 |
Correct |
579 ms |
35624 KB |
Output is correct |
16 |
Correct |
606 ms |
35240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
34688 KB |
Output is correct |
2 |
Correct |
369 ms |
32936 KB |
Output is correct |
3 |
Correct |
405 ms |
33320 KB |
Output is correct |
4 |
Correct |
369 ms |
32936 KB |
Output is correct |
5 |
Correct |
384 ms |
32936 KB |
Output is correct |
6 |
Correct |
365 ms |
32936 KB |
Output is correct |
7 |
Correct |
368 ms |
33192 KB |
Output is correct |
8 |
Correct |
375 ms |
33048 KB |
Output is correct |
9 |
Correct |
356 ms |
32936 KB |
Output is correct |
10 |
Correct |
358 ms |
33064 KB |
Output is correct |
11 |
Correct |
379 ms |
33064 KB |
Output is correct |
12 |
Correct |
355 ms |
33112 KB |
Output is correct |
13 |
Correct |
352 ms |
32932 KB |
Output is correct |
14 |
Correct |
355 ms |
33064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
7 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
7 ms |
5120 KB |
Output is correct |
17 |
Correct |
8 ms |
5120 KB |
Output is correct |
18 |
Correct |
7 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
8 ms |
5120 KB |
Output is correct |
23 |
Correct |
8 ms |
5120 KB |
Output is correct |
24 |
Correct |
7 ms |
5120 KB |
Output is correct |
25 |
Correct |
7 ms |
5120 KB |
Output is correct |
26 |
Correct |
8 ms |
5120 KB |
Output is correct |
27 |
Correct |
7 ms |
5120 KB |
Output is correct |
28 |
Correct |
7 ms |
5120 KB |
Output is correct |
29 |
Correct |
8 ms |
5120 KB |
Output is correct |
30 |
Correct |
8 ms |
5120 KB |
Output is correct |
31 |
Correct |
7 ms |
5120 KB |
Output is correct |
32 |
Correct |
8 ms |
5120 KB |
Output is correct |
33 |
Correct |
570 ms |
35884 KB |
Output is correct |
34 |
Correct |
578 ms |
36264 KB |
Output is correct |
35 |
Correct |
617 ms |
36364 KB |
Output is correct |
36 |
Correct |
600 ms |
36008 KB |
Output is correct |
37 |
Correct |
540 ms |
35752 KB |
Output is correct |
38 |
Correct |
557 ms |
35880 KB |
Output is correct |
39 |
Correct |
573 ms |
35752 KB |
Output is correct |
40 |
Correct |
555 ms |
35752 KB |
Output is correct |
41 |
Correct |
571 ms |
36136 KB |
Output is correct |
42 |
Correct |
549 ms |
35880 KB |
Output is correct |
43 |
Correct |
520 ms |
35880 KB |
Output is correct |
44 |
Correct |
486 ms |
35748 KB |
Output is correct |
45 |
Correct |
478 ms |
34984 KB |
Output is correct |
46 |
Correct |
536 ms |
35496 KB |
Output is correct |
47 |
Correct |
511 ms |
34972 KB |
Output is correct |
48 |
Correct |
487 ms |
34984 KB |
Output is correct |
49 |
Correct |
492 ms |
34920 KB |
Output is correct |
50 |
Correct |
495 ms |
35756 KB |
Output is correct |
51 |
Correct |
498 ms |
35776 KB |
Output is correct |
52 |
Correct |
485 ms |
35120 KB |
Output is correct |
53 |
Correct |
517 ms |
35236 KB |
Output is correct |
54 |
Correct |
514 ms |
34724 KB |
Output is correct |
55 |
Correct |
511 ms |
34988 KB |
Output is correct |
56 |
Correct |
562 ms |
34984 KB |
Output is correct |
57 |
Correct |
560 ms |
34856 KB |
Output is correct |
58 |
Correct |
563 ms |
35460 KB |
Output is correct |
59 |
Correct |
473 ms |
34904 KB |
Output is correct |
60 |
Correct |
479 ms |
34756 KB |
Output is correct |
61 |
Correct |
495 ms |
35492 KB |
Output is correct |
62 |
Correct |
481 ms |
34668 KB |
Output is correct |
63 |
Correct |
479 ms |
34596 KB |
Output is correct |
64 |
Correct |
483 ms |
35748 KB |
Output is correct |
65 |
Correct |
501 ms |
34856 KB |
Output is correct |
66 |
Correct |
472 ms |
35632 KB |
Output is correct |
67 |
Correct |
475 ms |
34404 KB |
Output is correct |
68 |
Correct |
505 ms |
35620 KB |
Output is correct |
69 |
Correct |
459 ms |
34468 KB |
Output is correct |
70 |
Correct |
502 ms |
34624 KB |
Output is correct |
71 |
Correct |
492 ms |
34596 KB |
Output is correct |
72 |
Correct |
508 ms |
34724 KB |
Output is correct |
73 |
Correct |
529 ms |
34612 KB |
Output is correct |
74 |
Correct |
564 ms |
35764 KB |
Output is correct |
75 |
Correct |
505 ms |
35116 KB |
Output is correct |
76 |
Correct |
546 ms |
35880 KB |
Output is correct |
77 |
Correct |
509 ms |
35108 KB |
Output is correct |
78 |
Correct |
550 ms |
35368 KB |
Output is correct |
79 |
Correct |
539 ms |
35752 KB |
Output is correct |
80 |
Correct |
560 ms |
35308 KB |
Output is correct |
81 |
Correct |
558 ms |
35112 KB |
Output is correct |
82 |
Correct |
549 ms |
34984 KB |
Output is correct |
83 |
Correct |
557 ms |
35752 KB |
Output is correct |
84 |
Correct |
547 ms |
35624 KB |
Output is correct |
85 |
Correct |
554 ms |
35624 KB |
Output is correct |
86 |
Correct |
538 ms |
34956 KB |
Output is correct |
87 |
Correct |
579 ms |
35624 KB |
Output is correct |
88 |
Correct |
606 ms |
35240 KB |
Output is correct |
89 |
Correct |
374 ms |
34688 KB |
Output is correct |
90 |
Correct |
369 ms |
32936 KB |
Output is correct |
91 |
Correct |
405 ms |
33320 KB |
Output is correct |
92 |
Correct |
369 ms |
32936 KB |
Output is correct |
93 |
Correct |
384 ms |
32936 KB |
Output is correct |
94 |
Correct |
365 ms |
32936 KB |
Output is correct |
95 |
Correct |
368 ms |
33192 KB |
Output is correct |
96 |
Correct |
375 ms |
33048 KB |
Output is correct |
97 |
Correct |
356 ms |
32936 KB |
Output is correct |
98 |
Correct |
358 ms |
33064 KB |
Output is correct |
99 |
Correct |
379 ms |
33064 KB |
Output is correct |
100 |
Correct |
355 ms |
33112 KB |
Output is correct |
101 |
Correct |
352 ms |
32932 KB |
Output is correct |
102 |
Correct |
355 ms |
33064 KB |
Output is correct |