Submission #491526

# Submission time Handle Problem Language Result Execution time Memory
491526 2021-12-02T21:54:56 Z leaked New Home (APIO18_new_home) C++14
47 / 100
5000 ms 1048580 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define sz(x) (int)(x).size()
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pw(x) (1LL<<(x))
#define m_p make_pair
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

template <class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template <class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;

const int lg=log2(300'000)+1;
const int N=pw(lg);
bool del[5*N];
struct segtree{
    vec<int> uk[2*N][2];
    vec<int> pos[2*N];
    vec<int> mx[2*N];
    int type=0;
    void build(int v,int tl,int tr){
        /// my positions are like that
        if(type){
            for(int i=sz(pos[v])-2;i>=0;i--)
                umax(mx[v][i],mx[v][i+1]);
            if(tl==tr)
                return;
            for(int t=0;t<2;t++){
                uk[v][t].resize(sz(pos[v]));
                int u=2*v+t;
                int j=sz(pos[u]);
                for(int i=sz(pos[v])-1;i>=0;i--){
                    while(j!=0 && pos[u][j-1]>=pos[v][i]) --j;
                    uk[v][t][i]=j;
                }
            }
        }
        else{

            for(int i=1;i<sz(pos[v]);i++)
                umax(mx[v][i],mx[v][i-1]);
//            cout<<"BUILD "<<v<<endl;
//            for(auto &z : pos[v])
//                cout<<z<<' ';
//            cout<<endl;
//            for(auto &z : mx[v])
//                cout<<z<<' ';
//            cout<<endl;
            if(tl==tr)
                return;
            for(int t=0;t<2;t++){
                uk[v][t].resize(sz(pos[v]));
                int u=2*v+t;
                int j=-1;
                for(int i=0;i<sz(pos[v]);i++){
                    while(j+1!=sz(pos[u]) && pos[u][j+1]<=pos[v][i]) ++j;
                    uk[v][t][i]=j;
                }
            }
        }
        int tm=(tl+tr)>>1;
        build(2*v,tl,tm);build(2*v+1,tm+1,tr);
    }
    void add(int l,int r,int i,int x,int v,int tl,int tr){
        if(tl>r||tr<l)
            return;
        if(!sz(pos[v]) || pos[v].back()!=i)
            pos[v].pb(i),mx[v].pb(-1e9);
//        cout<<"ADDED "<<i<<' '<<x<<' '<<l<<' '<<r<<' '<<tl<<' '<<tr<<'\n';
        if(tl>=l&&tr<=r){
//            cout<<"_SET "<<v<<' '<<sz(mx[v])-1<<' '<<i<<' '<<x<<endl;
            umax(mx[v].back(),x);
            return;
        }
        int tm=(tl+tr)>>1;
        add(l,r,i,x,2*v,tl,tm);add(l,r,i,x,2*v+1,tm+1,tr);
    }
    int get(int _t,int j,int v,int tl,int tr){
        if(_t>tr || _t<tl)
            return -1e9;
        if(j==-1 || j==sz(pos[v]))
            return -1e9;
        int ans=mx[v][j];
        if(tl==tr){
            return ans;
        }
        int tm=(tl+tr)>>1;
        if(tm>=_t)
            return max(ans,get(_t,uk[v][0][j],2*v,tl,tm));
        else
            return max(ans,get(_t,uk[v][1][j],2*v+1,tm+1,tr));
    }
}lft,rgt;
vec<int> ts;
map<pii,int>mp;
int vr[4*N];
int tmr,cur_t=-1;
vec<array<int,4>>addls;
vec<array<int,4>>addrs;
void add(int l,int r,int t,int tp,int _e=-1){
    if(l==r) return;
    int mid=(l+r)>>1;
    if(!t){
        int st=vr[mp[{l,tp}]],_end=_e;
        if(st>_end)
            return;
        mp.erase({l,tp});
//        cout<<"ADDITIOn "<<l<<' '<<' '<<r<<' '<<st<<' '<<_end<<endl;
        addls.pb({mid,st,_end,-l});
//        cout<<"WHAT "<<mid<<' '<<r<<' '<<st<<' '<<_end<<endl;
        addrs.pb({mid+1,st,_end,r});
        return;
    }
//    cout<<"CHE "<<l<<' '<<r<<endl;
    mp[{l,tp}]=tmr;
    vr[tmr]=_e;
    ++tmr;
}
const int Nax=3e5+1;
multiset<int> st[Nax];
vec<int>here[Nax];
signed main(){
    fast_iati;
    lft.type=1;rgt.type=0;
    int n,q,k;
//    n=15;k=4;q=15;
    cin>>n>>k>>q;
    vec<array<int,3>>arr;
    vec<int>x(n),a(n),b(n),t(n);
    for(int i=0;i<n;i++){
        cin>>x[i]>>t[i]>>a[i]>>b[i];
//        x[i]=rand(),t[i]=rand()%k+1;
//        a[i]=rand(),b[i]=rand();
//        if(a[i]>b[i])
//            swap(a[i],b[i]);
//        kek.pb(x[i]);
//        ts.pb(a[i]);
//        ts.pb(b[i]+1);
        arr.pb({a[i],0,i});
        arr.pb({b[i]+1,-1,i});
        --t[i];
        here[t[i]].pb(i);
    }
    vec<int>l(q),y(q);
    for(int i=0;i<q;i++){
        cin>>l[i]>>y[i];
//        l[i]=rand(),y[i]=rand();
        ts.pb(y[i]);
        arr.pb({y[i],2,i});
    }
    ts.pb(-2e9);
    sort(all(ts));ts.erase(unique(all(ts)),ts.end());
    sort(all(arr));
    auto stupid=[&](array<int,3> z){
        pii me={-1e9,0};
        int tx=l[z[1]];
        for(int j=0;j<k;j++){
            if(sz(st[j])==2) continue;
            auto it=st[j].lower_bound(tx);
            int omn=2e9;
            if(it!=st[j].end()) umin(omn,*it-tx);
            if(it!=st[j].begin()) umin(omn,tx-*prev(it));
//            cout<<"COLOUR "<<j<<' '<<omn<<' '<<tx<<endl;
            umax(me.f,omn);me.s++;
        }
        return me;
    };
//    int uk=0;
//    for(auto &z : arr){
//        while(uk<sz(ts) && ts[uk]<=z[0]) ++uk;
////        cout<<"ALOt "<<z[0]<<' '<<uk<<' '<<ts[uk]<<endl;
////        if(z[1]!=2)
////            z[0]=uk+z[1];
//    }
    vec<int>cnt(k,0);
    cur_t=0;
    for(int i=0;i<k;i++){
        add(-3e8,3e8,1,i,0);
        st[i].insert(-3e8);st[i].insert(3e8);
    }
    int total=0;
    vec<int>sta(q,-1);
    vec<bool>bad(q,0);
    for(auto &z : arr){
        if(z[1]!=2)
            cur_t=z[0];
        swap(z[1],z[2]);
//        if(z[2]!=2)cout<<"ALO "<<x[z[1]]<<' '<<z[1]<<endl;
        if(z[2]==2){
            bad[z[1]]=(total==k?0:1);
//            cerr<<"QEU "<<y[z[1]]<<' '<<total<<endl;
//            cout<<"ALO "<<
//            sta[z[1]]=(total==k?stupid(z).f:-1);
            continue;
        }
//        cout<<"TYPE "<<t[z[1]]<<endl;
        if(z[2]==-1){
            int i=z[1];
            auto it=st[t[i]].lower_bound(x[i]);
            int et=upper_bound(all(ts),z[0]-1)-ts.begin()-1;
            int srt=lower_bound(all(ts),z[0])-ts.begin();
            add(x[i],*next(it),0,t[i],et);
            add(*prev(it),x[i],0,t[i],et);
            add(*prev(it),*next(it),1,t[i],srt);
            st[t[i]].erase(it);
            cnt[t[i]]--;
            if(!cnt[t[i]])
                total--;
//            cout<<"DEL "<<x[i]<<' '<<total<<endl;
        }
        else{
            int i=z[1];
            int et=upper_bound(all(ts),z[0]-1)-ts.begin()-1;
            int srt=lower_bound(all(ts),z[0])-ts.begin();
//            cout<<"ADD "<<x[i]<<endl;
            if(!cnt[t[i]])
                total++;
            cnt[t[i]]++;
            st[t[i]].insert(x[i]);
            auto it=st[t[i]].lower_bound(x[i]);
            add(*prev(it),*next(it),0,t[i],et);
            add(x[i],*next(it),1,t[i],srt);
            add(*prev(it),x[i],1,t[i],srt);
        }
//        cout<<z[0]<<' '<<total<<endl;
    }
    ///building
    sort(all(addls));sort(all(addrs));
    for(auto &z : addls)
        lft.add(z[1],z[2],z[0],z[3],1,0,sz(ts)-1);
//    cout<<endl;
    for(auto &z : addrs)
        rgt.add(z[1],z[2],z[0],z[3],1,0,sz(ts)-1);
    lft.build(1,0,sz(ts)-1);
//    cout<<endl;cout<<endl;
    rgt.build(1,0,sz(ts)-1);

    for(int i=0;i<q;i++){
//        if(cntt[])
        int j=l[i];
        int my_t=lower_bound(all(ts),y[i])-ts.begin();
//        cout<<"STUPID "<<sta[i]<<' ';
        if(bad[i]){
            cout<<-1<<'\n';
            continue;
        }
        int id=lower_bound(all(lft.pos[1]),j)-lft.pos[1].begin();
        int ans=max(0,j+lft.get(my_t,id,1,0,sz(ts)-1));
//        cout<<"ALO "<<ans<<endl;
        id=upper_bound(all(rgt.pos[1]),j)-rgt.pos[1].begin()-1;
//        cout<<"DEBUGID "<<id<<endl;
        umax(ans,-j+rgt.get(my_t,id,1,0,sz(ts)-1));
//        cout<<ans<<endl;
        if(ans>1e8)
            ans=-1;
        cout<<ans<<'\n';
    }
    return 0;
}
/*

4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10


2 1 3
1 1 1 4
1 1 2 6
1 3
1 5
1 7

1 1 1
100000000 1 1 1
1 1
*/

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:162:10: warning: variable 'stupid' set but not used [-Wunused-but-set-variable]
  162 |     auto stupid=[&](array<int,3> z){
      |          ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 120 ms 218384 KB Output is correct
2 Correct 111 ms 218412 KB Output is correct
3 Correct 112 ms 218380 KB Output is correct
4 Correct 113 ms 218396 KB Output is correct
5 Correct 138 ms 218456 KB Output is correct
6 Correct 131 ms 219272 KB Output is correct
7 Correct 121 ms 219368 KB Output is correct
8 Correct 119 ms 219264 KB Output is correct
9 Correct 124 ms 219236 KB Output is correct
10 Correct 120 ms 219344 KB Output is correct
11 Correct 147 ms 219084 KB Output is correct
12 Correct 119 ms 218968 KB Output is correct
13 Correct 119 ms 218848 KB Output is correct
14 Correct 114 ms 218864 KB Output is correct
15 Correct 124 ms 219332 KB Output is correct
16 Correct 116 ms 219400 KB Output is correct
17 Correct 116 ms 219204 KB Output is correct
18 Correct 114 ms 219280 KB Output is correct
19 Correct 116 ms 219340 KB Output is correct
20 Correct 117 ms 219248 KB Output is correct
21 Correct 119 ms 218724 KB Output is correct
22 Correct 116 ms 219256 KB Output is correct
23 Correct 117 ms 219388 KB Output is correct
24 Correct 121 ms 219372 KB Output is correct
25 Correct 124 ms 219392 KB Output is correct
26 Correct 120 ms 219060 KB Output is correct
27 Correct 133 ms 218692 KB Output is correct
28 Correct 116 ms 219036 KB Output is correct
29 Correct 113 ms 219008 KB Output is correct
30 Correct 116 ms 218824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 218384 KB Output is correct
2 Correct 111 ms 218412 KB Output is correct
3 Correct 112 ms 218380 KB Output is correct
4 Correct 113 ms 218396 KB Output is correct
5 Correct 138 ms 218456 KB Output is correct
6 Correct 131 ms 219272 KB Output is correct
7 Correct 121 ms 219368 KB Output is correct
8 Correct 119 ms 219264 KB Output is correct
9 Correct 124 ms 219236 KB Output is correct
10 Correct 120 ms 219344 KB Output is correct
11 Correct 147 ms 219084 KB Output is correct
12 Correct 119 ms 218968 KB Output is correct
13 Correct 119 ms 218848 KB Output is correct
14 Correct 114 ms 218864 KB Output is correct
15 Correct 124 ms 219332 KB Output is correct
16 Correct 116 ms 219400 KB Output is correct
17 Correct 116 ms 219204 KB Output is correct
18 Correct 114 ms 219280 KB Output is correct
19 Correct 116 ms 219340 KB Output is correct
20 Correct 117 ms 219248 KB Output is correct
21 Correct 119 ms 218724 KB Output is correct
22 Correct 116 ms 219256 KB Output is correct
23 Correct 117 ms 219388 KB Output is correct
24 Correct 121 ms 219372 KB Output is correct
25 Correct 124 ms 219392 KB Output is correct
26 Correct 120 ms 219060 KB Output is correct
27 Correct 133 ms 218692 KB Output is correct
28 Correct 116 ms 219036 KB Output is correct
29 Correct 113 ms 219008 KB Output is correct
30 Correct 116 ms 218824 KB Output is correct
31 Correct 1750 ms 490216 KB Output is correct
32 Correct 221 ms 224988 KB Output is correct
33 Correct 1601 ms 472356 KB Output is correct
34 Correct 1647 ms 473228 KB Output is correct
35 Correct 1707 ms 489684 KB Output is correct
36 Correct 1664 ms 489336 KB Output is correct
37 Correct 1276 ms 446160 KB Output is correct
38 Correct 1211 ms 445588 KB Output is correct
39 Correct 930 ms 401092 KB Output is correct
40 Correct 942 ms 412372 KB Output is correct
41 Correct 1530 ms 431372 KB Output is correct
42 Correct 1392 ms 434016 KB Output is correct
43 Correct 183 ms 227208 KB Output is correct
44 Correct 1508 ms 427944 KB Output is correct
45 Correct 1422 ms 406444 KB Output is correct
46 Correct 1222 ms 362900 KB Output is correct
47 Correct 726 ms 356880 KB Output is correct
48 Correct 679 ms 345872 KB Output is correct
49 Correct 802 ms 377404 KB Output is correct
50 Correct 943 ms 420700 KB Output is correct
51 Correct 849 ms 364576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3395 ms 774276 KB Output is correct
2 Correct 3000 ms 713336 KB Output is correct
3 Runtime error 3814 ms 1048580 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5152 ms 841532 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 120 ms 218384 KB Output is correct
2 Correct 111 ms 218412 KB Output is correct
3 Correct 112 ms 218380 KB Output is correct
4 Correct 113 ms 218396 KB Output is correct
5 Correct 138 ms 218456 KB Output is correct
6 Correct 131 ms 219272 KB Output is correct
7 Correct 121 ms 219368 KB Output is correct
8 Correct 119 ms 219264 KB Output is correct
9 Correct 124 ms 219236 KB Output is correct
10 Correct 120 ms 219344 KB Output is correct
11 Correct 147 ms 219084 KB Output is correct
12 Correct 119 ms 218968 KB Output is correct
13 Correct 119 ms 218848 KB Output is correct
14 Correct 114 ms 218864 KB Output is correct
15 Correct 124 ms 219332 KB Output is correct
16 Correct 116 ms 219400 KB Output is correct
17 Correct 116 ms 219204 KB Output is correct
18 Correct 114 ms 219280 KB Output is correct
19 Correct 116 ms 219340 KB Output is correct
20 Correct 117 ms 219248 KB Output is correct
21 Correct 119 ms 218724 KB Output is correct
22 Correct 116 ms 219256 KB Output is correct
23 Correct 117 ms 219388 KB Output is correct
24 Correct 121 ms 219372 KB Output is correct
25 Correct 124 ms 219392 KB Output is correct
26 Correct 120 ms 219060 KB Output is correct
27 Correct 133 ms 218692 KB Output is correct
28 Correct 116 ms 219036 KB Output is correct
29 Correct 113 ms 219008 KB Output is correct
30 Correct 116 ms 218824 KB Output is correct
31 Correct 1750 ms 490216 KB Output is correct
32 Correct 221 ms 224988 KB Output is correct
33 Correct 1601 ms 472356 KB Output is correct
34 Correct 1647 ms 473228 KB Output is correct
35 Correct 1707 ms 489684 KB Output is correct
36 Correct 1664 ms 489336 KB Output is correct
37 Correct 1276 ms 446160 KB Output is correct
38 Correct 1211 ms 445588 KB Output is correct
39 Correct 930 ms 401092 KB Output is correct
40 Correct 942 ms 412372 KB Output is correct
41 Correct 1530 ms 431372 KB Output is correct
42 Correct 1392 ms 434016 KB Output is correct
43 Correct 183 ms 227208 KB Output is correct
44 Correct 1508 ms 427944 KB Output is correct
45 Correct 1422 ms 406444 KB Output is correct
46 Correct 1222 ms 362900 KB Output is correct
47 Correct 726 ms 356880 KB Output is correct
48 Correct 679 ms 345872 KB Output is correct
49 Correct 802 ms 377404 KB Output is correct
50 Correct 943 ms 420700 KB Output is correct
51 Correct 849 ms 364576 KB Output is correct
52 Correct 1335 ms 431488 KB Output is correct
53 Correct 1362 ms 426844 KB Output is correct
54 Correct 1494 ms 477832 KB Output is correct
55 Correct 1426 ms 447876 KB Output is correct
56 Correct 1362 ms 447928 KB Output is correct
57 Correct 1509 ms 433828 KB Output is correct
58 Correct 1445 ms 444484 KB Output is correct
59 Correct 1416 ms 442124 KB Output is correct
60 Correct 1455 ms 436632 KB Output is correct
61 Correct 360 ms 250156 KB Output is correct
62 Correct 1235 ms 439596 KB Output is correct
63 Correct 1550 ms 469356 KB Output is correct
64 Correct 1622 ms 476156 KB Output is correct
65 Correct 1648 ms 484408 KB Output is correct
66 Correct 1595 ms 447756 KB Output is correct
67 Correct 352 ms 235204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 218384 KB Output is correct
2 Correct 111 ms 218412 KB Output is correct
3 Correct 112 ms 218380 KB Output is correct
4 Correct 113 ms 218396 KB Output is correct
5 Correct 138 ms 218456 KB Output is correct
6 Correct 131 ms 219272 KB Output is correct
7 Correct 121 ms 219368 KB Output is correct
8 Correct 119 ms 219264 KB Output is correct
9 Correct 124 ms 219236 KB Output is correct
10 Correct 120 ms 219344 KB Output is correct
11 Correct 147 ms 219084 KB Output is correct
12 Correct 119 ms 218968 KB Output is correct
13 Correct 119 ms 218848 KB Output is correct
14 Correct 114 ms 218864 KB Output is correct
15 Correct 124 ms 219332 KB Output is correct
16 Correct 116 ms 219400 KB Output is correct
17 Correct 116 ms 219204 KB Output is correct
18 Correct 114 ms 219280 KB Output is correct
19 Correct 116 ms 219340 KB Output is correct
20 Correct 117 ms 219248 KB Output is correct
21 Correct 119 ms 218724 KB Output is correct
22 Correct 116 ms 219256 KB Output is correct
23 Correct 117 ms 219388 KB Output is correct
24 Correct 121 ms 219372 KB Output is correct
25 Correct 124 ms 219392 KB Output is correct
26 Correct 120 ms 219060 KB Output is correct
27 Correct 133 ms 218692 KB Output is correct
28 Correct 116 ms 219036 KB Output is correct
29 Correct 113 ms 219008 KB Output is correct
30 Correct 116 ms 218824 KB Output is correct
31 Correct 1750 ms 490216 KB Output is correct
32 Correct 221 ms 224988 KB Output is correct
33 Correct 1601 ms 472356 KB Output is correct
34 Correct 1647 ms 473228 KB Output is correct
35 Correct 1707 ms 489684 KB Output is correct
36 Correct 1664 ms 489336 KB Output is correct
37 Correct 1276 ms 446160 KB Output is correct
38 Correct 1211 ms 445588 KB Output is correct
39 Correct 930 ms 401092 KB Output is correct
40 Correct 942 ms 412372 KB Output is correct
41 Correct 1530 ms 431372 KB Output is correct
42 Correct 1392 ms 434016 KB Output is correct
43 Correct 183 ms 227208 KB Output is correct
44 Correct 1508 ms 427944 KB Output is correct
45 Correct 1422 ms 406444 KB Output is correct
46 Correct 1222 ms 362900 KB Output is correct
47 Correct 726 ms 356880 KB Output is correct
48 Correct 679 ms 345872 KB Output is correct
49 Correct 802 ms 377404 KB Output is correct
50 Correct 943 ms 420700 KB Output is correct
51 Correct 849 ms 364576 KB Output is correct
52 Correct 3395 ms 774276 KB Output is correct
53 Correct 3000 ms 713336 KB Output is correct
54 Runtime error 3814 ms 1048580 KB Execution killed with signal 9
55 Halted 0 ms 0 KB -