Submission #1021176

#TimeUsernameProblemLanguageResultExecution timeMemory
1021176PieArmyElection (BOI18_election)C++17
100 / 100
324 ms46240 KiB
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}

struct Node{
    int sum=0,pref=0,suf=0,ans=0;
};

struct Seg{
    int n;
    vector<Node>tree;
    vector<int>arr;
    Node comb(Node x,Node y){
        Node res;
        res.sum=x.sum+y.sum;
        res.pref=max(x.pref,x.sum+y.pref);
        res.suf=max(x.suf+y.sum,y.suf);
        res.ans=max(max(x.ans+y.sum,x.sum+y.ans),x.pref+y.suf);
        return res;
    }
    void build(int node=1,int left=0,int right=-1){
        if(right==-1)right=n-1;
        if(left==right){
            tree[node].sum=arr[left];
            tree[node].pref=max(arr[left],0);
            tree[node].suf=max(arr[left],0);
            tree[node].ans=max(arr[left],0);
            return;
        }
        build(node*2,left,mid);build(node*2+1,mid+1,right);
        tree[node]=comb(tree[node*2],tree[node*2+1]);
    }
    Seg(vector<int>v){
        n=v.size();
        arr=v;
        tree.resize(n<<2);
        build();
    }
    int l,r;
    Node qu(int node=1,int left=0,int right=-1){
        if(right==-1)right=n-1;
        Node res;
        if(left>r||right<l)return res;
        if(left>=l&&right<=r)return tree[node]; 
        res=comb(qu(node*2,left,mid),qu(node*2+1,mid+1,right));
        return res;
    }
    int query(int lef,int rig){
        l=lef;r=rig;
        return qu().ans;
    }
};

void code(){
	int n;cin>>n;
    vector<int>v;
    for(int i=0;i<n;i++){
        char c;cin>>c;
        v.pb(c=='T'?1:-1);
    }
    Seg seg(v);
    int q;cin>>q;
    while(q--){
        int l,r;cin>>l>>r;
        cout<<seg.query(l-1,r-1)<<endl;
    }
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
	int t=1;
	if(!t)cin>>t;
	while(t--){code();cout<<endl;}
    return 0;
}

Compilation message (stderr)

election.cpp: In function 'int main()':
election.cpp:80:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~
election.cpp:80:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...