답안 #710697

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710697 2023-03-15T15:40:20 Z lam 새 집 (APIO18_new_home) C++14
5 / 100
5000 ms 301760 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
typedef pair<ii,ii> iiii;
#define ff first
#define ss second
const int maxn = 3e5+10;
int n,k,m,o;
map<int,int> mp;
multiset<int> ms[maxn];
int b[maxn];
vector <iiii> d;
int res[maxn];
struct node
{
    int wt, key, sz;
    node *l, *r;
    node (int x)
    {
        l=r=0;
        sz = 1;
        key = x; sz = 1;
        wt = rng();
    }
};
inline int get_sz(node *&x)
{
    return (x)?x->sz:0;
}
inline void update(node *&x)
{
    x->sz = 1 + get_sz(x->l) + get_sz(x->r);
}

void split(node *x, node *&l, node *&r, int k)
{
    if (!x)
    {
        l=r=0;
        return;
    }
    if (get_sz(x->l)>=k)
    {
        split(x->l,l,x->l,k);
        r=x;
    }
    else
    {
        split(x->r,x->r,r,k-1-get_sz(x->l));
        l=x;
    }
    update(x);
}

void split_key(node *x, node *&l, node *&r, int key)
{
    if (!x)
    {
        l=r=0;
        return;
    }
    if (x->key > key)
    {
        split_key(x->l,l,x->l,key);
        r=x;
    }
    else
    {
        split_key(x->r,x->r,r,key);
        l=x;
    }
    update(x);
}

void merg(node *&x, node *l, node *r)
{
    if (!l||!r)
    {
        if (!l) x=r;
        else x=l;
        return;
    }
    if (l->wt > r->wt)
    {
        merg(l->r,l->r,r);
        x=l;
    }
    else
    {
        merg(r->l,l,r->l);
        x=r;
    }
    update(x);
}

void ins(node *&treap, int val)
{
    node *a,*b;
    split_key(treap,treap,a,val);
    b = new node(val);
    merg(treap,treap,b);
    merg(treap,treap,a);
}

void dbg(node *&x)
{
    if (!x) return;
    dbg(x->l);
    cerr<<x->key<<" :: ";
    dbg(x->r);
}
void xoa(node *&treap, int val)
{
    node *a, *b;
//    cerr<<"xoa "<<val<<endl;
//    dbg(treap); cerr<<endl;
    split_key(treap,treap,a,val-1);
    split(a,b,a,1);
    merg(treap,treap,a);
//    dbg(treap); cerr<<endl;
//    cerr<<"------------"<<endl;
    delete(b);
}


int lbound(node *&treap, int r)
{
    node *a,*b;
//    cerr<<"dbg "; dbg(treap); cerr<<endl;
    split_key(treap,treap,a,r);
    int ans = get_sz(a);
    merg(treap,treap,a);
//    cerr<<r<<" --> "<<ans<<endl;
    return ans;
}

node* f[4*maxn];

int get(int x, int lx, int rx, int l, int r)
{
    if (b[lx]>r||b[rx]<l) return 0;
    if (b[lx]>=l&&b[rx]<=r)
    {
//        cerr<<"??? "<<x<<' '<<b[lx]<<' '<<b[rx]<<endl;
        return lbound(f[x],r);
    }
    int mid=(lx+rx)/2;
    return get(2*x,lx,mid,l,r) + get(2*x+1,mid+1,rx,l,r);
}

void add(int x, int lx, int rx, int idx, int val)
{
//    cerr<<"+ "<<x<<' '<<b[lx]<<' '<<b[rx]<<" = "<<idx<<' '<<val<<endl;
    ins(f[x],val);
//    cerr<<" :) "<<endl;
    if (lx==rx) return;
    int mid=(lx+rx)/2;
    if (idx<=b[mid]) add(2*x,lx,mid,idx,val);
    else add(2*x+1,mid+1,rx,idx,val);
}
void er(int x, int lx, int rx, int idx, int val)
{
//    cerr<<"- "<<x<<' '<<b[lx]<<' '<<b[rx]<<" = "<<idx<<' '<<val<<endl;
    xoa(f[x],val);
//    cerr<<" :< "<<endl;
    if (lx==rx) return;
    int mid=(lx+rx)/2;
    if (idx<=b[mid]) er(2*x,lx,mid,idx,val);
    else er(2*x+1,mid+1,rx,idx,val);
}

