Submission #345977

# Submission time Handle Problem Language Result Execution time Memory
345977 2021-01-08T20:15:54 Z Ruxandra985 Election (BOI18_election) C++14
0 / 100
10 ms 13036 KB
#include <bits/stdc++.h>
#define DIMN 500010
using namespace std;
int vi[DIMN] , sp[DIMN] , tt[DIMN] , next_c[DIMN];
map <int , int> mp;
pair <int , int> rmq[20][DIMN];
int srmq[20][DIMN];
vector <int> v[DIMN];

int aint[4 * DIMN];
int sol;

char buff[10000000];
int pos;

int getnr(){
    int nr = 0;
    while ('0' > buff[pos] || buff[pos] > '9')
        pos++;

    while ('0' <= buff[pos] && buff[pos] <= '9'){
        nr = nr * 10 + buff[pos] - '0';
        pos++;
    }
    return nr;
}


void build (int nod ,int st,int dr){
    int mid = (st + dr)/2;
    if (st == dr){

        aint[nod] = min(0 , vi[st]);

        return;
    }

    build (2*nod,st,mid);
    build (2*nod+1,mid+1,dr);

    aint[nod] = min(aint[2 * nod + 1] , sp[dr] - sp[mid] + aint[2 * nod]);

}

void query (int nod , int st , int dr , int l , int r){
    int mid = (st + dr)/2;

    if (l <= st && dr <= r){ /// sunt in interval bun
        sol = min(sol , aint[nod] + sp[r] - sp[dr]);

    }
    else {

        if (mid + 1 <= r)
            query(2 * nod + 1 , mid + 1 , dr , l , r);

        if (l <= mid)
            query(2 * nod , st , mid , l , r);


    }


}

int main()
{
    FILE *fin = stdin;
    //FILE *ans = fopen ("idk.in","r");
    FILE *fout = stdout;
    int n, q , i , l , r , nod , j , vecin , maxi , fth , inc = 0;
    char c;

    fread(buff , 1 , 10000000 , fin);

    n = getnr();
    pos++;
    for (i = 1 ; i <= n ; i++){
        c=buff[pos];
        if (c == 'C')
            vi[i] = 1;
        else vi[i] = -1;

        sp[i] = vi[i] + sp[i - 1];
        pos++;

    }
    next_c[n + 1] = n + 1;
    for (i = n ; i ; i--){
        if (vi[i] == 1)
            next_c[i] = i;
        else next_c[i] = next_c[i + 1];
    }

    for (i = n ; i ; i--){

        /// sp[j] - sp[i - 1] = -1
        /// sp[j] = -1 + sp[i - 1]

        if (vi[i] == -1){

            v[i + 1].push_back(i);
            tt[i] = i + 1;

        }
        else {

            if (mp[sp[i - 1] - 1] != 0){
                v[mp[sp[i - 1] - 1]].push_back(i); /// intervalul i...chestie are suma -1
                tt[i] = mp[sp[i - 1] - 1];
            }
            else {
                v[n + 1].push_back(i); /// n + 1 = nod fictiv radacina
                tt[i] = n + 1;
            }

        }

        mp[sp[i]] = i;

    }

    build (1 , 1 , n);

    /// vezi ce valoare dai muchiilor

    for (nod = 1 ; nod <= n + 1 ; nod++){ /// nod este tatal

        for (j = 0 ; j < v[nod].size() ; j++){

            vecin = v[nod][j];

            //printf ("%d %d " , vecin , nod);

            if (nod - vecin > 1 || (nod - vecin == 1 && vi[nod] == -1))
                srmq[0][vecin] = 1; /// altfel e 0

            if (nod - vecin == 1 && vi[vecin] == -1){

                rmq[0][vecin] = make_pair(nod , 0);
                //printf ("%d\n", 0);
                continue;

            }

            sol = DIMN;
            query(1 , 1 , n , vecin , min(n , nod));

            if (nod != n + 1){
                rmq[0][vecin] = make_pair(nod , -sol - 1);
                //printf ("%d\n", -sol - 1);
            }
            else {
                rmq[0][vecin] = make_pair(nod , -sol);
                //printf ("%d\n",-sol);
            }

        }

    }

    for (i = 1 ; i < 20 ; i++){
        for (j = 1 ; j <= n ; j++){


            rmq[i][j].first = rmq[i - 1][rmq[i - 1][j].first].first;

            //rmq[i][j].second = rmq[i - 1][rmq[i - 1][j].first].second

            rmq[i][j].second = max(rmq[i - 1][j].second , rmq[i - 1][rmq[i - 1][j].first].second);
            srmq[i][j] = srmq[i - 1][j] + srmq[i - 1][rmq[i - 1][j].first];

        }
    }



    q = getnr();

    int correct , mini = DIMN , li , ri;
    int deadd = 0 , ant;

    for (int aux = 1 ; aux <= q ; aux++){

        l = getnr();
        r = getnr();

        li = l;
        ri = r;

        maxi = 0;

        inc = 0;
        l = next_c[l];

        if (l > r){
            deadd = ri - li + 1;
            inc = maxi = 0;
        }
        else {
            deadd = l - li;

            while (tt[l] <= r){

                if (srmq[0][l]){
                    if (l + 1 != tt[l])
                        maxi = max(maxi - (sp[tt[l] - 1] - sp[l - 1]) , rmq[0][l].second);
                }
                inc += srmq[0][l];

                ant = srmq[0][l];

                l = tt[l];

            }

            /*maxi = 0;
            inc = -1;
            for (i = 19 ; i >= 0 ; i--){

                fth = rmq[i][l].first;
                if (fth  < r && fth){
                    maxi = max(maxi , rmq[i][l].second);
                    l = fth;
                    inc += (1 << i);
                }

            }*/

            /// l e < r

            if (ant)
                l++;

            if (l <= r){
                sol = DIMN;
                query (1 , 1 , n , l , r);
                maxi = max(maxi - ( sp[r] - sp[l - 1] ) , -sol);
            }

        }


        fprintf (fout,"%d\n",inc + maxi + deadd);

    }
    return 0;
}

Compilation message

election.cpp: In function 'int main()':
election.cpp:129:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for (j = 0 ; j < v[nod].size() ; j++){
      |                      ~~^~~~~~~~~~~~~~~
election.cpp:71:53: warning: unused variable 'fth' [-Wunused-variable]
   71 |     int n, q , i , l , r , nod , j , vecin , maxi , fth , inc = 0;
      |                                                     ^~~
election.cpp:180:9: warning: unused variable 'correct' [-Wunused-variable]
  180 |     int correct , mini = DIMN , li , ri;
      |         ^~~~~~~
election.cpp:180:19: warning: unused variable 'mini' [-Wunused-variable]
  180 |     int correct , mini = DIMN , li , ri;
      |                   ^~~~
election.cpp:74:10: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   74 |     fread(buff , 1 , 10000000 , fin);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:232:13: warning: 'ant' may be used uninitialized in this function [-Wmaybe-uninitialized]
  232 |             if (ant)
      |             ^~
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 13036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 13036 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 13036 KB Output isn't correct
2 Halted 0 ms 0 KB -