Submission #849244

# Submission time Handle Problem Language Result Execution time Memory
849244 2023-09-14T09:56:18 Z faruk The Potion of Great Power (CEOI20_potion) C++17
100 / 100
449 ms 44116 KB
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>

using namespace std;

/*
Save neighbour list by node, reduce storage space by constant factor (DIF)

Made for your by Mate Busa
*/

const int INF = 1000000000;
const int DIF = 50; //other numbers would also work, but i like this number
int NN, cur, goes, last, place, vv, a, b, ans;
int HH[100000];
int who[2][DIF];
int step[2];
int id[2];
int num[2];
int st[2];
int p[2][100000];
bool in[100000];
bool out[2][100000];
struct point{
    int a, day, id;
    bool in;
};
vector<pair<int,int> > change[100000];
vector<point> update[100000];
vector<pair<int,int> > vec[400000];
set<pair<int,int> > szet;
int news[2][DIF];

void init(int N, int D, int H[]) {
    NN = N; cur = 1;
    for(int i=0; i<N; i++) HH[i] = H[i];
}

void curseChanges(int U, int A[], int B[]) {
    for(int i=0; i<U; i++) {
        change[A[i]].push_back({B[i],i+1});
        change[B[i]].push_back({A[i],i+1});
    }
    for(int i=0; i<NN; i++){
        update[i].resize(change[i].size()+1); update[i][0].day = 0; update[i][0].id = 0; goes = 1;
        szet.clear();
        for(pair<int,int> j:change[i]){
            in[j.first] = !in[j.first];
            update[i][goes].in = in[j.first];
            update[i][goes].a = j.first;
            update[i][goes].day = j.second;
            if(in[j.first]){
                szet.insert({HH[j.first],j.first});
            } else {
                szet.erase({HH[j.first],j.first});
            }
            if(goes%DIF==0){
                vec[cur].resize(szet.size()); place = 0;
                for(pair<int,int> k:szet) vec[cur][place++] = k;
                update[i][goes].id = cur++;
            } else {
                update[i][goes].id = -1;
            }
            goes++;
        }
        for(pair<int,int> j:szet) in[j.second] = false;
    }
}

int bi(int x, int y){
    if(x==y) return x;
    int fel = (x+y+1)/2;
    if(update[place][fel].day<=vv) return bi(fel,y);
    return bi(x,fel-1);
}
void choose(int x, int y){
    place = x; place = bi(0,update[x].size()-1);
    step[y] = 0; num[y] = 0;
    while(update[x][place].id==-1){
        a = update[x][place].a;
        if(!in[a]){
            in[a] = true;
            if(update[x][place].in){
                news[y][num[y]++] = HH[a];
            } else {
                out[y][a] = true;
            }
        }
        who[y][step[y]++] = a;
        place--;
    }
    sort(news[y],news[y]+num[y]);
    for(int i=0; i<step[y]; i++) {in[who[y][i]] = false;}
    id[y] = update[x][place].id;
}
int question(int x, int y, int v) {
    vv = v;
    choose(x,0); choose(y,1);
    ans = INF;
    for(int i=0; i<2; i++){
        int aa = 0, bb = 0; st[i] = 0;
        while(aa<vec[id[i]].size() || bb<num[i]){
            if(aa==vec[id[i]].size()){
                p[i][st[i]++] = news[i][bb++];
            } else if(bb==num[i]){
                if(!out[i][vec[id[i]][aa].second]) p[i][st[i]++] = vec[id[i]][aa].first;
                aa++;
            } else if(vec[id[i]][aa].first<news[i][bb]){
                if(!out[i][vec[id[i]][aa].second]) p[i][st[i]++] = vec[id[i]][aa].first;
                aa++;
            } else {
                p[i][st[i]++] = news[i][bb++];
            }
        }
    }
    int g = 0;
    for(int i=0; i<st[0]; i++){
        while(g<st[1] && p[1][g]<p[0][i]){
            ans = min(ans,p[0][i] - p[1][g++]);
        }
        if(g==st[1]) break;
        ans = min(ans,p[1][g] - p[0][i]);
    }


    for(int i=0; i<2; i++){
        for(int j=0; j<step[i]; j++) out[i][who[i][j]] = false;
    }
    return ans;
}

Compilation message

potion.cpp: In function 'int question(int, int, int)':
potion.cpp:104:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         while(aa<vec[id[i]].size() || bb<num[i]){
      |               ~~^~~~~~~~~~~~~~~~~~
potion.cpp:105:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |             if(aa==vec[id[i]].size()){
      |                ~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 4 ms 14936 KB Output is correct
4 Correct 18 ms 19028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 33132 KB Output is correct
2 Correct 123 ms 33096 KB Output is correct
3 Correct 122 ms 31092 KB Output is correct
4 Correct 308 ms 35172 KB Output is correct
5 Correct 218 ms 28680 KB Output is correct
6 Correct 449 ms 43260 KB Output is correct
7 Correct 215 ms 33604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 32668 KB Output is correct
2 Correct 279 ms 44108 KB Output is correct
3 Correct 220 ms 37512 KB Output is correct
4 Correct 298 ms 43012 KB Output is correct
5 Correct 153 ms 33912 KB Output is correct
6 Correct 317 ms 42844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 16236 KB Output is correct
2 Correct 55 ms 16216 KB Output is correct
3 Correct 78 ms 16216 KB Output is correct
4 Correct 170 ms 16216 KB Output is correct
5 Correct 146 ms 16216 KB Output is correct
6 Correct 57 ms 15704 KB Output is correct
7 Correct 151 ms 15960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 4 ms 14936 KB Output is correct
4 Correct 4 ms 14936 KB Output is correct
5 Correct 18 ms 19028 KB Output is correct
6 Correct 155 ms 33132 KB Output is correct
7 Correct 123 ms 33096 KB Output is correct
8 Correct 122 ms 31092 KB Output is correct
9 Correct 308 ms 35172 KB Output is correct
10 Correct 218 ms 28680 KB Output is correct
11 Correct 449 ms 43260 KB Output is correct
12 Correct 215 ms 33604 KB Output is correct
13 Correct 131 ms 32668 KB Output is correct
14 Correct 279 ms 44108 KB Output is correct
15 Correct 220 ms 37512 KB Output is correct
16 Correct 298 ms 43012 KB Output is correct
17 Correct 153 ms 33912 KB Output is correct
18 Correct 317 ms 42844 KB Output is correct
19 Correct 27 ms 16236 KB Output is correct
20 Correct 55 ms 16216 KB Output is correct
21 Correct 78 ms 16216 KB Output is correct
22 Correct 170 ms 16216 KB Output is correct
23 Correct 146 ms 16216 KB Output is correct
24 Correct 57 ms 15704 KB Output is correct
25 Correct 151 ms 15960 KB Output is correct
26 Correct 229 ms 35408 KB Output is correct
27 Correct 298 ms 37568 KB Output is correct
28 Correct 286 ms 37392 KB Output is correct
29 Correct 322 ms 34676 KB Output is correct
30 Correct 441 ms 43188 KB Output is correct
31 Correct 409 ms 44116 KB Output is correct
32 Correct 425 ms 43200 KB Output is correct