제출 #1159290

#제출 시각아이디문제언어결과실행 시간메모리
1159290KluydQThe Potion of Great Power (CEOI20_potion)C++20
100 / 100
2517 ms73328 KiB
#include <bits/stdc++.h>

#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
//#define int long long
#define ll long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
 
using namespace std;

const int N = 2e5 + 123;
const int inf = 1e9;
const int mod = 1e9 + 7;
const double eps = 1e-13;
 
int a[N], b[N], v[N], u[N], obr[N], n, m, k, z, x, y, w, ans, tp;
set <int> st[2];
vector <int> v1, v2;
map <pii, int> mp;
vector <int> ord;

mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
 
int rand( int l, int r )
{
    uniform_int_distribution <int> uid( l, r );
    return uid( rng );
}
bool cmp( int i, int j )
{
	if( a[i] < a[j] || (a[i] == a[j] && i < j) ) return 1;
	return 0;
}
vector <pii> t[4 * N];

void update( int l, int r, pii val, int v = 1, int tl = 0, int tr = m - 1 )
{
	if( l > tr || tl > r ) return;
	if( l <= tl && tr <= r ) { t[v].pb(val); return; };
	
	int tm = tl + tr >> 1;
	
	update( l, r, val, v + v, tl, tm );
	update( l, r, val, v + v + 1, tm + 1, tr );
}
void srt( int v = 1, int tl = 0, int tr = m - 1 )
{
	sort( all( t[v] ));
	
	if( tl < tr )
	{
		int tm = tl + tr >> 1;
		
		srt( v + v, tl, tm );
		srt( v + v + 1, tm + 1, tr );
	}
}
void get( int pos, int who, int tp, int v = 1, int tl = 0, int tr = m - 1 )
{
	int le = 0, ri = sz(t[v]) - 1, res = 0;
	
	while( le <= ri )
	{
		int md = le + ri >> 1;
		
		if( t[v][md].F >= who ) ri = md - 1, res = md;
		else le = md + 1;
	}
	if( sz(t[v]) && t[v][res].F == who )
	{
		while( res < sz(t[v]) && t[v][res].F == who )
		{
			if( tp == 0 ) v1.pb( t[v][res].S );
			if( tp == 1 ) v2.pb( t[v][res].S );
			
			res ++;
		}
	}
	if( tl < tr )
	{
		int tm = (tl + tr) >> 1;
		
		if( pos <= tm ) get( pos, who, tp, v + v, tl, tm );
		else get( pos, who, tp, v + v + 1, tm + 1, tr );
	}
}
int question( int X, int Y, int V )
{
	X = b[X], Y = b[Y];
	v1.clear(), v2.clear();
	
	get( V - 1, X, 0 );
	get( V - 1, Y, 1 );
	
	sort( all(v1), cmp );
	sort( all(v2), cmp );
	
	x = 0, y = 0;
	int res = 1e9;
	
	while( x < sz(v1) && y < sz(v2) )
	{
		res = min( res, abs( a[v2[y]] - a[v1[x]] ));
		
		if( a[v1[x]] > a[v2[y]] && y < sz(v2) ) y ++;
		else x ++;
	}
	return res;
}
void upd( int l, int r, int i )
{
	update( l, r, {v[i], u[i]} );
	update( l, r, {u[i], v[i]} );
}
void curseChanges( int U, int A[], int B[] )
{
	m = U;
	FOR( i, 0, m - 1, 1 ) v[i] = A[i], u[i] = B[i];
	
	FOR( i, 0, m - 1, 1 )
	{
		v[i] = b[v[i]], u[i] = b[u[i]];
		if( v[i] > u[i] ) swap( v[i], u[i] );
		mp[{v[i], u[i]}] = -1;
	}
	FOR( i, 0, m - 1, 1 )
	{
		if( mp[{ v[i], u[i] }] == -1 ) mp[{ v[i], u[i] }] = i;
		else upd( mp[{ v[i], u[i] }], i - 1, i ), mp[{ v[i], u[i] }] = -1;
	}
	FORR( i, m - 1, 0, 1 )
	{
		if( mp[{ v[i], u[i] }] != -1 ) upd( mp[{ v[i], u[i] }], m - 1, i ), mp[{ v[i], u[i] }] = -1;
	}
	srt();
}
void init( int NN, int D, int H[] )
{
	respagold;
	
	n = NN, w = D;
	FOR( i, 0, n - 1, 1 ) a[i] = H[i], ord.pb(i);
	
	sort( all(ord), cmp );
	FOR( i, 0, n - 1, 1 ) a[i] = H[ord[i]], b[ord[i]] = i, obr[i] = ord[i];
}
/*
6 5 4
2 42 1000 54 68 234
11
0 1
2 0
3 4
3 5
3 5
1 3
5 3
0 5
3 0
1 3
3 5
0 3 4
3 0 8
0 5 5
3 0 11
*/
#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...