Submission #965491

# Submission time Handle Problem Language Result Execution time Memory
965491 2024-04-18T18:17:52 Z Huseyn123 The Potion of Great Power (CEOI20_potion) C++17
100 / 100
1922 ms 27144 KB
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int sz = 50;
vector<vector<int>> pre;
vector<int> e, vec, cnt;
int a[200001],b[200001];
vector<int> d[200001][2]; 

void init(int N, int D, int H[]) {
    pre.resize(N + 1);
    cnt.resize(N + 1, 0);
    for (int i = 0; i < N; i++) {
        vec.pb(H[i]);
    }
}

void curseChanges(int U, int A[], int B[]) {
    for (int i = 0; i < U; i++) {
        a[i] = A[i];
        b[i] = B[i];
    }
    for (int i = 0; i < U; i++) {
        pre[a[i]].pb(i);
        pre[b[i]].pb(i);
        if ((int)pre[a[i]].size() % sz == 0) {
            int h1 = pre[a[i]].size();
            int h = h1 - sz;
            e.clear();
            if (h != 0) {
                int h2 = 0;
                if (a[pre[a[i]][h - 1]] != a[i]) {
                    h2 = 1;
                }
                for (auto x : d[pre[a[i]][h - 1]][h2]) {
                    e.pb(x);
                    cnt[x]++;
                }
            }
            for (int j = h; j < h1; j++) {
                int h2 = a[pre[a[i]][j]];
                if (h2 == a[i]) {
                    h2 = b[pre[a[i]][j]];
                }
                cnt[h2]++;
                if (cnt[h2] == 1) {
                    e.pb(h2);
                }
            }
            for (auto x : e) {
                if (cnt[x] % 2) {
                    d[i][0].pb(x);
                }
                cnt[x] = 0;
            }
        }
        if ((int)pre[b[i]].size() % sz == 0) {
            int h1 = pre[b[i]].size();
            int h = h1 - sz;
            e.clear();
            if (h != 0) {
                int h2 = 0;
                if (a[pre[b[i]][h - 1]] != b[i]) {
                    h2 = 1;
                }
                for (auto x : d[pre[b[i]][h - 1]][h2]) {
                    e.pb(x);
                    cnt[x]++;
                }
            }
            for (int j = h; j < h1; j++) {
                int h2 = b[pre[b[i]][j]];
                if (h2 == b[i]) {
                    h2 = a[pre[b[i]][j]];
                }
                cnt[h2]++;
                if (cnt[h2] == 1) {
                    e.pb(h2);
                }
            }
            for (auto x : e) {
                if (cnt[x] % 2) {
                    d[i][1].pb(x);
                }
                cnt[x] = 0;
            }
        }
    }
}

