답안 #643230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643230 2022-09-21T14:47:36 Z Vladth11 The Potion of Great Power (CEOI20_potion) C++17
38 / 100
3000 ms 42956 KB
#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>

//#define debug(x) cerr << #x << " " << x << "\n"

#pragma GCC optimize("Ofast")

using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int bSize = 30;
const int bits = 17;
const int NMAX = 100000;
const int VMAX = NMAX * 89;

int a[NMAX], n;
int u;

struct BLOCK {
    int time;
    vector <int> v;
};

class DS {
public:
    vector <BLOCK> blocks;
    vector <pii> changes;
    unordered_set <int> vecini;
    void baga(int x, int cand) {
        if(vecini.find(x) != vecini.end()) {
            vecini.erase(x);
            changes.push_back({x, cand});
        } else {
            vecini.insert(x);
            changes.push_back({x, cand});
        }
        if(changes.size() % bSize == 0) {
            BLOCK nou;
            nou.v.clear();
            nou.time = cand;
            for(auto x : vecini) {
                nou.v.push_back(x);
            }
            blocks.push_back(nou);
        }
    }

} ds[NMAX];

void init(int N, int D, int H[]) {
    n = N;
    for(int i = 0; i < N; i++) {
        a[i] = H[i];
    }
}

void curseChanges(int U, int A[], int B[]) {
    u = U;
    for(int i = 0; i < U; i++) {
        int timp = i + 1;
        ds[A[i]].baga(B[i], timp);
        ds[B[i]].baga(A[i], timp);
    }
}

bool cmp(int A, int B) {
    if(a[A] != a[B])
    return a[A] < a[B];
    return A < B;
}

int impX[NMAX];
int impY[NMAX];
int intrebare;

int question(int x, int y, int v) {
    int r = -1;
    intrebare++;
    int timpX = 0, timpY = 0;
    int pas = (1 << bits);
    while(pas) {
        if(r + pas < ds[x].blocks.size() && ds[x].blocks[r + pas].time <= v)
            r += pas;
        pas /= 2;
    }
    vector <int> parcurgere[2];
    unordered_set <int> stX;
    if(r != -1) {
        timpX = ds[x].blocks[r].time + 1;
        parcurgere[0] = ds[x].blocks[r].v;
    }
    r = -1;
    pas = (1 << bits);
    while(pas) {
        if(r + pas < ds[y].blocks.size() && ds[y].blocks[r + pas].time <= v)
            r += pas;
        pas /= 2;
    }
    if(r != -1) {
        timpY = ds[y].blocks[r].time + 1;
        parcurgere[1] = ds[y].blocks[r].v;
    }
    r = -1, pas = (1 << bits);
    while(pas) {
        if(r + pas < ds[x].changes.size() && ds[x].changes[r + pas].second < timpX)
            r += pas;
        pas /= 2;
    }
    r++;
    for(; r < ds[x].changes.size() && ds[x].changes[r].second <= v; r++) {
        pii change = ds[x].changes[r];
        parcurgere[0].push_back(change.first);
    }
    r = -1, pas = (1 << bits);
    while(pas) {
        if(r + pas < ds[y].changes.size() && ds[y].changes[r + pas].second < timpY)
            r += pas;
        pas /= 2;
    }
    r++;
    for(; r < ds[y].changes.size() && ds[y].changes[r].second <= v; r++) {
        pii change = ds[y].changes[r];
        parcurgere[1].push_back(change.first);
    }
    if(!parcurgere[0].size() || !parcurgere[1].size()) return 1e9;
    sort(parcurgere[0].begin(), parcurgere[0].end(), cmp);
    sort(parcurgere[1].begin(), parcurgere[1].end(), cmp);
    int cnt = 0, i;
    for(i = 0; i < parcurgere[0].size(); i++) {
        if(i != 0 && parcurgere[0][i - 1] != parcurgere[0][i]) {
            if(cnt % 2 == 0) {
                impX[parcurgere[0][i - 1]] = intrebare;
            }
            cnt = 0;
        }
        cnt++;
    }
    if(cnt % 2 == 0) {
        impX[parcurgere[0][i - 1]] = intrebare;
    }
    cnt = 0;
    for(i = 0; i < parcurgere[1].size(); i++) {
        if(i != 0 && parcurgere[1][i - 1] != parcurgere[1][i]) {
            if(cnt % 2 == 0) {
                impY[parcurgere[1][i - 1]] = intrebare;
            }
            cnt = 0;
        }
        cnt++;
    }
    if(cnt % 2 == 0) {
        impY[parcurgere[1][i - 1]] = intrebare;
    }
    cnt = 0;
    int minim = 1e9;
    i = 0;
    int j = 0;
    while(i < parcurgere[0].size() && j < parcurgere[1].size()) {
        if(impX[parcurgere[0][i]] == intrebare){
            i++;
            continue;
        }
        if(impY[parcurgere[1][j]] == intrebare){
            j++;
            continue;
        }
        minim = min(minim, abs(a[parcurgere[0][i]] - a[parcurgere[1][j]]));
        if(a[parcurgere[0][i]] < a[parcurgere[1][j]])
            i++;
        else
            j++;
    }
    return minim;
}

