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;
#define ll int
#define pb push_back
ll n;
ll refseg=1,curver;
ll members[500005];
struct SEG{
vector<vector<ll> >seg,lc,rc;
vector<ll>temp,templ,tempr;
void init(ll n){
seg.resize(4*n+5),lc.resize(4*n+5),rc.resize(4*n+5),temp.resize(4*n+5);
templ.resize(4*n+5),tempr.resize(4*n+5);
}
void build(ll l, ll r, ll id){
if(l==r){
seg[id].pb(0);
return;
}
else{
ll mid=(l+r)>>1;
build(l,mid,id*2),build(mid+1,r,id*2+1);
seg[id].pb(0),lc[id].pb(0),rc[id].pb(0);
}
}
void upd(ll ver, ll ul, ll l, ll r, ll val, ll id){
if(l==r){
seg[id].pb(seg[id][ver]+val);
return;
}
ll mid=(l+r)>>1;
if(ul<=mid){
upd(lc[id][ver],ul,l,mid,val,id*2);
seg[id].pb(seg[id][ver]+val);
lc[id].pb(seg[id*2].size()-1);
rc[id].pb(rc[id][ver]);
}
else{
upd(rc[id][ver],ul,mid+1,r,val,id*2+1);
seg[id].pb(seg[id][ver]+val);
rc[id].pb(seg[id*2+1].size()-1);
lc[id].pb(lc[id][ver]);
}
}
void create(ll l, ll r, ll id){
if(l==r){
seg[id].pb(0);
}
else{
ll mid=(l+r)>>1;
create(l,mid,id*2),create(mid+1,r,id*2+1);
seg[id].pb(0),lc[id].pb(seg[id*2].size()-1),rc[id].pb(seg[id*2+1].size()-1);
}
}
void pre(ll ver, ll ql, ll qr, ll l, ll r, ll id){
if(l>qr||r<ql||ql>qr){
return;
}
if(l>=ql&&r<=qr){
temp[id]=ver;
if(l!=r){templ[id]=lc[id][ver],tempr[id]=rc[id][ver];}
return;
}
ll mid=(l+r)>>1;
pre(lc[id][ver],ql,qr,l,mid,id*2),pre(rc[id][ver],ql,qr,mid+1,r,id*2+1);
}
ll qry(ll ver, ll ql, ll qr, ll l, ll r, ll id){
if(l>qr||r<ql){
return 0;
}
if(l>=ql&&r<=qr){
return seg[id][ver];
}
ll mid=(l+r)>>1;
return qry(lc[id][ver],ql,qr,l,mid,id*2),qry(rc[id][ver],ql,qr,mid+1,r,id*2+1);
}
void assign(ll ver, ll ql, ll qr, ll l, ll r, ll id){
if(l>qr||r<ql||ql>qr){
return;
}
seg[id].pb(0);
if(l>=ql&&r<=qr){
if(l!=r){
lc[id].pb(templ[id]);
rc[id].pb(tempr[id]);
}
seg[id][seg[id].size()-1]=seg[id][temp[id]];
return;
}
lc[id].pb(0),rc[id].pb(0);
ll mid=(l+r)>>1;
if(mid>=ql){
assign(lc[id][ver],ql,qr,l,mid,id*2);
lc[id][seg[id].size()-1]=seg[id*2].size()-1;
}
else{
lc[id][seg[id].size()-1]=lc[id][ver];
}
if(mid+1<=qr){
assign(rc[id][ver],ql,qr,mid+1,r,id*2+1);
rc[id][seg[id].size()-1]=seg[id*2+1].size()-1;
}
else{
rc[id][seg[id].size()-1]=rc[id][ver];
}
seg[id][seg[id].size()-1]=seg[id][lc[id][seg[id].size()-1]]+seg[id][rc[id][seg[id].size()-1]];
}
}reference,st;
vector<ll>adj[500005];
void init(int N, int a[], int b[]) {
n=N;
for(int i=0;i<n;i++){
adj[a[i]].pb(b[i]);
}
st.init(n); st.build(1,n,1);
reference.init(n); reference.build(1,n,1);
for(int i=1;i<=n;i++){
for(auto& u: adj[i]){
reference.upd(refseg-1,u,1,n,1,1);
st.upd(refseg-1,u,1,n,1,1);
refseg++;
}
members[i]=refseg-1;
}
st.create(1,n,1);
}
bool ck(ll used, ll cur, ll par){
ll hv=reference.qry(members[cur],cur,par,1,n,1);
ll alr=st.qry(used,cur,par,1,n,1);
return (hv-alr)>=cur;
}
int can(int m, int k[]) {
ll sum=0;
for(int i=0;i<m;i++){
sum+=k[i];
}
if(sum>n){
return 0;
}
curver=n+1;
sort(k,k+m);
for(int i=0;i<m;i++){
// cout<<k[i]<<" "<<curver<<'\n';
ll l=k[i],r=n;
if(!ck(curver,k[i],r))return 0;
while(l<=r){
if(l==r)break;
else if(l==r-1){if(ck(curver,k[i],l)){r=l;}else{l=r;}}
else{
ll mid=(l+r)>>1;
if(ck(curver,k[i],mid)){r=mid;}
else{l=mid;}
}
}
ll lol2=reference.qry(k[i],l,l,1,n,1);
ll val=reference.qry(members[k[i]],k[i],l,1,n,1)-st.qry(curver,k[i],l,1,n,1);
ll rem=val-k[i];
if(rem==0){
st.pre(members[k[i]],k[i],l,1,n,1);
st.assign(curver,k[i],l,1,n,1);
curver=st.seg[1].size()-1;
}
else{
if(k[i]<=l-1){
st.pre(members[k[i]],k[i],l-1,1,n,1);
st.assign(curver,k[i],l-1,1,n,1);
curver=st.seg[1].size()-1;
}
ll lol=st.qry(curver,l,l,1,n,1);
st.upd(curver,l,1,n,lol2-rem-lol,1);
curver=st.seg[1].size()-1;
}
}
return 1;
}
// int main() {
// // _inputFile = fopen("teams.in", "rb");
// // _outputFile = fopen("teams.out", "w");
// int N;
// cin>>N;
// int *A = (int*)malloc(sizeof(int)*(unsigned int)N);
// int *B = (int*)malloc(sizeof(int)*(unsigned int)N);
// for (int i = 0; i < N; ++i) {
// cin>>A[i]>>B[i];
// }
// init(N, A, B);
// int Q;
// cin>>Q;
// for (int i = 0; i < Q; ++i) {
// int M;
// cin>>M;
// int *K = (int*)malloc(sizeof(int)*(unsigned int)M);
// for (int j = 0; j < M; ++j) {
// cin>>K[j];
// }
// cout<<can(M,K)<<'\n';
// }
// return 0;
// }
Compilation message (stderr)
teams.cpp: In member function 'void SEG::init(int)':
teams.cpp:12:18: warning: declaration of 'n' shadows a global declaration [-Wshadow]
12 | void init(ll n){
| ^
teams.cpp:6:4: note: shadowed declaration is here
6 | ll n;
| ^
teams.cpp: In member function 'void SEG::upd(int, int, int, int, int, int)':
teams.cpp:36:39: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
36 | lc[id].pb(seg[id*2].size()-1);
| ~~~~~~~~~~~~~~~~^~
teams.cpp:42:41: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
42 | rc[id].pb(seg[id*2+1].size()-1);
| ~~~~~~~~~~~~~~~~~~^~
teams.cpp: In member function 'void SEG::create(int, int, int)':
teams.cpp:53:53: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
53 | seg[id].pb(0),lc[id].pb(seg[id*2].size()-1),rc[id].pb(seg[id*2+1].size()-1);
| ~~~~~~~~~~~~~~~~^~
teams.cpp:53:85: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
53 | seg[id].pb(0),lc[id].pb(seg[id*2].size()-1),rc[id].pb(seg[id*2+1].size()-1);
| ~~~~~~~~~~~~~~~~~~^~
teams.cpp: In member function 'void SEG::assign(int, int, int, int, int, int)':
teams.cpp:95:54: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
95 | lc[id][seg[id].size()-1]=seg[id*2].size()-1;
| ~~~~~~~~~~~~~~~~^~
teams.cpp:102:56: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} may change value [-Wconversion]
102 | rc[id][seg[id].size()-1]=seg[id*2+1].size()-1;
| ~~~~~~~~~~~~~~~~~~^~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:164:36: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
164 | curver=st.seg[1].size()-1;
| ~~~~~~~~~~~~~~~~^~
teams.cpp:170:36: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
170 | curver=st.seg[1].size()-1;
| ~~~~~~~~~~~~~~~~^~
teams.cpp:174:36: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
174 | curver=st.seg[1].size()-1;
| ~~~~~~~~~~~~~~~~^~
# | 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... |