void build(int x, int lx, int rx)
{
    f[x] = 0;
    if (lx==rx) return;
    int mid=(lx+rx)/2;
    build(2*x,lx,mid); build(2*x+1,mid+1,rx);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin>>n>>k>>m;
    for (int i=1; i<=n; i++)
    {
        int x,t,l,r; cin>>x>>t>>l>>r;
        mp[x] = 1;
        d.push_back({{l,-1},{x,t}});
        d.push_back({{r,1},{x,t}});
    }
    for (int i=1; i<=m; i++)
    {
        int x,l; cin>>x>>l;
        d.push_back({{l,0},{x,i}});
    }
    sort(d.begin(),d.end());
    int cnt = 0;
    o = 0;
    for (auto &c:mp) c.ss = ++o, b[o] = c.ff;
//    cerr<<o<<endl;
    build(1,1,o);
    multiset<int> ms2;
    for (iiii i:d)
    {
//        cerr<<" -- "<<i.ff.ff<<' '<<i.ff.ss<<' '<<i.ss.ff<<' '<<i.ss.ss<<" -- "<<endl;
        if (i.ff.ss == -1)
        {
            int x = i.ss.ff; int t = i.ss.ss;
//            cerr<<"pro "<<x<<endl;
            if (ms[t].empty()) cnt++;
            auto r = ms[t].upper_bound(x);
            auto l = r;
            if (l!=ms[t].begin())
            {
                l--;
                if (r!=ms[t].end())
                    er(1,1,o,(*l),(*r));
                else er(1,1,o,(*l),1e9);
                add(1,1,o,(*l),x);
            }
            if (r!=ms[t].end()) add(1,1,o,x,(*r));
            else add(1,1,o,x,1e9);
            ms[t].insert(x);
            ms2.insert(x);
//            cerr<<"pri"<<endl;
        }
        else if (i.ff.ss == 1)
        {
            int x = i.ss.ff; int t = i.ss.ss;
            auto j = ms[t].lower_bound(x);
            auto l = j; auto r = j;
//            cerr<<"notpro "<<x<<endl;
            if (r==ms[t].end()) cerr<<" :@"<<endl;
            r++;
            if (l!=ms[t].begin())
            {
                l--;
                er(1,1,o,(*l),x);
                if (r!=ms[t].end()) add(1,1,o,(*l),(*r));
                else add(1,1,o,(*l),1e9);
            }
            if (r!=ms[t].end()) er(1,1,o,x,(*r));
            else er(1,1,o,x,1e9);
            ms[t].erase(ms[t].find(x));
//            cerr<<"notpri"<<endl;
            if (ms[t].empty()) cnt--;
            ms2.erase(ms2.find(x));
        }
        else
        {
            int x = i.ss.ff; int id = i.ss.ss;
            if (cnt<k) res[id] = -1;
            else
            {
                int l,r;
                auto j = ms2.end(); j--;
                r = (*j) - x;
                j=ms2.begin();
                r = max(r,x - (*j));
//                cerr<<"? "<<x<<' '<<cnt<<" = "<<id<<endl;
                l = 0;
                int ans = -1;
                while (l<=r)
                {
                    int mid = l+(r-l)/2;
                    if (get(1,1,o,x-mid,x+mid)==k)
                    {
                        ans = mid;
                        r=mid-1;
                    }
                    else l=mid+1;
                }
                res[id] = ans;
            }
        }
    }
    for (int i=1; i<=m; i++) cout<<res[i]<<'\n';
}
/**
4 2 4
3 1 1 10
9 2 1 7
7 2 1 4
4 1 1 10
5 3
5 5
5 8
1 4
**/

Compilation message

