#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
///Dremix10's template
template<typename T>
struct SPARSE{
vector<vector<T> > sparse;
vector<int> log;
const int maxPow = 20;
int N;
void init(int n){
sparse.assign(n,vector<T>(maxPow));
log.assign(n+1,0);
N = n;
for(int i = 2;i <= n;i++)
log[i] = log[i/2] + 1;
}
// supports 0-indexing
void build(vector<T> arr){
int i,j;
for(i = 0;i < N ;i++)
sparse[i][0] = arr[i];
for(j = 1;j < maxPow;j++)
for(i = 0;i + (1<<j) <= N;i++)
sparse[i][j] = min(sparse[i][j-1],sparse[i+(1<<(j-1))][j-1]);
}
T query(int l, int r){
int k = log[r-l+1];
return min(sparse[l][k],sparse[r-(1<<k)+1][k]);
}
};
vector<iii>rang[100005];
SPARSE<ll>s[100005];
ll n;
ll h[100005];
void init(int N, int D, int H[]){
n = N;
f(i,0,n){
h[i] = H[i];
}
}
void curseChanges(int U, int A[], int B[]) {
ll u = U;
vector<ii>exo[n];
f(i,0,u){
ll a = A[i],b = B[i];
exo[a].pb(ii(b,i+1));
exo[b].pb(ii(a,i+1));
}
ll last[n];
memset(last,-1,sizeof last);
f(i,0,n){
set<ll>ex;
for(auto x:exo[i]){
if(last[x.F] == -1){
last[x.F] = x.S;
ex.insert(x.F);
}
else{
rang[i].pb(iii(ii(x.S - 1,last[x.F]),h[x.F]));
last[x.F] = -1;
ex.erase(x.F);
}
}
for(auto x:ex){
rang[i].pb(iii(ii(u + 5,last[x]),h[x]));
last[x] = -1;
}
sort(all(rang[i]));
s[i].init(rang[i].size());
vector<ll>vale;
for(auto x:rang[i]){
vale.pb(x.F.S);
}
s[i].build(vale);
}
}
int question(int x, int y, int v) {
ll ans = 1e9;
vector<ii>kame;
ll L;
ll pos = lower_bound(all(rang[x]),iii(ii(v,0),0)) - rang[x].begin();
if(rang[x].size() > 750){
f(j,pos,rang[x].size()){
if(rang[x][j].F.S <= v){
kame.pb(ii(rang[x][j].S,0));
}
}
}
else{
L = pos;
while(1){
ll l = L,r = rang[x].size() - 1;
pos = r + 1;
while(l <= r){
ll mid = (l+r)/2;
if(s[x].query(L,mid) <= v){
r = mid - 1;
pos = min(pos,mid);
}
else{
l = mid + 1;
}
}
if(pos < rang[x].size()){
kame.pb(ii(rang[x][pos].S,0));
}
else{
break;
}
L = pos + 1;
}
}
pos = lower_bound(all(rang[y]),iii(ii(v,0),0)) - rang[y].begin();
if(rang[y].size() > 750){
f(j,pos,rang[y].size()){
if(rang[y][j].F.S <= v){
kame.pb(ii(rang[y][j].S,1));
}
}
}
else{
L = pos;
while(1){
ll l = L,r = rang[y].size() - 1;
pos = r + 1;
while(l <= r){
ll mid = (l+r)/2;
if(s[y].query(L,mid) <= v){
r = mid - 1;
pos = min(pos,mid);
}
else{
l = mid + 1;
}
}
if(pos < rang[y].size()){
kame.pb(ii(rang[y][pos].S,1));
}
else{
break;
}
L = pos + 1;
}
}
sort(all(kame));
f(i,0,(ll)kame.size()-1){
if(kame[i].S != kame[i+1].S){
ans = min(ans,kame[i+1].F - kame[i].F);
}
}
return ans;
}
Compilation message
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:9:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | #define f(i,a,b) for(ll i = a;i < b;i++)
......
112 | f(j,pos,rang[x].size()){
| ~~~~~~~~~~~~~~~~~~~~
potion.cpp:112:9: note: in expansion of macro 'f'
112 | f(j,pos,rang[x].size()){
| ^
potion.cpp:133:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | if(pos < rang[x].size()){
| ~~~~^~~~~~~~~~~~~~~~
potion.cpp:9:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | #define f(i,a,b) for(ll i = a;i < b;i++)
......
144 | f(j,pos,rang[y].size()){
| ~~~~~~~~~~~~~~~~~~~~
potion.cpp:144:9: note: in expansion of macro 'f'
144 | f(j,pos,rang[y].size()){
| ^
potion.cpp:165:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
165 | if(pos < rang[y].size()){
| ~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8528 KB |
Output is correct |
2 |
Correct |
6 ms |
8528 KB |
Output is correct |
3 |
Correct |
6 ms |
8528 KB |
Output is correct |
4 |
Correct |
28 ms |
15116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
325 ms |
77888 KB |
Output is correct |
2 |
Correct |
320 ms |
77772 KB |
Output is correct |
3 |
Correct |
194 ms |
46464 KB |
Output is correct |
4 |
Correct |
1899 ms |
56212 KB |
Output is correct |
5 |
Correct |
522 ms |
70884 KB |
Output is correct |
6 |
Correct |
2595 ms |
57816 KB |
Output is correct |
7 |
Correct |
578 ms |
58108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
77836 KB |
Output is correct |
2 |
Correct |
1446 ms |
48468 KB |
Output is correct |
3 |
Correct |
1368 ms |
58184 KB |
Output is correct |
4 |
Correct |
1362 ms |
57724 KB |
Output is correct |
5 |
Correct |
446 ms |
77256 KB |
Output is correct |
6 |
Correct |
1465 ms |
57832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
11984 KB |
Output is correct |
2 |
Correct |
104 ms |
10448 KB |
Output is correct |
3 |
Correct |
193 ms |
10448 KB |
Output is correct |
4 |
Correct |
1109 ms |
11344 KB |
Output is correct |
5 |
Correct |
1065 ms |
11768 KB |
Output is correct |
6 |
Correct |
118 ms |
11216 KB |
Output is correct |
7 |
Correct |
692 ms |
10516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8144 KB |
Output is correct |
2 |
Correct |
6 ms |
8528 KB |
Output is correct |
3 |
Correct |
6 ms |
8528 KB |
Output is correct |
4 |
Correct |
6 ms |
8528 KB |
Output is correct |
5 |
Correct |
28 ms |
15116 KB |
Output is correct |
6 |
Correct |
325 ms |
77888 KB |
Output is correct |
7 |
Correct |
320 ms |
77772 KB |
Output is correct |
8 |
Correct |
194 ms |
46464 KB |
Output is correct |
9 |
Correct |
1899 ms |
56212 KB |
Output is correct |
10 |
Correct |
522 ms |
70884 KB |
Output is correct |
11 |
Correct |
2595 ms |
57816 KB |
Output is correct |
12 |
Correct |
578 ms |
58108 KB |
Output is correct |
13 |
Correct |
289 ms |
77836 KB |
Output is correct |
14 |
Correct |
1446 ms |
48468 KB |
Output is correct |
15 |
Correct |
1368 ms |
58184 KB |
Output is correct |
16 |
Correct |
1362 ms |
57724 KB |
Output is correct |
17 |
Correct |
446 ms |
77256 KB |
Output is correct |
18 |
Correct |
1465 ms |
57832 KB |
Output is correct |
19 |
Correct |
57 ms |
11984 KB |
Output is correct |
20 |
Correct |
104 ms |
10448 KB |
Output is correct |
21 |
Correct |
193 ms |
10448 KB |
Output is correct |
22 |
Correct |
1109 ms |
11344 KB |
Output is correct |
23 |
Correct |
1065 ms |
11768 KB |
Output is correct |
24 |
Correct |
118 ms |
11216 KB |
Output is correct |
25 |
Correct |
692 ms |
10516 KB |
Output is correct |
26 |
Correct |
959 ms |
48964 KB |
Output is correct |
27 |
Correct |
1727 ms |
58176 KB |
Output is correct |
28 |
Correct |
1747 ms |
69704 KB |
Output is correct |
29 |
Correct |
1601 ms |
56264 KB |
Output is correct |
30 |
Correct |
2465 ms |
57684 KB |
Output is correct |
31 |
Correct |
2435 ms |
48292 KB |
Output is correct |
32 |
Correct |
2288 ms |
58064 KB |
Output is correct |