Submission #97378

# Submission time Handle Problem Language Result Execution time Memory
97378 2019-02-15T15:30:16 Z georgerapeanu Election (BOI18_election) C++11
100 / 100
1187 ms 52012 KB
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int NMAX = 5e5;
const int QMAX = 5e5;

struct node_t{
    int sum;
    int suf_sum;
   
    node_t(int sum = 0,int suf_sum = 0){
        this->sum = sum;
        this->suf_sum = suf_sum;
    }

    node_t join(node_t &other){
        return node_t(sum + other.sum,min(other.suf_sum,suf_sum + other.sum));
    }
};

struct query_t{
    int r,ind;
};

node_t aint[4 * NMAX + 5];

void update(int nod,int st,int dr,int pos,int val){
    if(dr < pos || st > pos){
        return ;
    }
    
    if(st == dr){
        aint[nod].sum += val;
        aint[nod].suf_sum += val;
        return;
    }

    int mid = (st + dr) / 2;

    update(nod * 2,st,mid,pos,val);
    update(nod * 2 + 1,mid + 1,dr,pos,val);

    aint[nod] = aint[2 * nod].join(aint[2 * nod + 1]);
}

node_t query(int nod,int st,int dr,int S,int D){
    if(st > D || dr < S){
        return {0,0};
    }
    
    if(S <= st && dr <= D){
        return aint[nod];
    }

    int mid = (st + dr) / 2;

    node_t a = query(nod * 2,st,mid,S,D);
    node_t b = query(nod * 2 + 1,mid + 1,dr,S,D);

    return a.join(b);
}

int n,q;
int ans[QMAX + 5];
char c[NMAX + 5];
vector<query_t> queries[NMAX + 5];

int non_active[NMAX + 5],len;

int main(){
    
    scanf("%d\n",&n);
    fgets(c + 1,NMAX + 5,stdin);
    scanf("%d\n",&q);
    
    for(int i = 1;i <= q;i++){
        int l,r;
        scanf("%d %d\n",&l,&r);
        queries[l].push_back({r,i});
    }

    for(int i = n;i;i--){
        if(c[i] == 'T'){
            non_active[++len] = i;
        }
        else{
            update(1,1,n,i,1);
            if(len){
                update(1,1,n,non_active[len],-1);
                len--;
            }
        }
        for(auto it:queries[i]){
            int tmp = max(0,-query(1,1,n,i,it.r).suf_sum);
            int st = 0,dr = len + 1;
            while(dr - st > 1){
                int mid = (st + dr) / 2;
                if(non_active[mid] <= it.r){
                    dr = mid;
                }
                else{
                    st = mid;
                }
            }
            tmp += len - st;
            ans[it.ind] = tmp;
        }
    }

    for(int i = 1;i <= q;i++){
        printf("%d\n",ans[i]);
    }

    return 0;
}

Compilation message

election.cpp: In function 'int main()':
election.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d\n",&n);
     ~~~~~^~~~~~~~~~~
election.cpp:76:10: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fgets(c + 1,NMAX + 5,stdin);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
election.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d\n",&q);
     ~~~~~^~~~~~~~~~~
election.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d\n",&l,&r);
         ~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:936:0,
                 from /usr/include/c++/7/cstdio:42,
                 from election.cpp:1:
In function 'char* fgets(char*, int, FILE*)',
    inlined from 'int main()' at election.cpp:76:10:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:261:58: warning: call to '__fgets_chk_warn' declared with attribute warning: fgets called with bigger size than length of destination buffer
  return __fgets_chk_warn (__s, __bos (__s), __n, __stream);
                                                          ^
# Verdict Execution time Memory Grader output
1 Correct 29 ms 27776 KB Output is correct
2 Correct 31 ms 27772 KB Output is correct
3 Correct 31 ms 27908 KB Output is correct
4 Correct 34 ms 27768 KB Output is correct
5 Correct 30 ms 27768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 27776 KB Output is correct
2 Correct 31 ms 27772 KB Output is correct
3 Correct 31 ms 27908 KB Output is correct
4 Correct 34 ms 27768 KB Output is correct
5 Correct 30 ms 27768 KB Output is correct
6 Correct 157 ms 30912 KB Output is correct
7 Correct 135 ms 30276 KB Output is correct
8 Correct 141 ms 30412 KB Output is correct
9 Correct 135 ms 30676 KB Output is correct
10 Correct 146 ms 30608 KB Output is correct
11 Correct 131 ms 30840 KB Output is correct
12 Correct 155 ms 30888 KB Output is correct
13 Correct 153 ms 30968 KB Output is correct
14 Correct 149 ms 30916 KB Output is correct
15 Correct 142 ms 30852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 27776 KB Output is correct
2 Correct 31 ms 27772 KB Output is correct
3 Correct 31 ms 27908 KB Output is correct
4 Correct 34 ms 27768 KB Output is correct
5 Correct 30 ms 27768 KB Output is correct
6 Correct 157 ms 30912 KB Output is correct
7 Correct 135 ms 30276 KB Output is correct
8 Correct 141 ms 30412 KB Output is correct
9 Correct 135 ms 30676 KB Output is correct
10 Correct 146 ms 30608 KB Output is correct
11 Correct 131 ms 30840 KB Output is correct
12 Correct 155 ms 30888 KB Output is correct
13 Correct 153 ms 30968 KB Output is correct
14 Correct 149 ms 30916 KB Output is correct
15 Correct 142 ms 30852 KB Output is correct
16 Correct 1187 ms 50316 KB Output is correct
17 Correct 937 ms 46552 KB Output is correct
18 Correct 961 ms 47816 KB Output is correct
19 Correct 1033 ms 49240 KB Output is correct
20 Correct 1083 ms 49576 KB Output is correct
21 Correct 1119 ms 51692 KB Output is correct
22 Correct 992 ms 51352 KB Output is correct
23 Correct 1140 ms 52012 KB Output is correct
24 Correct 1112 ms 51424 KB Output is correct
25 Correct 1088 ms 50680 KB Output is correct