# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
640754 | Tenis0206 | Santa Claus (RMI19_santa) | C++11 | 449 ms | 12676 KiB |
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;
int n;
int x[100005],v[100005];
bool h[100005];
bool sel[100005];
int nr_left = 0;
int ai[500005],lazy[500005];
void preset(int nod, int a, int b)
{
ai[nod] = lazy[nod] = 0;
if(a==b)
{
return;
}
int mij = (a + b) >> 1;
preset(nod*2,a,mij);
preset(nod*2+1,mij+1,b);
}
void propag(int nod)
{
if(!lazy[nod])
{
return;
}
ai[nod*2] += lazy[nod];
ai[nod*2+1] += lazy[nod];
lazy[nod*2] += lazy[nod];
lazy[nod*2+1] += lazy[nod];
lazy[nod] = 0;
return;
}
void update(int ua, int ub, int val, int nod, int a, int b)
{
if(ua<=a && ub>=b)
{
ai[nod] += val;
lazy[nod] += val;
return;
}
propag(nod);
int mij = (a + b) >> 1;
if(ua<=mij)
{
update(ua,ub,val,nod*2,a,mij);
}
if(ub>mij)
{
update(ua,ub,val,nod*2+1,mij+1,b);
}
ai[nod] = min(ai[nod*2],ai[nod*2+1]);
}
void add_elf(int val)
{
update(val,n,-1,1,1,n);
}
void remove_elf(int val)
{
update(val,n,+1,1,1,n);
}
void add_child(int val)
{
update(val,n,+1,1,1,n);
}
void remove_child(int val)
{
update(val,n,-1,1,1,n);
}
bool is_ok()
{
return (ai[1] >= 0);
}
bool can_remove(int val, bool type)
{
if(type==0)
{
return true;
}
remove_child(val);
bool ok = is_ok();
add_child(val);
return ok;
}
void solve_test()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>x[i];
}
int last_elf = 0;
for(int i=1; i<=n; i++)
{
cin>>h[i];
if(!h[i])
{
last_elf = i;
}
}
for(int i=1; i<=n; i++)
{
cin>>v[i];
}
int poz = 0;
for(int i=1; i<=n; i++)
{
if(h[i]==0)
{
add_elf(v[i]);
}
else
{
add_child(v[i]);
}
if(last_elf > i)
{
cout<<-1<<' ';
continue;
}
if(is_ok())
{
poz = i;
break;
}
}
multiset<int> s;
for(int i=1; i<=poz; i++)
{
if(h[i]==0)
{
s.insert(v[i]);
continue;
}
auto dr = s.lower_bound(v[i]);
if(dr==s.end())
{
continue;
}
remove_elf(*dr);
if(can_remove(v[i],h[i]))
{
remove_child(v[i]);
s.erase(dr);
sel[i] = true;
}
else
{
add_elf(*dr);
}
}
int j = 1;
while(j<poz && (can_remove(v[j],h[j]) || sel[j] || j > last_elf))
{
if(!sel[j] && h[j])
{
remove_child(v[j]);
}
++j;
}
cout<<2 * x[poz] - x[j]<<' ';
for(int i=poz+1; i<=n; i++)
{
if(h[i]==0)
{
add_elf(v[i]);
}
else
{
add_child(v[i]);
}
if(last_elf > i)
{
cout<<-1<<' ';
continue;
}
while(j<i && (can_remove(v[j],h[j]) || sel[j] || j > last_elf))
{
if(!sel[j] && h[j])
{
remove_child(v[j]);
}
++j;
}
cout<<2 * x[i] - x[j]<<' ';
}
cout<<'\n';
preset(1,1,n);
for(int i=1; i<=n; i++)
{
sel[i] = false;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
for(int test=1; test<=t; test++)
{
solve_test();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |