Submission #996100

# Submission time Handle Problem Language Result Execution time Memory
996100 2024-06-10T07:59:22 Z amine_aroua Election (BOI18_election) C++17
0 / 100
9 ms 29272 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define pb push_back
#define nl '\n'
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i, x, y) for(int i = x;i<=y;i++)
#define forn(i, y, x) for(int i = y; i >= x; i--)
int n , q;
const int N = (1<<19);
struct segTree
{
    vector<int> tree;
    segTree(vector<int> &a)
    {
        tree.assign(2*N , N);
        fore(i , N)
        {
            tree[i + N] = a[i];
        }
        forn(i , N - 1 , 1)
            tree[i] = min(tree[2*i] , tree[2*i + 1]);
    }
    int query(int l , int r)
    {
        l+=N;
        r+=N;
        if(l == r)
            return tree[l];
        int lt = tree[l] , rt = tree[r];
        while(l + 1 < r) {
            if (l % 2 == 0)
                lt = min(lt, tree[l + 1]);
            if (r % 2 == 1)
                rt = min(tree[r - 1], rt);
            l>>=1;
            r>>=1;
        }
        return min(lt , rt);
    }
};
vector<int> a(N) , pref(N) , suf(N);
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    map<int , vector<int>>posPref , posSuf ;
    forr(i , 1 , n)
    {
        char c;
        cin>>c;
        if(c == 'C')
            a[i] = 1;
        else
            a[i] = -1;
        pref[i] = a[i] + pref[i - 1];
        posPref[pref[i]].pb(i);
    }
    forn(i , n , 1)
    {
        suf[i] = suf[i + 1] + a[i];
    }
    forr(i , 1 , n)
        posSuf[suf[i]].pb(i);
    segTree pmin(pref) , smin(suf);
    cin>>q;
    while (q--)
    {
        int l , r;
        cin>>l>>r;
        int mp = pmin.query(l , r) - pref[l - 1];
        int ms = smin.query(l , r) - suf[r + 1];
        auto &v = posPref[mp + pref[l - 1]];
        int i = *lower_bound(v.begin() , v.end() , l);
        auto &u = posSuf[ms + suf[r + 1]];
        int j = *prev(upper_bound(u.begin() , u.end() , r));
        if(i < j)
            cout<<abs(mp) + abs(ms)<<nl;
        else
            cout<<max(abs(mp) , abs(ms))<<nl;
    }
}

# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 29272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 29272 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 29272 KB Output isn't correct
2 Halted 0 ms 0 KB -