#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 my_rand(time(NULL));
struct Node
{
Node *st, *dr;
int val, sz, prior;
};
struct tr
{
Node nil;
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 = nod->st->sz + 1 + nod->dr->sz;
return nod;
}
pair<Node*,Node*> split(Node *nod, int k)
{
if(nod==&nil)
{
return {&nil,&nil};
}
if(nod -> st -> sz >= k)
{
if(nod->st != &nil)
{
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)};
}
else
{
if(nod->dr != &nil)
{
nod->dr = new Node{nod->dr->st, nod->dr->dr, nod->dr->val, nod->dr->sz, nod->dr->prior};
}
auto t = split(nod -> dr, k - nod -> st -> sz - 1);
return {mod_fiu(nod,1,t.first),t.second};
}
}
Node *join(Node *st, Node *dr)
{
if(st==&nil)
{
return dr;
}
if(dr==&nil)
{
return st;
}
if(st->prior>=dr->prior)
{
if(st->dr != &nil)
{
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 != &nil)
{
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==&nil)
{
return 0;
}
if(val>=nod->val)
{
return nod->st->sz + 1 + caut(nod->dr,val);
}
return caut(nod->st,val);
}
void parcurg(Node *nod)
{
if(nod == &nil)
{
return;
}
l.push_back(nod->val);
parcurg(nod->st);
parcurg(nod->dr);
}
void update_per(int poz, int val, int nr)
{
pair<int, Node*> cur = {nr, &nil};
if(r.size() && r.back().second != &nil)
{
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{&nil,&nil,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);
pair<int,Node*> wtf = t[x].r.back();
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);
}
}
/*for(int i=1; i<=U; i++)
{
t[0].find_friends(i);
for(auto it : l)
{
cerr<<it<<' ';
}
cerr<<'\n';
}
*/
}
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
potion.cpp: In member function 'void tr::update_per(int, int, int)':
potion.cpp:131:77: warning: narrowing conversion of 'my_rand.std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::operator()()' from 'std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::result_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
131 | cur.second = join(join(t.first, new Node{&nil,&nil,poz,1,my_rand()}), t.second);
| ~~~~~~~^~
potion.cpp: In function 'void curseChanges(int, int*, int*)':
potion.cpp:187:29: warning: variable 'wtf' set but not used [-Wunused-but-set-variable]
187 | pair<int,Node*> wtf = t[x].r.back();
| ^~~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:228:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
228 | for(int i=0; i<vx.size(); i++)
| ~^~~~~~~~~~
potion.cpp:230:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
230 | while(poz < vy.size() && vy[poz] < vx[i])
| ~~~~^~~~~~~~~~~
potion.cpp:234:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
234 | if(poz >= vy.size())
| ~~~~^~~~~~~~~~~~
potion.cpp:241:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
241 | for(int i=0; i<vy.size(); i++)
| ~^~~~~~~~~~
potion.cpp:243:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
243 | while(poz < vx.size() && vx[poz] < vy[i])
| ~~~~^~~~~~~~~~~
potion.cpp:247:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
247 | if(poz >= vx.size())
| ~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
3 ms |
6232 KB |
Output is correct |
3 |
Correct |
3 ms |
6232 KB |
Output is correct |
4 |
Correct |
12 ms |
6952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
427 ms |
81256 KB |
Output is correct |
2 |
Correct |
442 ms |
81488 KB |
Output is correct |
3 |
Correct |
299 ms |
149752 KB |
Output is correct |
4 |
Runtime error |
545 ms |
262144 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
422 ms |
81400 KB |
Output is correct |
2 |
Correct |
1913 ms |
241352 KB |
Output is correct |
3 |
Correct |
1435 ms |
212364 KB |
Output is correct |
4 |
Correct |
2291 ms |
244808 KB |
Output is correct |
5 |
Correct |
600 ms |
96228 KB |
Output is correct |
6 |
Correct |
2493 ms |
243396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
9304 KB |
Output is correct |
2 |
Correct |
69 ms |
12632 KB |
Output is correct |
3 |
Correct |
107 ms |
11864 KB |
Output is correct |
4 |
Correct |
679 ms |
15600 KB |
Output is correct |
5 |
Correct |
661 ms |
12888 KB |
Output is correct |
6 |
Correct |
98 ms |
13148 KB |
Output is correct |
7 |
Correct |
578 ms |
15960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
3 ms |
6232 KB |
Output is correct |
3 |
Correct |
3 ms |
6232 KB |
Output is correct |
4 |
Correct |
3 ms |
6232 KB |
Output is correct |
5 |
Correct |
12 ms |
6952 KB |
Output is correct |
6 |
Correct |
427 ms |
81256 KB |
Output is correct |
7 |
Correct |
442 ms |
81488 KB |
Output is correct |
8 |
Correct |
299 ms |
149752 KB |
Output is correct |
9 |
Runtime error |
545 ms |
262144 KB |
Execution killed with signal 9 |
10 |
Halted |
0 ms |
0 KB |
- |