Compilation message

potion.cpp: In function 'int question(int, int, int)':
potion.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<BLOCK>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         if(r + pas < ds[x].blocks.size() && ds[x].blocks[r + pas].time <= v)
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
potion.cpp:99:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<BLOCK>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |         if(r + pas < ds[y].blocks.size() && ds[y].blocks[r + pas].time <= v)
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
potion.cpp:109:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |         if(r + pas < ds[x].changes.size() && ds[x].changes[r + pas].second < timpX)
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:114:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |     for(; r < ds[x].changes.size() && ds[x].changes[r].second <= v; r++) {
      |           ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         if(r + pas < ds[y].changes.size() && ds[y].changes[r + pas].second < timpY)
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:125:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |     for(; r < ds[y].changes.size() && ds[y].changes[r].second <= v; r++) {
      |           ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:133:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |     for(i = 0; i < parcurgere[0].size(); i++) {
      |                ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:146:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |     for(i = 0; i < parcurgere[1].size(); i++) {
      |                ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:162:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |     while(i < parcurgere[0].size() && j < parcurgere[1].size()) {
      |           ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:162:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |     while(i < parcurgere[0].size() && j < parcurgere[1].size()) {
      |                                       ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10704 KB Output is correct
2 Correct 8 ms 10704 KB Output is correct
3 Correct 8 ms 10704 KB Output is correct
4 Correct 20 ms 11564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 306 ms 41840 KB Output is correct
2 Correct 318 ms 41716 KB Output is correct
3 Correct 240 ms 18320 KB Output is correct
4 Correct 1589 ms 31776 KB Output is correct
5 Correct 471 ms 36312 KB Output is correct
6 Execution timed out 3036 ms 42040 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 292 ms 41640 KB Output is correct
2 Correct 2309 ms 33772 KB Output is correct
3 Correct 1453 ms 37324 KB Output is correct
4 Correct 2766 ms 41816 KB Output is correct
5 Correct 504 ms 42956 KB Output is correct
6 Execution timed out 3076 ms 42076 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 12560 KB Output is correct
2 Correct 128 ms 11060 KB Output is correct
3 Correct 206 ms 11088 KB Output is correct
4 Correct 910 ms 12336 KB Output is correct
5 Correct 849 ms 12660 KB Output is correct
6 Correct 109 ms 11620 KB Output is correct
7 Correct 798 ms 11376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10440 KB Output is correct
2 Correct 7 ms 10704 KB Output is correct
3 Correct 8 ms 10704 KB Output is correct
4 Correct 8 ms 10704 KB Output is correct
5 Correct 20 ms 11564 KB Output is correct
6 Correct 306 ms 41840 KB Output is correct
7 Correct 318 ms 41716 KB Output is correct
8 Correct 240 ms 18320 KB Output is correct
9 Correct 1589 ms 31776 KB Output is correct
10 Correct 471 ms 36312 KB Output is correct
11 Execution timed out 3036 ms 42040 KB Time limit exceeded
12 Halted 0 ms 0 KB -