Submission #1089816

# Submission time Handle Problem Language Result Execution time Memory
1089816 2024-09-17T08:22:10 Z lucri Election (BOI18_election) C++17
100 / 100
345 ms 45908 KB
#include <bits/stdc++.h>
using namespace std;
char a[500010];
bool e[500010];
int n,q,x,y,ans[500010];
vector<pair<int,int>>qu[500010];
struct arboreintervale{int sum,minp;}aint[2000010],zero;
arboreintervale join(arboreintervale a,arboreintervale b)
{
    arboreintervale c;
    c.sum=a.sum+b.sum;
    c.minp=min(a.minp,a.sum+b.minp);
    return c;
}
stack<int>s;
void update(int poz,int b,int e,int pozu,int val)
{
    if(b>pozu||e<pozu)
        return;
    if(b==e)
    {
        aint[poz].sum=val;
        aint[poz].minp=min(0,val);
        return;
    }
    update(poz*2,b,(b+e)/2,pozu,val);
    update(poz*2+1,(b+e)/2+1,e,pozu,val);
    aint[poz]=join(aint[poz*2],aint[poz*2+1]);
}
arboreintervale interval(int poz,int b,int e,int lim)
{
    if(e<lim)
        return zero;
    if(b>=lim)
        return aint[poz];
    return join(interval(poz*2,b,(b+e)/2,lim),interval(poz*2+1,(b+e)/2+1,e,lim));
}
int aib[500010];
int suma(int poz)
{
    int ans=0;
    for(;poz>=1;poz-=(poz&(-poz)))
        ans+=aib[poz];
    return ans;
}
void aduna(int poz,int val)
{
    for(;poz<=n;poz+=(poz&(-poz)))
        aib[poz]+=val;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>a+1>>q;
    for(int i=1;i<=q;++i)
    {
        cin>>x>>y;
        qu[y].push_back({x,i});
    }
    for(int i=1;i<=n;++i)
    {
        if(a[i]=='T')
        {
            s.push(i);
            aduna(i,1);
        }
        else
        {
            update(1,1,n,i,1);
            if(!s.empty())
            {
                update(1,1,n,s.top(),-1);
                aduna(s.top(),-1);
                s.pop();
            }
        }
        for(auto x:qu[i])
        {
            arboreintervale arb=interval(1,1,n,x.first);
            ans[x.second]=s.size()-arb.minp-suma(x.first-1);
        }
    }
    for(int i=1;i<=q;++i)
        cout<<ans[i]<<'\n';
    return 0;
}

Compilation message

election.cpp: In function 'int main()':
election.cpp:56:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   56 |     cin>>n>>a+1>>q;
      |             ~^~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12120 KB Output is correct
2 Correct 6 ms 12124 KB Output is correct
3 Correct 6 ms 12124 KB Output is correct
4 Correct 7 ms 12288 KB Output is correct
5 Correct 6 ms 12128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12120 KB Output is correct
2 Correct 6 ms 12124 KB Output is correct
3 Correct 6 ms 12124 KB Output is correct
4 Correct 7 ms 12288 KB Output is correct
5 Correct 6 ms 12128 KB Output is correct
6 Correct 42 ms 17232 KB Output is correct
7 Correct 40 ms 16988 KB Output is correct
8 Correct 43 ms 16980 KB Output is correct
9 Correct 42 ms 17236 KB Output is correct
10 Correct 47 ms 18260 KB Output is correct
11 Correct 39 ms 18512 KB Output is correct
12 Correct 39 ms 18468 KB Output is correct
13 Correct 38 ms 18004 KB Output is correct
14 Correct 41 ms 18260 KB Output is correct
15 Correct 39 ms 18256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12120 KB Output is correct
2 Correct 6 ms 12124 KB Output is correct
3 Correct 6 ms 12124 KB Output is correct
4 Correct 7 ms 12288 KB Output is correct
5 Correct 6 ms 12128 KB Output is correct
6 Correct 42 ms 17232 KB Output is correct
7 Correct 40 ms 16988 KB Output is correct
8 Correct 43 ms 16980 KB Output is correct
9 Correct 42 ms 17236 KB Output is correct
10 Correct 47 ms 18260 KB Output is correct
11 Correct 39 ms 18512 KB Output is correct
12 Correct 39 ms 18468 KB Output is correct
13 Correct 38 ms 18004 KB Output is correct
14 Correct 41 ms 18260 KB Output is correct
15 Correct 39 ms 18256 KB Output is correct
16 Correct 345 ms 44804 KB Output is correct
17 Correct 262 ms 41808 KB Output is correct
18 Correct 283 ms 43024 KB Output is correct
19 Correct 291 ms 44584 KB Output is correct
20 Correct 300 ms 43856 KB Output is correct
21 Correct 299 ms 45908 KB Output is correct
22 Correct 319 ms 44624 KB Output is correct
23 Correct 324 ms 43860 KB Output is correct
24 Correct 313 ms 44628 KB Output is correct
25 Correct 336 ms 43904 KB Output is correct