#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e15;
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+2);
for(int i=1;i<=n;i++)cin>>arr[i];
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]);
}
int q;
cin >> q;
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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
31 ms |
15724 KB |
Output is correct |
3 |
Correct |
26 ms |
15452 KB |
Output is correct |
4 |
Correct |
27 ms |
15708 KB |
Output is correct |
5 |
Correct |
23 ms |
15452 KB |
Output is correct |
6 |
Correct |
31 ms |
15900 KB |
Output is correct |
7 |
Correct |
29 ms |
15284 KB |
Output is correct |
8 |
Correct |
31 ms |
15964 KB |
Output is correct |
9 |
Correct |
29 ms |
15332 KB |
Output is correct |
10 |
Correct |
26 ms |
15704 KB |
Output is correct |
11 |
Correct |
23 ms |
15196 KB |
Output is correct |
12 |
Correct |
30 ms |
15196 KB |
Output is correct |
13 |
Correct |
28 ms |
15192 KB |
Output is correct |
14 |
Correct |
26 ms |
15704 KB |
Output is correct |
15 |
Correct |
27 ms |
15704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
31 ms |
15724 KB |
Output is correct |
3 |
Correct |
26 ms |
15452 KB |
Output is correct |
4 |
Correct |
27 ms |
15708 KB |
Output is correct |
5 |
Correct |
23 ms |
15452 KB |
Output is correct |
6 |
Correct |
31 ms |
15900 KB |
Output is correct |
7 |
Correct |
29 ms |
15284 KB |
Output is correct |
8 |
Correct |
31 ms |
15964 KB |
Output is correct |
9 |
Correct |
29 ms |
15332 KB |
Output is correct |
10 |
Correct |
26 ms |
15704 KB |
Output is correct |
11 |
Correct |
23 ms |
15196 KB |
Output is correct |
12 |
Correct |
30 ms |
15196 KB |
Output is correct |
13 |
Correct |
28 ms |
15192 KB |
Output is correct |
14 |
Correct |
26 ms |
15704 KB |
Output is correct |
15 |
Correct |
27 ms |
15704 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
293 ms |
16956 KB |
Output is correct |
18 |
Correct |
263 ms |
17492 KB |
Output is correct |
19 |
Correct |
308 ms |
17048 KB |
Output is correct |
20 |
Correct |
312 ms |
16968 KB |
Output is correct |
21 |
Correct |
293 ms |
17052 KB |
Output is correct |
22 |
Correct |
275 ms |
17444 KB |
Output is correct |
23 |
Correct |
299 ms |
16980 KB |
Output is correct |
24 |
Correct |
287 ms |
17048 KB |
Output is correct |
25 |
Correct |
283 ms |
16980 KB |
Output is correct |
26 |
Correct |
296 ms |
17060 KB |
Output is correct |
27 |
Correct |
247 ms |
18052 KB |
Output is correct |
28 |
Correct |
219 ms |
17796 KB |
Output is correct |
29 |
Correct |
219 ms |
17812 KB |
Output is correct |
30 |
Correct |
305 ms |
16728 KB |
Output is correct |
31 |
Correct |
307 ms |
16788 KB |
Output is correct |
32 |
Correct |
308 ms |
16980 KB |
Output is correct |
33 |
Correct |
266 ms |
17492 KB |
Output is correct |
34 |
Correct |
337 ms |
16976 KB |
Output is correct |
35 |
Correct |
301 ms |
17040 KB |
Output is correct |
36 |
Correct |
277 ms |
17456 KB |
Output is correct |
37 |
Correct |
225 ms |
16824 KB |
Output is correct |
38 |
Correct |
222 ms |
16932 KB |
Output is correct |
39 |
Correct |
241 ms |
17668 KB |
Output is correct |
40 |
Correct |
267 ms |
17872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
31 ms |
15724 KB |
Output is correct |
3 |
Correct |
26 ms |
15452 KB |
Output is correct |
4 |
Correct |
27 ms |
15708 KB |
Output is correct |
5 |
Correct |
23 ms |
15452 KB |
Output is correct |
6 |
Correct |
31 ms |
15900 KB |
Output is correct |
7 |
Correct |
29 ms |
15284 KB |
Output is correct |
8 |
Correct |
31 ms |
15964 KB |
Output is correct |
9 |
Correct |
29 ms |
15332 KB |
Output is correct |
10 |
Correct |
26 ms |
15704 KB |
Output is correct |
11 |
Correct |
23 ms |
15196 KB |
Output is correct |
12 |
Correct |
30 ms |
15196 KB |
Output is correct |
13 |
Correct |
28 ms |
15192 KB |
Output is correct |
14 |
Correct |
26 ms |
15704 KB |
Output is correct |
15 |
Correct |
27 ms |
15704 KB |
Output is correct |
16 |
Runtime error |
12 ms |
25436 KB |
Execution killed with signal 11 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
12636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |