This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5;
const int oo = 1e9;
int n;
int h[nmax + 5];
set<pair<int,int>> s;
vector<int> l;
mt19937_64 my_rand(time(NULL));
struct Node
{
Node *st, *dr;
int val;
short int sz;
long long prior;
};
struct tr
{
vector<pair<int,Node*>> r;
Node *mod_fiu(Node *nod, int care, Node *f)
{
if(care==0)
{
nod -> st = f;
}
else
{
nod -> dr = f;
}
nod -> sz = 1;
if(nod->st)
{
nod->sz += nod->st->sz;
}
if(nod->dr)
{
nod->sz += nod->dr->sz;
}
return nod;
}
pair<Node*,Node*> split(Node *nod, int k)
{
if(!nod)
{
return {nullptr, nullptr};
}
if(k==0 || (nod->st && nod->st->sz >= k))
{
if(nod->st)
{
nod->st = new Node{nod->st->st, nod->st->dr, nod->st->val, nod->st->sz, nod->st->prior};
auto t = split(nod -> st, k);
return {t.first,mod_fiu(nod,0,t.second)};
}
return {nullptr, mod_fiu(nod,0,nullptr)};
}
else
{
if(nod->dr)
{
nod->dr = new Node{nod->dr->st, nod->dr->dr, nod->dr->val, nod->dr->sz, nod->dr->prior};
if(nod->st)
{
auto t = split(nod -> dr, k - nod->st->sz - 1);
return {mod_fiu(nod,1,t.first),t.second};
}
auto t = split(nod -> dr, k - 1);
return {mod_fiu(nod,1,t.first),t.second};
}
return {mod_fiu(nod,1,nullptr), nullptr};
}
}
Node *join(Node *st, Node *dr)
{
if(!st)
{
return dr;
}
if(!dr)
{
return st;
}
if(st->prior>=dr->prior)
{
if(st->dr)
{
st->dr = new Node{st->dr->st, st->dr->dr, st->dr->val, st->dr->sz, st->dr->prior};
}
return mod_fiu(st,1,join(st->dr,dr));
}
else
{
if(dr->st)
{
dr->st = new Node{dr->st->st, dr->st->dr, dr->st->val, dr->st->sz, dr->st->prior};
}
return mod_fiu(dr,0,join(st,dr->st));
}
}
int caut(Node *nod, int val)
{
if(nod==nullptr)
{
return 0;
}
if(val>=nod->val)
{
if(nod->st)
{
return nod->st->sz + 1 + caut(nod->dr,val);
}
return 1 + caut(nod->dr,val);
}
else
{
return caut(nod->st,val);
}
}
void parcurg(Node *nod)
{
if(nod == nullptr)
{
return;
}
l.push_back(nod->val);
if(nod->st)
{
parcurg(nod->st);
}
if(nod->dr)
{
parcurg(nod->dr);
}
}
void update_per(int poz, int val, int nr)
{
pair<int, Node*> cur = {nr, nullptr};
if(r.size() && r.back().second)
{
cur = {nr,new Node{r.back().second->st,r.back().second->dr,r.back().second->val,r.back().second->sz,r.back().second->prior}};
}
int p = caut(cur.second, poz);
if(val==0)
{
auto t = split(cur.second, p - 1);
auto t2 = split(t.second, 1);
cur.second = join(t.first,t2.second);
}
else
{
auto t = split(cur.second, p);
cur.second = join(join(t.first, new Node{nullptr,nullptr,poz,1,my_rand()}), t.second);
}
r.push_back(cur);
}
void find_friends(int v)
{
l.clear();
int st = 0;
int dr = r.size() - 1;
int poz = -1;
while(st<=dr)
{
int mij = (st + dr) >> 1;
if(r[mij].first <= v)
{
poz = mij;
st = mij + 1;
}
else
{
dr = mij - 1;
}
}
if(poz==-1)
{
return;
}
parcurg(r[poz].second);
}
};
tr t[nmax + 5];
void init(int N, int D, int H[])
{
n = N;
for(int i=0; i<n; i++)
{
h[i] = H[i];
}
}
void curseChanges(int U, int A[], int B[])
{
for(int i=0; i<U; i++)
{
int x = A[i];
int y = B[i];
if(x > y)
{
swap(x,y);
}
if(s.find({x,y})==s.end())
{
s.insert({x,y});
t[x].update_per(y,+1,i + 1);
t[y].update_per(x,+1,i + 1);
}
else
{
auto it = s.find({x,y});
s.erase(it);
t[x].update_per(y,0,i + 1);
t[y].update_per(x,0,i + 1);
}
}
}
int question(int x, int y, int v)
{
t[x].find_friends(v);
vector<int> vx;
for(auto it : l)
{
vx.push_back(h[it]);
}
t[y].find_friends(v);
vector<int> vy;
for(auto it : l)
{
vy.push_back(h[it]);
}
sort(vx.begin(),vx.end());
sort(vy.begin(),vy.end());
int rez = oo;
int poz = 0;
for(int i=0; i<vx.size(); i++)
{
while(poz < vy.size() && vy[poz] < vx[i])
{
++poz;
}
if(poz >= vy.size())
{
break;
}
rez = min(rez, vy[poz] - vx[i]);
}
poz = 0;
for(int i=0; i<vy.size(); i++)
{
while(poz < vx.size() && vx[poz] < vy[i])
{
++poz;
}
if(poz >= vx.size())
{
break;
}
rez = min(rez, vx[poz] - vy[i]);
}
return rez;
}
Compilation message (stderr)
potion.cpp: In member function 'void tr::update_per(int, int, int)':
potion.cpp:160:83: warning: narrowing conversion of 'my_rand.std::mersenne_twister_engine<long unsigned int, 64, 312, 156, 31, 13043109905998158313, 29, 6148914691236517205, 17, 8202884508482404352, 37, 18444473444759240704, 43, 6364136223846793005>::operator()()' from 'std::mersenne_twister_engine<long unsigned int, 64, 312, 156, 31, 13043109905998158313, 29, 6148914691236517205, 17, 8202884508482404352, 37, 18444473444759240704, 43, 6364136223846793005>::result_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
160 | cur.second = join(join(t.first, new Node{nullptr,nullptr,poz,1,my_rand()}), t.second);
| ~~~~~~~^~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:246:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
246 | for(int i=0; i<vx.size(); i++)
| ~^~~~~~~~~~
potion.cpp:248:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
248 | while(poz < vy.size() && vy[poz] < vx[i])
| ~~~~^~~~~~~~~~~
potion.cpp:252:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
252 | if(poz >= vy.size())
| ~~~~^~~~~~~~~~~~
potion.cpp:259:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
259 | for(int i=0; i<vy.size(); i++)
| ~^~~~~~~~~~
potion.cpp:261:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
261 | while(poz < vx.size() && vx[poz] < vy[i])
| ~~~~^~~~~~~~~~~
potion.cpp:265:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
265 | if(poz >= vx.size())
| ~~~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |