Submission #1094293

# Submission time Handle Problem Language Result Execution time Memory
1094293 2024-09-29T09:21:29 Z alexdd Santa Claus (RMI19_santa) C++17
100 / 100
322 ms 13392 KB
#include <bits/stdc++.h>
using namespace std;
int n;

pair<int,int> aint[270000];
pair<int,int> emp = {0,0};
pair<int,int> combine(pair<int,int> x, pair<int,int> y)
{
    return {x.first + y.first, min(x.second, x.first + y.second)};
}
void upd(int nod, int st, int dr, int poz, int newv)
{
    if(st==dr)
    {
        aint[nod].first += newv;
        aint[nod].second += newv;
        return;
    }
    int mij=(st+dr)/2;
    if(poz<=mij) upd(nod*2,st,mij,poz,newv);
    else upd(nod*2+1,mij+1,dr,poz,newv);
    aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
int x[100000],h[100000],v[100000];
int rez[100000];
signed main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<min(4*n+2,270000);i++)
            aint[i] = emp;
        int ult0=0;
        for(int i=1;i<=n;i++)
            cin>>x[i];
        for(int i=1;i<=n;i++)
        {
            cin>>h[i];
            if(h[i]==0) ult0=i;
            rez[i] = -1;
        }
        for(int i=1;i<=n;i++)
            cin>>v[i];

        set<pair<int,int>> s;
        for(int i=1;i<=ult0;i++)
        {
            if(h[i]==0) upd(1,1,n,v[i],-1);
            else upd(1,1,n,v[i],+1);
        }
        int st=1;
        for(int dr=ult0;dr<=n;dr++)
        {
            if(dr>ult0)
            {
                assert(h[dr]==1);
                upd(1,1,n,v[dr],+1);
            }
            while(st<=dr && aint[1].second>=0)
            {
                if(h[st]==0)
                {
                    s.insert({v[st],st});
                }
                else
                {
                    upd(1,1,n,v[st],-1);
                    auto it = s.lower_bound({v[st],-1});
                    if(it!=s.end())
                    {
                        upd(1,1,n,(*it).first,+1);
                        s.erase(it);
                    }
                }
                st++;
            }
            if(st==1)
                rez[dr] = -1;
            else
                rez[dr] = 2*x[dr] - x[st-1];
        }
        for(int i=1;i<=n;i++)
            cout<<rez[i]<<" ";
        cout<<"\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 17 ms 1008 KB Output is correct
5 Correct 38 ms 1772 KB Output is correct
6 Correct 60 ms 2724 KB Output is correct
7 Correct 112 ms 4896 KB Output is correct
8 Correct 174 ms 7232 KB Output is correct
9 Correct 217 ms 10440 KB Output is correct
10 Correct 322 ms 13392 KB Output is correct