답안 #440210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
440210 2021-07-01T18:26:43 Z leaked Election (BOI18_election) C++14
100 / 100
1194 ms 109900 KB
#include <bits/stdc++.h>

#define vec vector
#define f first
#define s second
#define endl '\n'
#define pb push_back
#define sz(x) (int)x.size()

using namespace std;
typedef pair<int,int> pii;
const int N=5e5+1;
int lwr[N];
int ans[N],x[N];
vec<pii>qry[N];
int mn[N][20];
int lvl[N];
vec<int>del[N];
bool is=0;
struct tr{
    int sm=0;
    int suf=2*N;
};
tr mg(tr a,tr b){
    tr c;
    c.sm=a.sm+b.sm;
    c.suf=min({b.suf,a.suf+b.sm});
   if(is) cout<<c.suf<<endl;
    return c;
}
tr emp,t[4*N];
void upd(int id,int x,int v,int tl,int tr){
    if(tl==tr){
        t[v].sm=x;t[v].suf=x;
        return;
    }
    int tm=(tl+tr)>>1;
    if(tm>=id) upd(id,x,2*v,tl,tm);
    else upd(id,x,2*v+1,tm+1,tr);
    t[v]=mg(t[2*v],t[2*v+1]);
}

tr get(int l,int r,int v,int tl,int tr){
    if(tl>=l && tr<=r) return t[v];
    if(tl>r || tr<l) return emp;
    int tm=(tl+tr)>>1;
    return mg(get(l,r,2*v,tl,tm),get(l,r,2*v+1,tm+1,tr));
}
int getmn(int l,int r){
    int x=lvl[r-l+1];
    return min(mn[l][x],mn[r-(1<<x)+1][x]);
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,q;
    cin>>n;
    vec<int>a(n+1);string s;
    cin>>s;s.insert(s.begin(),'0');
    for(int i=1;i<=n;i++){
        a[i]=(i?a[i-1]:0)+(s[i]=='C'?1:-1);
        x[i]=(s[i]=='C'?1:-1);
        mn[i][0]=a[i];
    }
    for(int i=2;i<N;i++) lvl[i]=lvl[i/2]+1;
    for(int j=1;j<20;j++){
        for(int i=0;i+(1<<(j-1))<=n;i++){
            mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
            //cout<<i<<' '<<j<<' '<<mn[i][j]<<endl;
        }
    }
    vec<pii>vc;
    {
        vc.pb({-2e9,-1});
        for(int i=0;i<=n;i++){
            while(sz(vc) && vc.back().f>a[i]) vc.pop_back();
            lwr[i]=vc.back().s;
            vc.pb({a[i],i});
        }
    }
    cin>>q;
    for(int i=0;i<q;i++){
        int l,r;
        cin>>l>>r;
        qry[l].pb({r,i});
    }
    for(int i=0;i<=n;i++){
        if(x[i]<0)del[lwr[i]+2].pb(i);
    }
   // int x=0;
    for(int i=n;i>=1;i--){
        int w=(x[i]<0?0:1);
        upd(i,w,1,0,n);
     //   cout<<"APLY "<<i<<' '<<1<<endl;
        for(auto &z : qry[i]){

            int l=i,r=z.f;
            int answ=0;
            answ+=max(0,-(getmn(l,r)-a[l-1]));
           // cout<<get(l,r,1,0,n).suf<<endl;
            answ-=min(0,get(l,r,1,0,n).suf);
            ans[z.s]=answ;
        }
        for(auto &z : del[i]){
            upd(z,-1,1,0,n);
          // cout<<"APly "<<z<<' '<<-1<<endl;
        }
    }
    for(int i=0;i<q;i++) cout<<ans[i]<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 26060 KB Output is correct
2 Correct 17 ms 26092 KB Output is correct
3 Correct 17 ms 26096 KB Output is correct
4 Correct 18 ms 26092 KB Output is correct
5 Correct 19 ms 26060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 26060 KB Output is correct
2 Correct 17 ms 26092 KB Output is correct
3 Correct 17 ms 26096 KB Output is correct
4 Correct 18 ms 26092 KB Output is correct
5 Correct 19 ms 26060 KB Output is correct
6 Correct 124 ms 37824 KB Output is correct
7 Correct 107 ms 37556 KB Output is correct
8 Correct 111 ms 37772 KB Output is correct
9 Correct 121 ms 38076 KB Output is correct
10 Correct 115 ms 38020 KB Output is correct
11 Correct 119 ms 38084 KB Output is correct
12 Correct 115 ms 38212 KB Output is correct
13 Correct 116 ms 37956 KB Output is correct
14 Correct 111 ms 38124 KB Output is correct
15 Correct 114 ms 38148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 26060 KB Output is correct
2 Correct 17 ms 26092 KB Output is correct
3 Correct 17 ms 26096 KB Output is correct
4 Correct 18 ms 26092 KB Output is correct
5 Correct 19 ms 26060 KB Output is correct
6 Correct 124 ms 37824 KB Output is correct
7 Correct 107 ms 37556 KB Output is correct
8 Correct 111 ms 37772 KB Output is correct
9 Correct 121 ms 38076 KB Output is correct
10 Correct 115 ms 38020 KB Output is correct
11 Correct 119 ms 38084 KB Output is correct
12 Correct 115 ms 38212 KB Output is correct
13 Correct 116 ms 37956 KB Output is correct
14 Correct 111 ms 38124 KB Output is correct
15 Correct 114 ms 38148 KB Output is correct
16 Correct 1194 ms 109524 KB Output is correct
17 Correct 918 ms 104792 KB Output is correct
18 Correct 1077 ms 106532 KB Output is correct
19 Correct 944 ms 108136 KB Output is correct
20 Correct 1097 ms 108392 KB Output is correct
21 Correct 1139 ms 109408 KB Output is correct
22 Correct 1087 ms 109900 KB Output is correct
23 Correct 1128 ms 109380 KB Output is correct
24 Correct 1077 ms 108884 KB Output is correct
25 Correct 1097 ms 107568 KB Output is correct