Submission #1314338

#TimeUsernameProblemLanguageResultExecution timeMemory
1314338Zbyszek99The Potion of Great Power (CEOI20_potion)C++20
100 / 100
1208 ms47520 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

int H[100001];
vector<vi> lists[100001];
vi changes_times[100001];
vector<pair<vector<pii>,vector<pii>>> changes[100001];
vi cur_list[100001];
int n,d;

vi add_elm(vi& x, vi a)
{
    vi ans;
    int p = 0;
    forall(it,x)
    {
        while(p < siz(a) && it > a[p])
        {
            ans.pb(a[p++]);
        }
        ans.pb(it);
    }    
    rep2(i,p,siz(a)-1) ans.pb(a[i]);
    return ans;
}

vi del_elm(vi& x, vi a)
{
    vi ans;
    int p = 0;
    forall(it,x) 
    {
        while(p < siz(a) && a[p] < it) p++;
        if(p == siz(a) || it != a[p]) ans.pb(it);
        else p++;
    }
    return ans;
}

void init(int N, int D, int H2[]) 
{
    n = N;
    d = D;
    rep(i,n) H[i] = H2[i];
    rep(i,n)
    {
        changes_times[i].pb(0);
        changes[i] = {{}};
        lists[i].pb({});
        cur_list[i] = {};
    }
}

void curseChanges(int U, int A[], int B[]) 
{
    map<pii,bool> is;
    rep(i,U)
    {
        if(A[i] < B[i]) swap(A[i],B[i]);
        if(is[{A[i],B[i]}])
        {
            cur_list[A[i]] = del_elm(cur_list[A[i]],{H[B[i]]});
            cur_list[B[i]] = del_elm(cur_list[B[i]],{H[A[i]]});
            changes[A[i]].back().ss.pb({H[B[i]],i+1});
            changes[B[i]].back().ss.pb({H[A[i]],i+1});
        }   
        else
        {
            cur_list[A[i]] = add_elm(cur_list[A[i]],{H[B[i]]});
            cur_list[B[i]] = add_elm(cur_list[B[i]],{H[A[i]]});    
            changes[B[i]].back().ff.pb({H[A[i]],i+1});
            changes[A[i]].back().ff.pb({H[B[i]],i+1});    
        }
        if(siz(changes[A[i]].back().ff)+siz(changes[A[i]].back().ss) == d)
        {
            if(is[{A[i],B[i]}]) changes[A[i]].back().ss.pop_back();
            else changes[A[i]].back().ff.pop_back();
            changes[A[i]].pb({});
            changes_times[A[i]].pb(i+1);
            lists[A[i]].pb(cur_list[A[i]]);
        }
        if(siz(changes[B[i]].back().ff)+siz(changes[B[i]].back().ss) == d)
        {
            if(is[{A[i],B[i]}]) changes[B[i]].back().ss.pop_back();
            else changes[B[i]].back().ff.pop_back();
            changes[B[i]].pb({});
            changes_times[B[i]].pb(i+1);
            lists[B[i]].pb(cur_list[B[i]]);
        }
        is[{A[i],B[i]}] ^= 1;
    }
    rep(i,n) forall(it,changes[i])
    {
        sort(all(it.ff));
        sort(all(it.ss));
    }
}

int question(int x, int y, int v) 
{
    int ans = 1e9;
    vi x_list;
    vi y_list;
    int l = 0;
    int r = siz(changes[x])-1;
    int ind = 0;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(changes_times[x][mid] <= v)
        {
            ind = mid;
            l = mid+1;
        }
        else 
        {
            r = mid-1;
        }
    }
    x_list = lists[x][ind];
    vi add;
    vi del;
    forall(it,changes[x][ind].ff) if(it.ss <= v) add.pb(it.ff);
    forall(it,changes[x][ind].ss) if(it.ss <= v) del.pb(it.ff);
    x_list = add_elm(x_list,add);
    x_list = del_elm(x_list,del);
    l = 0;
    r = siz(changes[y])-1;
    ind = 0;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(changes_times[y][mid] <= v)
        {
            ind = mid;
            l = mid+1;
        }
        else 
        {
            r = mid-1;
        }
    }
    y_list = lists[y][ind];
    add = {};
    del = {};
    forall(it,changes[y][ind].ff) if(it.ss <= v) add.pb(it.ff);
    forall(it,changes[y][ind].ss) if(it.ss <= v) del.pb(it.ff);
    y_list = add_elm(y_list,add);
    y_list = del_elm(y_list,del);
    if(siz(x_list) == 0) return ans;
    int ptr = 0;
    forall(it,y_list)
    {
        while(ptr+1 < siz(x_list) && x_list[ptr+1] < it)
        {
            ptr++;
        }
        ans = min(ans,abs(it-x_list[ptr]));
        if(ptr != siz(x_list)-1) ans = min(ans,abs(it-x_list[ptr+1]));
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...