이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
//#define left id<<1
//#define right id<<1|1
#define re exit(0);
#define _lower(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()+1
const int mod = 1e9+7 ;
const int INF = 1e9 ;
const int LOG = 18 ;
typedef vector<int> vi ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
typedef vector<ll> vl ;
typedef pair<ll,ll> pll ;
typedef vector<pll> vll ;
void add ( int&a , int b ) { if ((a+=b) > mod ) a -= mod ; }
void sub ( int&a , int b ) { if ((a-=b) < 0 ) a += mod ; }
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
void rf ( )
{
freopen ("bai1.inp","r",stdin) ;
// freopen ("bai1.out","w",stdout) ;
}
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow ( a , n / 2 ) ;
if ( n % 2 ) return ( 1ll * res * res % mod * a % mod ) ;
else return ( 1ll * res * res % mod ) ;
}
int n , m , nq ;
vi point [maxn] ;
pii E [maxn] ;
vi adjList [maxn] ;
int fac [maxn] ;
void mapping ()
{
vi v ;
map <int,int> mp ;
for ( int i = 2 ; i <= n ; i ++ ) for ( auto x : point [i] ) v . pb (x) ;
sort (v.begin(),v.end()) ;
for ( int i = 2 ; i <= n ; i ++ )
{
for ( auto &x : point [i] )
{
int _x = _lower(v,x) + mp [_lower(v,x)] ; mp [_lower(v,x)] ++ ;
fac [_x] = x ;
x = _x ;
}
}
}
int dep [maxn] ;
int num_gold [maxn] ;
int par [maxn][20] ;
void dfs ( int u = 1 )
{
for ( auto v : adjList [u] )
{
if ( v == par [u][0] ) continue ;
par [v][0] = u ; dep [v] = dep [u] + 1 ;
for ( int i = 1 ; i <= LOG ; i ++ ) par [v][i] = par[par[v][i-1]][i-1] ;
dfs (v) ;
}
}
int lca ( int u , int v )
{
if ( dep [u] < dep [v] ) swap (u,v) ;
int h = dep [u] - dep [v] ;
for ( int i = LOG ; i >= 0 ; i -- ) if ( h >> i & 1 ) u = par [u][i] ;
if ( u == v ) return u ;
for ( int i = LOG ; i >= 0 ; i -- ) if ( par [u][i] != par [v][i] ) u = par [u][i] , v = par [v][i] ;
return par [u][0] ;
}
struct shape {
int left , right ;
ll silver ;
int num_gold ;
} T [maxn*30] ; int root [maxn] , cnt = 0 ;
int build ( int l , int r )
{
int New = ++ cnt ;
if ( l == r ) return New ;
int mid = (l+r)/2 ;
T [New].left = build (l,mid) ;
T [New].right = build (mid+1,r) ;
return New ;
}
int update ( int id , int l , int r , int pos , int silver )
{
if ( l > pos || r < pos ) return id ;
int New = ++ cnt ;
T [New] = T [id] ;
if ( l == r )
{
T [New].silver = silver ;
T [New].num_gold = 1 ;
return New ;
}
int mid = (l+r)/2 ;
T [New].left = update (T[id].left,l,mid,pos,silver) ;
T [New].right = update (T[id].right,mid+1,r,pos,silver) ;
T [New].silver = T[T[New].left].silver + T[T[New].right].silver ;
T [New].num_gold = T[T[New].left].num_gold + T[T[New].right].num_gold ;
return New ;
}
const int N = 2e5 ;
int loss_max = 0 ;
void get ( int rootA , int rootB , int rootLCA , ll silver , int l = 1 , int r = N )
{
if ( T[rootA].silver+T[rootB].silver-2*T[rootLCA].silver <= silver )
{
loss_max += T[rootA].num_gold+T[rootB].num_gold-2*T[rootLCA].num_gold ;
return ;
}
if ( l == r ) return ;
int mid = (l+r)/2 ;
ll sum = T[T[rootA].left].silver + T[T[rootB].left].silver - 2*T[T[rootLCA].left].silver ;
if ( sum <= silver )
{
loss_max += T[T[rootA].left].num_gold + T[T[rootB].left].num_gold - 2*T[T[rootLCA].left].num_gold ;
get (T[rootA].right,T[rootB].right,T[rootLCA].right,silver-sum,mid+1,r) ;
}
else get (T[rootA].left,T[rootB].left,T[rootLCA].left,silver,l,mid) ;
}
void dfs_addvalue ( int u = 1 )
{
for ( auto v : adjList [u] )
{
if ( v == par [u][0] ) continue ;
root [v] = root [u] ;
for ( auto x : point [v] )
{
root [v] = update (root[v],1,N,x,fac[x]) ;
}
num_gold [v] = num_gold [u] + (int)point[v].size () ;
dfs_addvalue (v) ;
}
}
void build ()
{
mapping () ;
root [1] = build (1,n) ;
dfs_addvalue () ;
}
void solve ()
{
for ( int i = 1 ; i <= nq ; i ++ )
{
int u , v , gold ; ll silver ;
cin >> u >> v >> gold >> silver ;
loss_max = 0 ;
get (root[u],root[v],root[lca(u,v)],silver) ;
gold -= (num_gold[u]+num_gold[v]-2*num_gold[lca(u,v)]) - loss_max ;
if ( gold < 0 ) cout << - 1 << "\n" ;
else cout << gold << "\n" ;
}
}
int main ( )
{
ios_base :: sync_with_stdio (0) ;
cin.tie(0) ; cout.tie(0) ;
// rf ( ) ;
cin >> n >> m >> nq ;
for ( int i = 1 ; i < n ; i ++ )
{
int u , v ; cin >> u >> v ;
adjList [u] . pb (v) ;
adjList [v] . pb (u) ;
E [i] = {u,v} ;
}
dfs () ;
for ( int i = 1 ; i <= m ; i ++ )
{
int p , c ; cin >> p >> c ;
int u = E[p].fi , v = E[p].se ;
if ( dep [u] > dep [v] ) swap (u,v) ;
point [v] . pb (c) ;
}
build () ;
solve () ;
}
//-std=c++11
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp: In function 'void rf()':
currencies.cpp:31:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |