Submission #274233

# Submission time Handle Problem Language Result Execution time Memory
274233 2020-08-19T10:13:58 Z egekabas Santa Claus (RMI19_santa) C++14
80 / 100
403 ms 32120 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n;
struct node{
    int x, h, v;
} a[500009];
int used[500009];
int ans[500009];
pii seg[2000009];
multiset<int> s[500009];
void build(int v, int tl, int tr){
    if(tl == tr){
        s[tl].clear();
        seg[v] = {1e9, tl};
        return;
    }
    build(2*v, tl, (tl+tr)/2);
    build(2*v+1, (tl+tr)/2+1, tr);
    seg[v] = min(seg[2*v], seg[2*v+1]);
}
void upd(int v, int tl, int tr, int idx){
    if(idx < tl || idx > tr)
        return;
    if(idx == tl && idx == tr){
        if(s[tl].empty())
            seg[v].ff = 1e9;
        else
           seg[v].ff = *s[tl].begin();
    }
    else{
        upd(2*v, tl, (tl+tr)/2, idx);
        upd(2*v+1, (tl+tr)/2+1, tr, idx);
        seg[v] = min(seg[2*v], seg[2*v+1]);
    }
}
pii get(int v, int tl, int tr, int l, int r){
    if(tr < l || r < tl)
        return {1e9, 1e9};
    if(l <= tl && tr <= r)
        return seg[v];
    else{
        return min(
        get(2*v, tl, (tl+tr)/2, l, r),
        get(2*v+1, (tl+tr)/2+1, tr, l, r)        
        );
    }
}
void solve(){
    cin >> n;
    int latest = 0;
    for(int i = 0; i < n; ++i)
        cin >> a[i].x;
    for(int i = 0; i < n; ++i){
        cin >> a[i].h;
        if(a[i].h == 0) latest = i;
    }
    for(int i = 0; i < n; ++i){
        cin >> a[i].v;
        used[i] = -1;
    }
    build(1, 0, n);
    multiset<int> pr;
    for(int i = 0; i <= latest; ++i){
        if(i != latest) ans[i] = -1;
        if(a[i].h == 0)
            pr.insert(a[i].v);
        else{
            if(pr.lower_bound(a[i].v) != pr.end()){
                a[i].h = 0;
                pr.erase(pr.lower_bound(a[i].v));
            }
        }
    }
    int j = 1e9;
    multiset<int, greater<int>> kids;
    for(int i = latest; i < n; ++i){
        if(j == 1e9){
            for(j = i; j >= 0; --j){
                if(a[j].h)
                    if(pr.lower_bound(a[j].v) != pr.end()){
                        used[j] = *pr.lower_bound(a[j].v);
                        pr.erase(pr.lower_bound(a[j].v));
                    }
                if(pr.empty())
                    break;
            }
        }
        if(j < 0) j = 0;
        if(a[i].h){
            int cur = a[i].v;
            pii sg = get(1, 0, n, cur, n);
            if(sg.ff < cur){
                s[sg.ss].erase(s[sg.ss].lower_bound(sg.ff));
                s[sg.ss].insert(cur);
                upd(1, 0, n, sg.ss);
                cur = sg.ff;
            }
            kids.insert(cur);
        }
        while(pr.size()){
            int cur = *pr.begin();
            if(kids.lower_bound(cur) != kids.end()){
                int change = *kids.lower_bound(cur);
                kids.erase(kids.lower_bound(cur));
                s[cur].insert(change);
                upd(1, 0, n, cur);
                pr.erase(pr.begin()); 
            }
            else
                break;
        }
        if(pr.size()){
            ans[i] = -1;
            continue;
        }
        while(j < i){
            if(used[j] < 0){
                ++j;
                continue;
            }
            int cur = used[j];
            if(kids.lower_bound(cur) != kids.end()){
                int change = *kids.lower_bound(cur);
                kids.erase(kids.lower_bound(cur));
                s[cur].insert(change);
                upd(1, 0, n, cur);
                ++j; 
            }
            else
                break;
        }
        ans[i] = 2*a[i].x-a[j].x; 
    }
    /*for(int i = n-1; i >= 1; --i)
        if(a[i].x == a[i-1].x)
            ans[i-1] = ans[i];*/
    for(int i = 0; i < n; ++i)
        cout << ans[i] << ' ';
    cout << '\n';
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    int t;
    cin >> t;
    while(t--){
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Incorrect 24 ms 23936 KB Output isn't correct
4 Correct 37 ms 24192 KB Output is correct
5 Incorrect 57 ms 24568 KB Output isn't correct
6 Correct 82 ms 25080 KB Output is correct
7 Correct 143 ms 26364 KB Output is correct
8 Correct 218 ms 27768 KB Output is correct
9 Correct 278 ms 31352 KB Output is correct
10 Correct 403 ms 32120 KB Output is correct