#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
void strat1(int n,vector<int> arr,int q){
vector<pair<int,int>> connection(n+1);
vector<int> left(n+1),right(n+1);
vector<long long> pref(n+2);
vector<pair<int,int>> adj(n+1);
auto solve = [&](int l,int r){
pref[l-1]=0;
for(int i=l;i<=r;i++){
pref[i]=pref[i-1]+arr[i];
adj[i].first=adj[i].second=0;
}
stack<pair<int,int>> s;
s.emplace(INF,l-1);
for(int i=l;i<=r;i++){
while(s.top().first<=arr[i])s.pop();
connection[i]=s.top();
left[i]=s.top().second;
s.emplace(arr[i],i);
}
while(!s.empty())s.pop();
s.emplace(INF,r+1);
for(int i=r;i>=l;i--){
while(s.top().first<arr[i])s.pop();
connection[i]=min(s.top(),connection[i]);
right[i]=s.top().second;
s.emplace(arr[i],i);
}
for(int i=l;i<=r;i++){
if(connection[i].second<i)adj[connection[i].second].second=i;
else adj[connection[i].second].first=i;
}
int ans = 0;
function<void(int,int)> dfs = [&](int x,int threshold){
if(pref[right[x]-1]-pref[left[x]]<threshold)return;
ans++;
if(adj[x].first)dfs(adj[x].first,arr[x]);
if(adj[x].second)dfs(adj[x].second,arr[x]);
};
dfs(adj[l-1].second,0);
return ans;
};
for(int i=1;i<=q;i++){
int type;cin>>type;
if(type==1){
int x,y;cin>>x>>y;
arr[x]=y;
} else {
int l,r;cin>>l>>r;
cout << solve(l,r) << '\n';
}
}
exit(0);
}
struct node{
int ele,cnt;
node(int x=INT64_MIN):ele(x),cnt(1){}
node(const node &a,const node &b):ele(max(a.ele,b.ele)),cnt(0){
if(a.ele==ele)cnt+=a.cnt;
if(b.ele==ele)cnt+=b.cnt;
}
};
struct segtree{
vector<node> tree;
segtree():tree(262144){}
void update(int k,int x){
k+=131072;
tree[k]=x;
while(k/=2){
tree[k]={tree[2*k],tree[2*k+1]};
}
}
pair<int,int> get(int l,int r){
node ans;l+=131072;r+=131072;
while(l<=r){
if(l&1)ans={ans,tree[l++]};
if(r%2 == 0)ans={ans,tree[r--]};
l/=2;r/=2;
}
return {ans.ele,ans.cnt};
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<long long> arr(n+1);
for(int i=1;i<=n;i++)cin>>arr[i];
int q;
cin >> q;
if(q<=1000)strat1(n,arr,q);
arr.emplace_back(0);
arr[0]=arr[n+1]=INF;
segtree cntranges;
segtree sumtree;
segtree sumtree2;
vector<int> sums(n+2);
vector<int> ranges(n+2);
sums[0]=arr[0];
for(int i=1;i<=n+1;i++)sums[i]=sums[i-1]+arr[i];
{
stack<pair<int,int>> s;
s.emplace(arr[0],0);
for(int i=1;i<=n;i++){
while(s.top().first<arr[i])s.pop();
if(sums[i-1]-sums[s.top().second] < arr[i]){
ranges[s.top().second+1]++;
ranges[i]--;
}
s.emplace(arr[i],i);
}
}
{
stack<pair<int,int>> s;
s.emplace(arr[n+1],n+1);
for(int i=n;i;i--){
while(s.top().first<=arr[i])s.pop();
if(sums[s.top().second-1]-sums[i] < arr[i]){
ranges[i+1]++;
ranges[s.top().second]--;
}
s.emplace(arr[i],i);
}
}
for(int i=1;i<=n+1;i++)ranges[i]+=ranges[i-1];
for(int i=1;i<=n;i++){
cntranges.update(i,-ranges[i]);
sumtree.update(i,arr[i]-sums[i-1]);
sumtree2.update(i,arr[i]+sums[i]);
}
for(int i=1;i<=q;i++){
int type;cin>>type;
int l,r;cin>>l>>r;
int ld = r;
int rd = l;
{
// transform query
for(int jump=65536;jump;jump/=2){
if(ld-jump<l)continue;
if(sumtree.get(ld-jump+1,r).first<=-sums[l-1])ld-=jump;
}
for(int jump=65536;jump;jump/=2){
if(rd+jump>r)continue;
if(sumtree2.get(l,rd+jump-1).first<=sums[r])rd+=jump;
}
}
assert(ld<=rd);
cout << cntranges.get(ld,rd).second << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
7396 KB |
Output is correct |
3 |
Correct |
9 ms |
7516 KB |
Output is correct |
4 |
Correct |
8 ms |
7516 KB |
Output is correct |
5 |
Correct |
7 ms |
7516 KB |
Output is correct |
6 |
Correct |
12 ms |
7516 KB |
Output is correct |
7 |
Correct |
8 ms |
7516 KB |
Output is correct |
8 |
Correct |
13 ms |
7532 KB |
Output is correct |
9 |
Correct |
8 ms |
7516 KB |
Output is correct |
10 |
Correct |
8 ms |
7392 KB |
Output is correct |
11 |
Correct |
8 ms |
7516 KB |
Output is correct |
12 |
Correct |
8 ms |
7564 KB |
Output is correct |
13 |
Correct |
9 ms |
7524 KB |
Output is correct |
14 |
Correct |
9 ms |
7772 KB |
Output is correct |
15 |
Correct |
11 ms |
7832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
8 ms |
7396 KB |
Output is correct |
23 |
Correct |
9 ms |
7516 KB |
Output is correct |
24 |
Correct |
8 ms |
7516 KB |
Output is correct |
25 |
Correct |
7 ms |
7516 KB |
Output is correct |
26 |
Correct |
12 ms |
7516 KB |
Output is correct |
27 |
Correct |
8 ms |
7516 KB |
Output is correct |
28 |
Correct |
13 ms |
7532 KB |
Output is correct |
29 |
Correct |
8 ms |
7516 KB |
Output is correct |
30 |
Correct |
8 ms |
7392 KB |
Output is correct |
31 |
Correct |
8 ms |
7516 KB |
Output is correct |
32 |
Correct |
8 ms |
7564 KB |
Output is correct |
33 |
Correct |
9 ms |
7524 KB |
Output is correct |
34 |
Correct |
9 ms |
7772 KB |
Output is correct |
35 |
Correct |
11 ms |
7832 KB |
Output is correct |
36 |
Correct |
212 ms |
8532 KB |
Output is correct |
37 |
Correct |
342 ms |
7768 KB |
Output is correct |
38 |
Correct |
442 ms |
7768 KB |
Output is correct |
39 |
Correct |
100 ms |
8024 KB |
Output is correct |
40 |
Correct |
462 ms |
7864 KB |
Output is correct |
41 |
Correct |
844 ms |
8280 KB |
Output is correct |
42 |
Correct |
1034 ms |
8280 KB |
Output is correct |
43 |
Correct |
581 ms |
7732 KB |
Output is correct |
44 |
Correct |
665 ms |
7720 KB |
Output is correct |
45 |
Correct |
254 ms |
8028 KB |
Output is correct |
46 |
Correct |
349 ms |
8104 KB |
Output is correct |
47 |
Correct |
489 ms |
7800 KB |
Output is correct |
48 |
Correct |
224 ms |
7824 KB |
Output is correct |
49 |
Correct |
763 ms |
7880 KB |
Output is correct |
50 |
Correct |
446 ms |
8540 KB |
Output is correct |
51 |
Correct |
1924 ms |
8604 KB |
Output is correct |
52 |
Correct |
2215 ms |
8592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
7396 KB |
Output is correct |
3 |
Correct |
9 ms |
7516 KB |
Output is correct |
4 |
Correct |
8 ms |
7516 KB |
Output is correct |
5 |
Correct |
7 ms |
7516 KB |
Output is correct |
6 |
Correct |
12 ms |
7516 KB |
Output is correct |
7 |
Correct |
8 ms |
7516 KB |
Output is correct |
8 |
Correct |
13 ms |
7532 KB |
Output is correct |
9 |
Correct |
8 ms |
7516 KB |
Output is correct |
10 |
Correct |
8 ms |
7392 KB |
Output is correct |
11 |
Correct |
8 ms |
7516 KB |
Output is correct |
12 |
Correct |
8 ms |
7564 KB |
Output is correct |
13 |
Correct |
9 ms |
7524 KB |
Output is correct |
14 |
Correct |
9 ms |
7772 KB |
Output is correct |
15 |
Correct |
11 ms |
7832 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
276 ms |
15392 KB |
Output is correct |
18 |
Correct |
245 ms |
15428 KB |
Output is correct |
19 |
Correct |
282 ms |
15424 KB |
Output is correct |
20 |
Correct |
282 ms |
15432 KB |
Output is correct |
21 |
Correct |
330 ms |
15428 KB |
Output is correct |
22 |
Correct |
264 ms |
15428 KB |
Output is correct |
23 |
Correct |
308 ms |
15420 KB |
Output is correct |
24 |
Correct |
309 ms |
15424 KB |
Output is correct |
25 |
Correct |
313 ms |
15428 KB |
Output is correct |
26 |
Correct |
286 ms |
15424 KB |
Output is correct |
27 |
Correct |
237 ms |
15680 KB |
Output is correct |
28 |
Correct |
248 ms |
15680 KB |
Output is correct |
29 |
Correct |
227 ms |
15676 KB |
Output is correct |
30 |
Correct |
278 ms |
15140 KB |
Output is correct |
31 |
Correct |
296 ms |
15168 KB |
Output is correct |
32 |
Correct |
321 ms |
15208 KB |
Output is correct |
33 |
Correct |
271 ms |
15448 KB |
Output is correct |
34 |
Correct |
310 ms |
15428 KB |
Output is correct |
35 |
Correct |
338 ms |
15168 KB |
Output is correct |
36 |
Correct |
264 ms |
15680 KB |
Output is correct |
37 |
Correct |
235 ms |
15428 KB |
Output is correct |
38 |
Correct |
202 ms |
15172 KB |
Output is correct |
39 |
Correct |
252 ms |
15684 KB |
Output is correct |
40 |
Correct |
246 ms |
15688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
8 ms |
7396 KB |
Output is correct |
3 |
Correct |
9 ms |
7516 KB |
Output is correct |
4 |
Correct |
8 ms |
7516 KB |
Output is correct |
5 |
Correct |
7 ms |
7516 KB |
Output is correct |
6 |
Correct |
12 ms |
7516 KB |
Output is correct |
7 |
Correct |
8 ms |
7516 KB |
Output is correct |
8 |
Correct |
13 ms |
7532 KB |
Output is correct |
9 |
Correct |
8 ms |
7516 KB |
Output is correct |
10 |
Correct |
8 ms |
7392 KB |
Output is correct |
11 |
Correct |
8 ms |
7516 KB |
Output is correct |
12 |
Correct |
8 ms |
7564 KB |
Output is correct |
13 |
Correct |
9 ms |
7524 KB |
Output is correct |
14 |
Correct |
9 ms |
7772 KB |
Output is correct |
15 |
Correct |
11 ms |
7832 KB |
Output is correct |
16 |
Correct |
0 ms |
600 KB |
Output is correct |
17 |
Runtime error |
41 ms |
31060 KB |
Execution killed with signal 6 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
8 ms |
7396 KB |
Output is correct |
23 |
Correct |
9 ms |
7516 KB |
Output is correct |
24 |
Correct |
8 ms |
7516 KB |
Output is correct |
25 |
Correct |
7 ms |
7516 KB |
Output is correct |
26 |
Correct |
12 ms |
7516 KB |
Output is correct |
27 |
Correct |
8 ms |
7516 KB |
Output is correct |
28 |
Correct |
13 ms |
7532 KB |
Output is correct |
29 |
Correct |
8 ms |
7516 KB |
Output is correct |
30 |
Correct |
8 ms |
7392 KB |
Output is correct |
31 |
Correct |
8 ms |
7516 KB |
Output is correct |
32 |
Correct |
8 ms |
7564 KB |
Output is correct |
33 |
Correct |
9 ms |
7524 KB |
Output is correct |
34 |
Correct |
9 ms |
7772 KB |
Output is correct |
35 |
Correct |
11 ms |
7832 KB |
Output is correct |
36 |
Correct |
212 ms |
8532 KB |
Output is correct |
37 |
Correct |
342 ms |
7768 KB |
Output is correct |
38 |
Correct |
442 ms |
7768 KB |
Output is correct |
39 |
Correct |
100 ms |
8024 KB |
Output is correct |
40 |
Correct |
462 ms |
7864 KB |
Output is correct |
41 |
Correct |
844 ms |
8280 KB |
Output is correct |
42 |
Correct |
1034 ms |
8280 KB |
Output is correct |
43 |
Correct |
581 ms |
7732 KB |
Output is correct |
44 |
Correct |
665 ms |
7720 KB |
Output is correct |
45 |
Correct |
254 ms |
8028 KB |
Output is correct |
46 |
Correct |
349 ms |
8104 KB |
Output is correct |
47 |
Correct |
489 ms |
7800 KB |
Output is correct |
48 |
Correct |
224 ms |
7824 KB |
Output is correct |
49 |
Correct |
763 ms |
7880 KB |
Output is correct |
50 |
Correct |
446 ms |
8540 KB |
Output is correct |
51 |
Correct |
1924 ms |
8604 KB |
Output is correct |
52 |
Correct |
2215 ms |
8592 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
276 ms |
15392 KB |
Output is correct |
55 |
Correct |
245 ms |
15428 KB |
Output is correct |
56 |
Correct |
282 ms |
15424 KB |
Output is correct |
57 |
Correct |
282 ms |
15432 KB |
Output is correct |
58 |
Correct |
330 ms |
15428 KB |
Output is correct |
59 |
Correct |
264 ms |
15428 KB |
Output is correct |
60 |
Correct |
308 ms |
15420 KB |
Output is correct |
61 |
Correct |
309 ms |
15424 KB |
Output is correct |
62 |
Correct |
313 ms |
15428 KB |
Output is correct |
63 |
Correct |
286 ms |
15424 KB |
Output is correct |
64 |
Correct |
237 ms |
15680 KB |
Output is correct |
65 |
Correct |
248 ms |
15680 KB |
Output is correct |
66 |
Correct |
227 ms |
15676 KB |
Output is correct |
67 |
Correct |
278 ms |
15140 KB |
Output is correct |
68 |
Correct |
296 ms |
15168 KB |
Output is correct |
69 |
Correct |
321 ms |
15208 KB |
Output is correct |
70 |
Correct |
271 ms |
15448 KB |
Output is correct |
71 |
Correct |
310 ms |
15428 KB |
Output is correct |
72 |
Correct |
338 ms |
15168 KB |
Output is correct |
73 |
Correct |
264 ms |
15680 KB |
Output is correct |
74 |
Correct |
235 ms |
15428 KB |
Output is correct |
75 |
Correct |
202 ms |
15172 KB |
Output is correct |
76 |
Correct |
252 ms |
15684 KB |
Output is correct |
77 |
Correct |
246 ms |
15688 KB |
Output is correct |
78 |
Correct |
0 ms |
600 KB |
Output is correct |
79 |
Runtime error |
41 ms |
31060 KB |
Execution killed with signal 6 |
80 |
Halted |
0 ms |
0 KB |
- |