#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define lsb(x) ((x)&(-(x)))
const int INF = INT_MAX;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 200010;
int n, q;
int arr[MAX];
int l[MAX], r[MAX];
int cnt[MAX], sum[MAX];
struct Query{
int l, r, t, id;
};
Query query[MAX];
int res[MAX];
vi ev[MAX];
int owo;
void modify(int id, int val){
owo += val; //
int pos = id;
//if(val == 1) cerr<<"+ ";
//else cerr<<"- ";
//cerr<<id<<endl;
while(pos < n){
cnt[pos] += val;
sum[pos] += arr[id] * val;
pos += lsb(pos);
}
}
int search(int qcnt){
int ret = 0;
int ptr = 0;
for(int j = 19; j >= 0; j--){
int np = ptr + (1<<j);
if(np >= n) continue;
if(qcnt > cnt[np]){
qcnt -= cnt[np];
ret += sum[np];
ptr = np;
}
}
if(ptr + 1 <= n){
ret += qcnt * arr[ptr + 1];
}
return ret;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
FOR(i, 1, n, 1){
cin>>arr[i];
}
FOR(i, 1, q, 1){
cin>>query[i].t>>query[i].l>>query[i].r;
query[i].id = i;
}
sort(query+1, query+q+1, [&](Query a, Query b){
return (a.t < b.t);
});
arr[0] = arr[n+1] = INF;
// l
vi stk;
stk.pb(0);
FOR(i, 1, n, 1){
while(arr[stk.back()] < arr[i]) stk.pop_back();
l[i] = stk.back();
stk.pb(i);
}
stk.clear();
stk.pb(n+1);
for(int i=n; i>=1; i--){
while(arr[stk.back()] <= arr[i]) stk.pop_back();
r[i] = stk.back();
stk.pb(i);
}
// + : [1, r[i] - i - 1]
// - : [i - l[i], i - l[i] + (r[i] - i - 1)]
FOR(i, 1, n, 1){
int ilen = r[i] - i - 1, dt = i - l[i];
if(l[i] == 0){
FOR(j, 1, ilen, 1){
if(j > n) break;
ev[j].pb(i);
}
}
else{
if(ilen < dt){
FOR(j, 1, ilen, 1){
if(j > n) break;
ev[j].pb(i);
}
FOR(j, dt, dt + ilen, 1){
if(j > n) break;
ev[j].pb(-i);
}
}
else{
FOR(j, 1, dt - 1, 1){
if(j > n) break;
ev[j].pb(i);
}
FOR(j, ilen + 1, dt + ilen, 1){
if(j > n) break;
ev[j].pb(-i);
}
}
}
}
// t = 0
FOR(i, 1, n, 1){
modify(i, 1);
}
int ptr = 0;
FOR(i, 1, q, 1){
while(ptr < query[i].t){
// ptr -> ptr+1
ptr++;
for(int e : ev[ptr]){
if(e > 0) modify(e, 1);
else modify(-e, -1);
}
assert(owo == n);
}
res[query[i].id] = search(query[i].r) - search(query[i].l - 1);
//cerr<<"query ["<<query[i].l<<", "<<query[i].r<<"] "<<endl;
}
FOR(i, 1, q, 1){
cout<<res[i]<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
4 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5044 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
5064 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5024 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5020 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5024 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
31 |
Correct |
4 ms |
5024 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
203 ms |
50564 KB |
Output is correct |
3 |
Correct |
216 ms |
53348 KB |
Output is correct |
4 |
Correct |
216 ms |
51284 KB |
Output is correct |
5 |
Correct |
220 ms |
53508 KB |
Output is correct |
6 |
Correct |
230 ms |
51988 KB |
Output is correct |
7 |
Correct |
215 ms |
51588 KB |
Output is correct |
8 |
Correct |
210 ms |
53108 KB |
Output is correct |
9 |
Correct |
198 ms |
52668 KB |
Output is correct |
10 |
Correct |
198 ms |
51164 KB |
Output is correct |
11 |
Correct |
189 ms |
52200 KB |
Output is correct |
12 |
Correct |
200 ms |
51776 KB |
Output is correct |
13 |
Correct |
231 ms |
51500 KB |
Output is correct |
14 |
Correct |
204 ms |
51760 KB |
Output is correct |
15 |
Correct |
217 ms |
52900 KB |
Output is correct |
16 |
Correct |
215 ms |
54692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
220 ms |
52292 KB |
Output is correct |
3 |
Correct |
232 ms |
51856 KB |
Output is correct |
4 |
Correct |
254 ms |
54244 KB |
Output is correct |
5 |
Correct |
220 ms |
49668 KB |
Output is correct |
6 |
Correct |
237 ms |
50288 KB |
Output is correct |
7 |
Correct |
208 ms |
50432 KB |
Output is correct |
8 |
Correct |
216 ms |
51324 KB |
Output is correct |
9 |
Correct |
209 ms |
50104 KB |
Output is correct |
10 |
Correct |
211 ms |
50544 KB |
Output is correct |
11 |
Correct |
213 ms |
53632 KB |
Output is correct |
12 |
Correct |
251 ms |
52204 KB |
Output is correct |
13 |
Correct |
216 ms |
52984 KB |
Output is correct |
14 |
Correct |
204 ms |
48212 KB |
Output is correct |
15 |
Correct |
221 ms |
52124 KB |
Output is correct |
16 |
Correct |
212 ms |
52192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
31040 KB |
Output is correct |
2 |
Correct |
176 ms |
31292 KB |
Output is correct |
3 |
Correct |
185 ms |
31924 KB |
Output is correct |
4 |
Correct |
186 ms |
30944 KB |
Output is correct |
5 |
Correct |
185 ms |
31612 KB |
Output is correct |
6 |
Correct |
190 ms |
31020 KB |
Output is correct |
7 |
Correct |
184 ms |
31736 KB |
Output is correct |
8 |
Correct |
192 ms |
31228 KB |
Output is correct |
9 |
Correct |
191 ms |
31428 KB |
Output is correct |
10 |
Correct |
183 ms |
30932 KB |
Output is correct |
11 |
Correct |
205 ms |
31456 KB |
Output is correct |
12 |
Correct |
186 ms |
31568 KB |
Output is correct |
13 |
Correct |
192 ms |
31296 KB |
Output is correct |
14 |
Correct |
182 ms |
31652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
4 ms |
5020 KB |
Output is correct |
8 |
Correct |
3 ms |
5020 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5044 KB |
Output is correct |
15 |
Correct |
3 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
4 ms |
5024 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
5064 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5024 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5020 KB |
Output is correct |
26 |
Correct |
3 ms |
5028 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5024 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
31 |
Correct |
4 ms |
5024 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
203 ms |
50564 KB |
Output is correct |
34 |
Correct |
216 ms |
53348 KB |
Output is correct |
35 |
Correct |
216 ms |
51284 KB |
Output is correct |
36 |
Correct |
220 ms |
53508 KB |
Output is correct |
37 |
Correct |
230 ms |
51988 KB |
Output is correct |
38 |
Correct |
215 ms |
51588 KB |
Output is correct |
39 |
Correct |
210 ms |
53108 KB |
Output is correct |
40 |
Correct |
198 ms |
52668 KB |
Output is correct |
41 |
Correct |
198 ms |
51164 KB |
Output is correct |
42 |
Correct |
189 ms |
52200 KB |
Output is correct |
43 |
Correct |
200 ms |
51776 KB |
Output is correct |
44 |
Correct |
231 ms |
51500 KB |
Output is correct |
45 |
Correct |
204 ms |
51760 KB |
Output is correct |
46 |
Correct |
217 ms |
52900 KB |
Output is correct |
47 |
Correct |
215 ms |
54692 KB |
Output is correct |
48 |
Correct |
220 ms |
52292 KB |
Output is correct |
49 |
Correct |
232 ms |
51856 KB |
Output is correct |
50 |
Correct |
254 ms |
54244 KB |
Output is correct |
51 |
Correct |
220 ms |
49668 KB |
Output is correct |
52 |
Correct |
237 ms |
50288 KB |
Output is correct |
53 |
Correct |
208 ms |
50432 KB |
Output is correct |
54 |
Correct |
216 ms |
51324 KB |
Output is correct |
55 |
Correct |
209 ms |
50104 KB |
Output is correct |
56 |
Correct |
211 ms |
50544 KB |
Output is correct |
57 |
Correct |
213 ms |
53632 KB |
Output is correct |
58 |
Correct |
251 ms |
52204 KB |
Output is correct |
59 |
Correct |
216 ms |
52984 KB |
Output is correct |
60 |
Correct |
204 ms |
48212 KB |
Output is correct |
61 |
Correct |
221 ms |
52124 KB |
Output is correct |
62 |
Correct |
212 ms |
52192 KB |
Output is correct |
63 |
Correct |
184 ms |
31040 KB |
Output is correct |
64 |
Correct |
176 ms |
31292 KB |
Output is correct |
65 |
Correct |
185 ms |
31924 KB |
Output is correct |
66 |
Correct |
186 ms |
30944 KB |
Output is correct |
67 |
Correct |
185 ms |
31612 KB |
Output is correct |
68 |
Correct |
190 ms |
31020 KB |
Output is correct |
69 |
Correct |
184 ms |
31736 KB |
Output is correct |
70 |
Correct |
192 ms |
31228 KB |
Output is correct |
71 |
Correct |
191 ms |
31428 KB |
Output is correct |
72 |
Correct |
183 ms |
30932 KB |
Output is correct |
73 |
Correct |
205 ms |
31456 KB |
Output is correct |
74 |
Correct |
186 ms |
31568 KB |
Output is correct |
75 |
Correct |
192 ms |
31296 KB |
Output is correct |
76 |
Correct |
182 ms |
31652 KB |
Output is correct |
77 |
Correct |
242 ms |
52220 KB |
Output is correct |
78 |
Correct |
249 ms |
53340 KB |
Output is correct |
79 |
Correct |
218 ms |
53252 KB |
Output is correct |
80 |
Correct |
223 ms |
52268 KB |
Output is correct |
81 |
Correct |
227 ms |
52528 KB |
Output is correct |
82 |
Correct |
221 ms |
52984 KB |
Output is correct |
83 |
Correct |
222 ms |
51756 KB |
Output is correct |
84 |
Correct |
206 ms |
50288 KB |
Output is correct |
85 |
Correct |
213 ms |
51548 KB |
Output is correct |
86 |
Correct |
233 ms |
54436 KB |
Output is correct |
87 |
Correct |
216 ms |
52624 KB |
Output is correct |
88 |
Correct |
200 ms |
50420 KB |
Output is correct |
89 |
Correct |
208 ms |
49980 KB |
Output is correct |
90 |
Correct |
210 ms |
51908 KB |
Output is correct |
91 |
Correct |
214 ms |
52172 KB |
Output is correct |
92 |
Correct |
218 ms |
52724 KB |
Output is correct |
93 |
Correct |
211 ms |
52908 KB |
Output is correct |
94 |
Correct |
212 ms |
52604 KB |
Output is correct |
95 |
Correct |
208 ms |
52388 KB |
Output is correct |
96 |
Correct |
227 ms |
53992 KB |
Output is correct |
97 |
Correct |
258 ms |
52932 KB |
Output is correct |
98 |
Correct |
225 ms |
51852 KB |
Output is correct |
99 |
Correct |
232 ms |
52216 KB |
Output is correct |
100 |
Correct |
272 ms |
54192 KB |
Output is correct |
101 |
Correct |
257 ms |
50788 KB |
Output is correct |
102 |
Correct |
256 ms |
50980 KB |
Output is correct |