new_home.cpp: In function 'int lbound(node*&, int)':
new_home.cpp:132:14: warning: unused variable 'b' [-Wunused-variable]
  132 |     node *a,*b;
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14432 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14520 KB Output is correct
6 Correct 13 ms 14552 KB Output is correct
7 Correct 9 ms 14556 KB Output is correct
8 Correct 11 ms 14676 KB Output is correct
9 Correct 9 ms 14656 KB Output is correct
10 Correct 13 ms 14676 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 11 ms 14420 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 9 ms 14468 KB Output is correct
15 Correct 11 ms 14588 KB Output is correct
16 Correct 11 ms 14548 KB Output is correct
17 Correct 12 ms 14532 KB Output is correct
18 Correct 11 ms 14548 KB Output is correct
19 Correct 10 ms 14548 KB Output is correct
20 Correct 12 ms 14620 KB Output is correct
21 Correct 8 ms 14512 KB Output is correct
22 Correct 9 ms 14660 KB Output is correct
23 Correct 11 ms 14548 KB Output is correct
24 Correct 12 ms 14512 KB Output is correct
25 Correct 11 ms 14532 KB Output is correct
26 Correct 11 ms 14500 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 10 ms 14420 KB Output is correct
29 Correct 10 ms 14420 KB Output is correct
30 Correct 9 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14432 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14520 KB Output is correct
6 Correct 13 ms 14552 KB Output is correct
7 Correct 9 ms 14556 KB Output is correct
8 Correct 11 ms 14676 KB Output is correct
9 Correct 9 ms 14656 KB Output is correct
10 Correct 13 ms 14676 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 11 ms 14420 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 9 ms 14468 KB Output is correct
15 Correct 11 ms 14588 KB Output is correct
16 Correct 11 ms 14548 KB Output is correct
17 Correct 12 ms 14532 KB Output is correct
18 Correct 11 ms 14548 KB Output is correct
19 Correct 10 ms 14548 KB Output is correct
20 Correct 12 ms 14620 KB Output is correct
21 Correct 8 ms 14512 KB Output is correct
22 Correct 9 ms 14660 KB Output is correct
23 Correct 11 ms 14548 KB Output is correct
24 Correct 12 ms 14512 KB Output is correct
25 Correct 11 ms 14532 KB Output is correct
26 Correct 11 ms 14500 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 10 ms 14420 KB Output is correct
29 Correct 10 ms 14420 KB Output is correct
30 Correct 9 ms 14420 KB Output is correct
31 Execution timed out 5043 ms 74836 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5037 ms 289132 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5077 ms 301760 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14432 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14520 KB Output is correct
6 Correct 13 ms 14552 KB Output is correct
7 Correct 9 ms 14556 KB Output is correct
8 Correct 11 ms 14676 KB Output is correct
9 Correct 9 ms 14656 KB Output is correct
10 Correct 13 ms 14676 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 11 ms 14420 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 9 ms 14468 KB Output is correct
15 Correct 11 ms 14588 KB Output is correct
16 Correct 11 ms 14548 KB Output is correct
17 Correct 12 ms 14532 KB Output is correct
18 Correct 11 ms 14548 KB Output is correct
19 Correct 10 ms 14548 KB Output is correct
20 Correct 12 ms 14620 KB Output is correct
21 Correct 8 ms 14512 KB Output is correct
22 Correct 9 ms 14660 KB Output is correct
23 Correct 11 ms 14548 KB Output is correct
24 Correct 12 ms 14512 KB Output is correct
25 Correct 11 ms 14532 KB Output is correct
26 Correct 11 ms 14500 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 10 ms 14420 KB Output is correct
29 Correct 10 ms 14420 KB Output is correct
30 Correct 9 ms 14420 KB Output is correct
31 Execution timed out 5043 ms 74836 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14432 KB Output is correct
2 Correct 9 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 10 ms 14520 KB Output is correct
6 Correct 13 ms 14552 KB Output is correct
7 Correct 9 ms 14556 KB Output is correct
8 Correct 11 ms 14676 KB Output is correct
9 Correct 9 ms 14656 KB Output is correct
10 Correct 13 ms 14676 KB Output is correct
11 Correct 9 ms 14420 KB Output is correct
12 Correct 11 ms 14420 KB Output is correct
13 Correct 8 ms 14416 KB Output is correct
14 Correct 9 ms 14468 KB Output is correct
15 Correct 11 ms 14588 KB Output is correct
16 Correct 11 ms 14548 KB Output is correct
17 Correct 12 ms 14532 KB Output is correct
18 Correct 11 ms 14548 KB Output is correct
19 Correct 10 ms 14548 KB Output is correct
20 Correct 12 ms 14620 KB Output is correct
21 Correct 8 ms 14512 KB Output is correct
22 Correct 9 ms 14660 KB Output is correct
23 Correct 11 ms 14548 KB Output is correct
24 Correct 12 ms 14512 KB Output is correct
25 Correct 11 ms 14532 KB Output is correct
26 Correct 11 ms 14500 KB Output is correct
27 Correct 10 ms 14448 KB Output is correct
28 Correct 10 ms 14420 KB Output is correct
29 Correct 10 ms 14420 KB Output is correct
30 Correct 9 ms 14420 KB Output is correct
31 Execution timed out 5043 ms 74836 KB Time limit exceeded
32 Halted 0 ms 0 KB -