답안 #413168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
413168 2021-05-28T10:36:38 Z Theo830 Election (BOI18_election) C++17
0 / 100
10 ms 332 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1e9+7;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
struct node{
   ll sum;
   ll mpre;
   ll msuf;
};
node seg[2000005];
string S;
void build(ll idx,ll s, ll e){
    if(e == s){
        ll v = 1;
        if(S[e-1] == 'C'){
            v = -1;
        }
        seg[idx].sum = v;
        seg[idx].mpre = seg[idx].msuf = max(0LL,v);
        return;
    }
    ll mid = (e+s)/2;
    build(idx*2,s,mid);
    build(idx*2+1,mid+1,e);
    seg[idx].sum = seg[idx*2].sum + seg[idx*2+1].sum;
    seg[idx].mpre = max(seg[idx*2].mpre,seg[idx*2].sum+seg[idx*2+1].mpre);
    seg[idx].msuf = max(seg[idx*2+1].msuf,seg[idx*2+1].sum+seg[idx*2].msuf);
}
node query(ll idx,ll s,ll e,ll qs, ll qe){
    node anss;
    if(qs <= s && e <= qe){
        return seg[idx];
    }
    if(qs > e || qe < s){
        anss.sum = anss.mpre = anss.msuf = 0;
        return anss;
    }
    ll mid = (e+s)/2;
    node a = query(idx*2,s,mid,qs,qe);
    node b = query(idx*2+1,mid+1,e,qs,qe);
    anss.sum = a.sum + b.sum;
    anss.mpre = max(a.mpre,a.sum+b.mpre);
    anss.msuf = max(b.msuf,b.sum+a.msuf);
    return anss;
}
int main(void){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n,q;
    cin>>n;
    string s;
    cin>>s;
    cin>>q;
    ll team[n+1] = {0};
    S = s;
    build(1,1,n);
    for(ll i = n-1;i >= 0;i--){
        if(s[i] == 'T'){
            if(query(1,1,n,i+2,n).mpre <= 0){
                team[i+1] = i+1;
            }
            else{
                ll l = i+2,r = n;
                ll ans = n;
                while(l <= r){
                    ll mid = (l+r)/2;
                    if(query(1,1,n,i+2,mid).mpre > 0){
                        r = mid - 1;
                        ans = min(ans,mid);
                    }
                    else{
                        l = mid + 1;
                    }
                }
                team[i+1] = ans;
            }
        }
    }
    while(q--){
        ll L,R;
        cin>>L>>R;
        ll ans = 0;
        ll POS = L,POS2 = L-1;
        if(query(1,1,n,L,R).mpre > 0){
            ll l = L,r = R;
            ll res = R;
            while(l <= r){
                ll mid = (l+r)/2;
                if(query(1,1,n,L,mid).mpre > 0){
                    r = mid - 1;
                    res = min(res,mid);
                }
                else{
                    l = mid + 1;
                }
            }
            ll T = res;
            ans = 1;
            while(team[T] <= R && T != team[T]){
                ans++;
                T = team[T];
            }
            /*
            ll posl = lower_bound(all(arr[T]),res) - arr[T].begin();
            ll P = upper_bound(all(arr[T]),R) - arr[T].begin();
            ll posr = min((ll)arr[T].size() - 1,P);
            ans += posr - posl + 1;
            */
            POS = T + 1;
            POS2 = res - 1;
        }
        ans += query(1,1,n,POS,R).msuf;
        ans += max(0LL,query(1,1,n,L,POS2).msuf - max(0LL,-query(1,1,n,POS,R).sum+query(1,1,n,POS,R).msuf));
        cout<<ans<<"\n";
    }
}
/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -