# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1018411 | YassirSalama | Election (BOI18_election) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define endl "\n"
// #define int ll
using ull=unsigned long long;
using ll=long long;
using pii=pair<int,int>;
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int mxn=5e5+100;
struct Node{
int sum;
int mxl;
int mxr;
int ans;
} tree[mxn];
int arr[mxn];
Node merge(Node a,Node b){
Node c;
c.sum=a.sum+b.sum;
c.mxl=max(a.mxl,a.sum+b.mxl);
c.mxr=max(b.mxr,b.sum+a.mxr);
c.ans=max({a.mxl+b.mxr,a.sum+b.ans,b.sum+a.ans});
return c;
}
void build(int node,int l,int r){
if(l==r){
int x=max(0LL,arr[l]);
tree[node]={arr[l],x,x,x};
return;
}
int mid=(l+r)/2;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
tree[node]=merge(tree[node<<1],tree[node<<1|1]);
}
Node query(int node,int l,int r,int ql,int qr){
if(ql>r||qr<l) return {0,0,0,0};
if(ql<=l&&r<=qr) return tree[node];
int mid=(l+r)/2;
return merge(
query(node<<1,l,mid,ql,qr),
query(node<<1|1,mid+1,r,ql,qr)
);
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n;
cin>>n;
string s;
cin>>s;
// int arr[n];
for(int i=0;i<n;i++){
if(s[i]=='C') arr[i]=-1;
else arr[i]=1;
}
build(1,0,n-1);
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
cout<<query(1,0,n-1,l-1,r-1).ans<<endl;
}
}