int question(int x, int y, int v) {
    vector<int> d1, d2;
    if (v == 0) {
        return 1e9;
    }
    v--;
    auto it = upper_bound(pre[x].begin(), pre[x].end(), v);
    auto it1 = upper_bound(pre[y].begin(), pre[y].end(), v);
    if (it == pre[x].begin() || it1 == pre[y].begin()) {
        return 1e9;
    }
    int h = it - pre[x].begin();
    int h1 = it1 - pre[y].begin();
    int h2, h3;
    h2 = h - h % sz;
    h3 = h1 - h1 % sz;
    e.clear();
    if (h2 != 0) {
        int tp = 0;
        if (a[pre[x][h2 - 1]] != x) {
            tp = 1;
        }
        for (auto z : d[pre[x][h2 - 1]][tp]) {
            e.pb(z);
            cnt[z]++;
        }
    }
    for (int i = h2; i < h; i++) {
        int tp = a[pre[x][i]];
        if (tp == x) {
            tp = b[pre[x][i]];
        }
        cnt[tp]++;
        if (cnt[tp] == 1) {
            e.pb(tp);
        }
    }
    for (auto z : e) {
        if (cnt[z] % 2) {
            d1.pb(vec[z]);
        }
        cnt[z] = 0;
    }
    e.clear();
    if (h3 != 0) {
        int tp = 0;
        if (a[pre[y][h3 - 1]] != y) {
            tp = 1;
        }
        for (auto z : d[pre[y][h3 - 1]][tp]) {
            e.pb(z);
            cnt[z]++;
        }
    }
    for (int i = h3; i < h1; i++) {
        int tp = a[pre[y][i]];
        if (tp == y) {
            tp = b[pre[y][i]];
        }
        cnt[tp]++;
        if (cnt[tp] == 1) {
            e.pb(tp);
        }
    }
    for (auto z : e) {
        if (cnt[z] % 2) {
            d2.pb(vec[z]);
        }
        cnt[z] = 0;
    }
    if(d1.size()==0 || d2.size()==0){
    	return 1e9;
    }
    sort(d1.begin(),d1.end());
    sort(d2.begin(),d2.end());
    int res=1e9;
    int j=0;
    for(int i=0;i<(int)d1.size();i++){
    	while(j<(int)d2.size() && d1[i]>d2[j]){
    		j++;
    	}
    	if(j<(int)d2.size()){
    		res=min(res,d2[j]-d1[i]);
    	}
    }
    j=(int)d2.size()-1;
    for(int i=(int)d1.size()-1;i>=0;i--){
    	while(j>=0 && d1[i]<d2[j]){
    		j--;
    	}
    	if(j>=0){
    		res=min(res,d1[i]-d2[j]);
    	}
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10840 KB Output is correct
2 Correct 3 ms 10840 KB Output is correct
3 Correct 3 ms 10840 KB Output is correct
4 Correct 13 ms 14820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 21216 KB Output is correct
2 Correct 104 ms 20744 KB Output is correct
3 Correct 122 ms 19196 KB Output is correct
4 Correct 930 ms 20948 KB Output is correct
5 Correct 254 ms 16588 KB Output is correct
6 Correct 1922 ms 27144 KB Output is correct
7 Correct 373 ms 21252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 20704 KB Output is correct
2 Correct 700 ms 26228 KB Output is correct
3 Correct 448 ms 23676 KB Output is correct
4 Correct 796 ms 26976 KB Output is correct
5 Correct 154 ms 21400 KB Output is correct
6 Correct 775 ms 26984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 11864 KB Output is correct
2 Correct 79 ms 11860 KB Output is correct
3 Correct 120 ms 11872 KB Output is correct
4 Correct 563 ms 11700 KB Output is correct
5 Correct 531 ms 11608 KB Output is correct
6 Correct 84 ms 11096 KB Output is correct
7 Correct 455 ms 11608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10840 KB Output is correct
2 Correct 3 ms 10840 KB Output is correct
3 Correct 3 ms 10840 KB Output is correct
4 Correct 3 ms 10840 KB Output is correct
5 Correct 13 ms 14820 KB Output is correct
6 Correct 118 ms 21216 KB Output is correct
7 Correct 104 ms 20744 KB Output is correct
8 Correct 122 ms 19196 KB Output is correct
9 Correct 930 ms 20948 KB Output is correct
10 Correct 254 ms 16588 KB Output is correct
11 Correct 1922 ms 27144 KB Output is correct
12 Correct 373 ms 21252 KB Output is correct
13 Correct 119 ms 20704 KB Output is correct
14 Correct 700 ms 26228 KB Output is correct
15 Correct 448 ms 23676 KB Output is correct
16 Correct 796 ms 26976 KB Output is correct
17 Correct 154 ms 21400 KB Output is correct
18 Correct 775 ms 26984 KB Output is correct
19 Correct 27 ms 11864 KB Output is correct
20 Correct 79 ms 11860 KB Output is correct
21 Correct 120 ms 11872 KB Output is correct
22 Correct 563 ms 11700 KB Output is correct
23 Correct 531 ms 11608 KB Output is correct
24 Correct 84 ms 11096 KB Output is correct
25 Correct 455 ms 11608 KB Output is correct
26 Correct 472 ms 22208 KB Output is correct
27 Correct 768 ms 23676 KB Output is correct
28 Correct 847 ms 24028 KB Output is correct
29 Correct 838 ms 21404 KB Output is correct
30 Correct 1708 ms 27000 KB Output is correct
31 Correct 1523 ms 26616 KB Output is correct
32 Correct 1478 ms 26988 KB Output is correct