답안 #78877

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
78877 2018-10-09T12:22:16 Z bogdan10bos Election (BOI18_election) C++14
100 / 100
821 ms 123956 KB
#include <bits/stdc++.h>

using namespace std;

//#define FILE_IO

typedef pair<int, int> pii;

pii qry[500005];
int N, Q;
int sol[500005];
char s[500005];
vector<int> stv, queries[500005];

namespace BinaryIndexedTree
{
    int aib[500005];
    void update(int pos, int val)
    {
        for(; pos <= N; pos += (pos & (-pos)))
            aib[pos] += val;
    }

    int query(int pos)
    {
        int ans = 0;
        for(; pos > 0; pos -= (pos & (-pos)))
            ans += aib[pos];
        return ans;
    }

    int query(int st, int dr) { return query(dr) - query(st - 1); }
}

namespace SegmentTree
{
    int sti, dri, pos, val, ans, pfx;
    int sum[2000005], mn[2000005];

    void remake(int nod)
    {
        sum[nod] = sum[nod * 2] + sum[nod * 2 + 1];
        mn[nod] = min(mn[nod * 2], mn[nod * 2 + 1] + sum[nod * 2]);
    }

    void B(int nod, int st, int dr)
    {
        if(st == dr)
        {
            sum[nod] = mn[nod] = s[st];
            return;
        }

        int mij = st + (dr - st) / 2;
        B(nod * 2, st, mij);
        B(nod * 2 + 1, mij + 1, dr);

        remake(nod);
    }

    void U(int nod, int st, int dr)
    {
        if(st == dr)
        {
            sum[nod] = mn[nod] = val;
            return;
        }

        int mij = st + (dr - st) / 2;

        if(pos <= mij)  U(nod * 2, st, mij);
        else    U(nod * 2 + 1, mij + 1, dr);

        remake(nod);
    }

    void Q(int nod, int st, int dr)
    {
        if(sti <= st && dr <= dri)
        {
            ans = min(ans, pfx + mn[nod]);
            pfx += sum[nod];
            return;
        }

        int mij = st + (dr - st) / 2;
        if(sti <= mij)  Q(nod * 2, st, mij);
        if(mij < dri)   Q(nod * 2 + 1, mij + 1, dr);
    }

    void update(int _pos, int _val)
    {
        pos = _pos, val = _val;
        U(1, 1, N);
    }

    int query(int st, int dr)
    {
        sti = st, dri = dr;
        pfx = 0;
        ans = 1 << 30;
        Q(1, 1, N);
        if(ans < 0) return -ans;
        return 0;
    }

    void build() { B(1, 1, N); }
}

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    scanf("%d\n", &N);
    scanf("%s", s + 1);
    for(int i = 1; i <= N; i++) s[i] = (s[i] == 'C' ? 1 : -1);

    scanf("%d", &Q);
    for(int q = 1; q <= Q; q++)
    {
        int st, dr;
        scanf("%d%d", &st, &dr);
        qry[q] = {st, dr};
        queries[dr].push_back(q);
    }

    SegmentTree::build();
    for(int i = 1; i <= N; i++)
    {
        if(s[i] == -1)
        {
            stv.push_back(i);
            SegmentTree::update(i, 0);
            BinaryIndexedTree::update(i, 1);
        }
        else
        {
            if(!stv.empty())
            {
                int x = stv.back(); stv.pop_back();
                SegmentTree::update(x, -1);
                BinaryIndexedTree::update(x, -1);
            }
        }

        for(auto id: queries[i])
        {
            int st = qry[id].first, dr = qry[id].second;
            sol[id] = SegmentTree::query(st, dr) + BinaryIndexedTree::query(st, dr);
        }
    }

    for(int i = 1; i <= Q; i++)
        printf("%d\n", sol[i]);

    return 0;
}

Compilation message

election.cpp: In function 'int main()':
election.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d\n", &N);
     ~~~~~^~~~~~~~~~~~
election.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", s + 1);
     ~~~~~^~~~~~~~~~~~~
election.cpp:121:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &Q);
     ~~~~~^~~~~~~~~~
election.cpp:125:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &st, &dr);
         ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12280 KB Output is correct
2 Correct 14 ms 12408 KB Output is correct
3 Correct 14 ms 12568 KB Output is correct
4 Correct 14 ms 12568 KB Output is correct
5 Correct 14 ms 12644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12280 KB Output is correct
2 Correct 14 ms 12408 KB Output is correct
3 Correct 14 ms 12568 KB Output is correct
4 Correct 14 ms 12568 KB Output is correct
5 Correct 14 ms 12644 KB Output is correct
6 Correct 103 ms 17932 KB Output is correct
7 Correct 87 ms 18296 KB Output is correct
8 Correct 85 ms 19560 KB Output is correct
9 Correct 88 ms 20848 KB Output is correct
10 Correct 104 ms 21668 KB Output is correct
11 Correct 103 ms 22964 KB Output is correct
12 Correct 95 ms 23800 KB Output is correct
13 Correct 94 ms 24896 KB Output is correct
14 Correct 91 ms 25588 KB Output is correct
15 Correct 90 ms 26488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12280 KB Output is correct
2 Correct 14 ms 12408 KB Output is correct
3 Correct 14 ms 12568 KB Output is correct
4 Correct 14 ms 12568 KB Output is correct
5 Correct 14 ms 12644 KB Output is correct
6 Correct 103 ms 17932 KB Output is correct
7 Correct 87 ms 18296 KB Output is correct
8 Correct 85 ms 19560 KB Output is correct
9 Correct 88 ms 20848 KB Output is correct
10 Correct 104 ms 21668 KB Output is correct
11 Correct 103 ms 22964 KB Output is correct
12 Correct 95 ms 23800 KB Output is correct
13 Correct 94 ms 24896 KB Output is correct
14 Correct 91 ms 25588 KB Output is correct
15 Correct 90 ms 26488 KB Output is correct
16 Correct 806 ms 56196 KB Output is correct
17 Correct 663 ms 59516 KB Output is correct
18 Correct 730 ms 69104 KB Output is correct
19 Correct 682 ms 78668 KB Output is correct
20 Correct 810 ms 85416 KB Output is correct
21 Correct 806 ms 95204 KB Output is correct
22 Correct 821 ms 101996 KB Output is correct
23 Correct 800 ms 110664 KB Output is correct
24 Correct 805 ms 117244 KB Output is correct
25 Correct 745 ms 123956 KB Output is correct