This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define taskname ""
#define el '\n'
#define fi first
#define sc second
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
#define Faster ios_base::sync_with_stdio(false);cin.tie(0);
const int maxn=1e5+7;
const int mod=1e9+9;
int n,a[maxn],q,b,c;
char x;
struct node
{
int sum,pre,suf,seg;
}ST[4*maxn];
node operator +(node a,node b)
{
node res;
res.pre=max(a.sum+b.pre,a.pre);
res.suf=max(a.suf+b.sum,b.suf);
res.sum=a.sum+b.sum;
res.seg=max({a.seg,b.seg,a.suf+b.pre});
return res;
}
void construct(int id,int cl,int cr)
{
if(cl==cr)
{
ST[id].pre=max(0LL,a[cl]);
ST[id].suf=max(0LL,a[cl]);
ST[id].seg=max(0LL,a[cl]);
ST[id].sum=a[cl];
return;
}
int mid=(cl+cr)/2;
construct(2*id,cl,mid);
construct(2*id+1,mid+1,cr);
ST[id]=ST[2*id]+ST[2*id+1];
}
node query(int id,int cl,int cr,int l,int r)
{
if(cl>r||cr<l) return {0,0,0,0};
if(cl>=l&&cr<=r) return ST[id];
int mid=(cl+cr)/2;
return query(2*id,cl,mid,l,r)+query(2*id+1,mid+1,cr,l,r);
}
signed main()
{
if (fopen(taskname".INP","r"))
{
freopen(taskname".INP","r",stdin);
freopen(taskname".OUT","w",stdout);
}
Faster
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
if(x=='C') a[i]=1;
else a[i]=-1;
}
construct(1,1,n);
cin>>q;
while(q--)
{
cin>>b>>c;
node w=query(1,1,n,b,c);
cout<<w.seg-w.sum<<"\n";
}
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:54:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | freopen(taskname".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | freopen(taskname".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |