#include <bits/stdc++.h>
using namespace std;
struct data
{
int l,r,id;
};
typedef pair<int,int> ii;
const int block=350;
data Q[100005];
int n,T,a[100005],Pre[100005];
int64_t rs[100005],Free[100005];
ii b[100005];
multiset<int64_t> s;
bool cmp(data x1,data x2)
{
if(x1.l/block==x2.l/block)
return x1.r<x2.r;
return x1.l/block<x2.l/block;
}
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
cin>>n>>T;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=ii(a[i],i);
}
sort(b+1,b+n+1);
Pre[b[1].second]=1;
int Count=1;
for(int i=2;i<=n;i++)
if(b[i].first>b[i-1].first)
Pre[b[i].second]=++Count;
else
Pre[b[i].second]=Count;
for(int i=1;i<=T;i++)
{
cin>>Q[i].l>>Q[i].r;
Q[i].id=i;
}
sort(Q+1,Q+T+1,cmp);
int l=1,r=0;
for(int i=1;i<=T;i++)
{
while(r<Q[i].r)
{
r++;
if(Free[Pre[r]]>0)
s.erase(s.find(Free[Pre[r]]));
Free[Pre[r]]+=a[r];
s.insert(Free[Pre[r]]);
}
while(l>Q[i].l)
{
l--;
if(Free[Pre[l]]>0)
s.erase(s.find(Free[Pre[l]]));
Free[Pre[l]]+=a[l];
s.insert(Free[Pre[l]]);
}
while(r>Q[i].r)
{
s.erase(s.find(Free[Pre[r]]));
Free[Pre[r]]-=a[r];
if(Free[Pre[r]]>0)
s.insert(Free[Pre[r]]);
r--;
}
while(l<Q[i].l)
{
s.erase(s.find(Free[Pre[l]]));
Free[Pre[l]]-=a[l];
if(Free[Pre[l]]>0)
s.insert(Free[Pre[l]]);
l++;
}
rs[Q[i].id]=*s.rbegin();
}
for(int i=1;i<=T;i++)
cout<<rs[i]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
2 ms |
440 KB |
Output is correct |
5 |
Correct |
2 ms |
440 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
528 KB |
Output is correct |
10 |
Correct |
2 ms |
584 KB |
Output is correct |
11 |
Correct |
2 ms |
676 KB |
Output is correct |
12 |
Correct |
2 ms |
676 KB |
Output is correct |
13 |
Correct |
2 ms |
676 KB |
Output is correct |
14 |
Correct |
2 ms |
676 KB |
Output is correct |
15 |
Correct |
2 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
676 KB |
Output is correct |
2 |
Correct |
7 ms |
676 KB |
Output is correct |
3 |
Correct |
16 ms |
676 KB |
Output is correct |
4 |
Correct |
33 ms |
724 KB |
Output is correct |
5 |
Correct |
74 ms |
892 KB |
Output is correct |
6 |
Correct |
118 ms |
892 KB |
Output is correct |
7 |
Correct |
123 ms |
908 KB |
Output is correct |
8 |
Correct |
96 ms |
924 KB |
Output is correct |
9 |
Correct |
96 ms |
924 KB |
Output is correct |
10 |
Correct |
117 ms |
924 KB |
Output is correct |
11 |
Correct |
127 ms |
1052 KB |
Output is correct |
12 |
Correct |
127 ms |
1052 KB |
Output is correct |
13 |
Correct |
125 ms |
1052 KB |
Output is correct |
14 |
Correct |
127 ms |
1052 KB |
Output is correct |
15 |
Correct |
126 ms |
1068 KB |
Output is correct |
16 |
Correct |
100 ms |
1068 KB |
Output is correct |
17 |
Correct |
46 ms |
1068 KB |
Output is correct |
18 |
Correct |
126 ms |
1068 KB |
Output is correct |
19 |
Correct |
137 ms |
1068 KB |
Output is correct |
20 |
Correct |
124 ms |
1072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1072 KB |
Output is correct |
2 |
Correct |
1 ms |
1072 KB |
Output is correct |
3 |
Correct |
2 ms |
1072 KB |
Output is correct |
4 |
Correct |
2 ms |
1072 KB |
Output is correct |
5 |
Correct |
3 ms |
1072 KB |
Output is correct |
6 |
Correct |
3 ms |
1072 KB |
Output is correct |
7 |
Correct |
6 ms |
1072 KB |
Output is correct |
8 |
Correct |
9 ms |
1072 KB |
Output is correct |
9 |
Correct |
16 ms |
1240 KB |
Output is correct |
10 |
Correct |
16 ms |
1452 KB |
Output is correct |
11 |
Correct |
94 ms |
4396 KB |
Output is correct |
12 |
Correct |
69 ms |
4396 KB |
Output is correct |
13 |
Correct |
61 ms |
4396 KB |
Output is correct |
14 |
Correct |
72 ms |
4396 KB |
Output is correct |
15 |
Correct |
80 ms |
5620 KB |
Output is correct |
16 |
Correct |
102 ms |
6940 KB |
Output is correct |
17 |
Correct |
54 ms |
6940 KB |
Output is correct |
18 |
Correct |
81 ms |
6940 KB |
Output is correct |
19 |
Correct |
69 ms |
6940 KB |
Output is correct |
20 |
Correct |
51 ms |
7340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
7340 KB |
Output is correct |
2 |
Correct |
754 ms |
7340 KB |
Output is correct |
3 |
Correct |
1741 ms |
7340 KB |
Output is correct |
4 |
Correct |
3126 ms |
7340 KB |
Output is correct |
5 |
Execution timed out |
4090 ms |
7340 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |