답안 #917829

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
917829 2024-01-29T00:32:45 Z CSQ31 새 집 (APIO18_new_home) C++17
0 / 100
1416 ms 321720 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
//solving only for negative slopes
//two adjacent stores at a and b causes a line at x = (a+b+1)/2 with y = b-x
const int MAXN = 3e5+1;
int n,q,k;
vector<int>ans(MAXN,-1),T;
multiset<int,greater<int>>lines[MAXN];
map<int,vector<pii>>pt;
map<int,int>crd;
int N;
void upd(int v,int l,int r,int pos){
    if(l==r){
        if(lines[l].empty())T[v] = 1e9;
        else T[v] = *lines[l].begin();
        return;
    }
    int tm = (l+r)/2;
    if(pos<=tm)upd(2*v,l,tm,pos);
    else upd(2*v+1,tm+1,r,pos);
    T[v] = max(T[2*v],T[2*v+1]);
}
int query(int v,int l,int r,int tl,int tr){
    if(l>r)return -1e9;
    if(l==tl && r==tr)return T[v];
    int tm = (tl+tr)/2;
    return max(query(2*v,l,min(r,tm),tl,tm),
               query(2*v+1,max(l,tm+1),r,tm+1,tr));
}
void add(int l,int r){
    int x = (l+r+1)/2;
    int y = r-x;
    //cout<<"add "<<x<<" "<<y<<'\n';
    lines[crd[x]].insert(x+y);
    upd(1,0,N,crd[x]);
}
void del(int l,int r){
    int x = (l+r+1)/2;
    int y = r-x;
    //cout<<"del "<<x<<" "<<y<<'\n';
    lines[crd[x]].erase(lines[crd[x]].find((x+y)));
    upd(1,0,N,crd[x]);
}
bool debug = 0;
vector<int>comp;
void solve(){
        vector<multiset<int>>store(k+1);
        comp = {0};
        crd.clear();
        for(auto z:pt){
            //cout<<z.fi<<'\n';
            vector<pii>v = z.se;
            for(pii st : v){
                int t = st.fi;
                int x = st.se;
                if(t > k){
                    //query
                    comp.pb(x);
                    continue;
                }
                //cout<<t<<" "<<x<<'\n';
                if(t > 0){ 
                    //add this guy
                    if(store[t].find(x) != store[t].end()){
                        store[t].insert(x);
                        continue;
                    }
                    auto r = store[t].lb(x);
                    //if nothing to the right of x we do nothing
                    if(r != store[t].end())comp.pb((x + *r + 1)/2);

                    if(r == store[t].begin()){} //insert 0
                    else {
                        r--;
                        comp.pb((x+*r+1)/2);
                    }
                    store[t].insert(x);
                }else{ 
                    t*=-1;
                    //remove this guy
                    store[t].erase(store[t].find(x));
                    if(store[t].find(x) != store[t].end())continue;

                    auto r = store[t].lb(x);
                    if(r != store[t].end() && r != store[t].begin()){
                        auto l = r;
                        l--;
                        comp.pb((*l+*r+1)/2);
                    }
                }
            }
        }

        sort(all(comp));
        comp.resize(unique(all(comp)) - comp.begin());
        for(int i=0;i<sz(comp);i++)crd[comp[i]] = i;
        N = sz(comp)-1;
        T.assign(4*(N+1),-1e9);

        for(int i=1;i<=k;i++)store[i].clear();

        int open = 0;
        for(auto z:pt){
        vector<pii>v = z.se;
        for(pii st : v){
            int t = st.fi;
            int x = st.se;
            if(t > k)continue;
            //cout<<"store "<<t<<" "<<x<<'\n';
            if(t > 0){ 
                if(store[t].empty())open++;
                if(store[t].find(x) != store[t].end()){
                    store[t].insert(x);
                    continue;
                }
                //l .... x .... r
                auto l = store[t].lb(x);
                auto r = l;
                if(r != store[t].end())add(x,*r);

                if(r == store[t].begin())add(-x,x); //add a point at 0
                else{
                    l--;
                    add(*l,x);
                }
                if(r != store[t].begin() && r != store[t].end())del(*l,*r);
                store[t].insert(x);
            }else{ 
                t*=-1;
                if(sz(store[t]) == 1)open--;
                //remove this guy
                store[t].erase(store[t].find(x));
                if(store[t].find(x) != store[t].end())continue;
                
                auto l = store[t].lb(x);
                auto r = l;
                if(r != store[t].end() && r != store[t].begin()){
                    l--;
                    add(*l,*r);
                }
                if(r == store[t].begin()){
                    del(-x,x);
                    add(-*r,*r);
                }

                if(r != store[t].end())del(x,*r);
                if(r != store[t].begin())del(*l,x);
            }
        }
        for(pii st:v){
            if(st.fi <= k)continue;
            int id = st.fi - k - 1;
            int x = st.se;
            if(open!=k)ans[id] = -1;
            else ans[id] = max(ans[id],query(1,0,crd[x],0,N) - x);
            //cout<<"query "<<id<<" "<<x<<" "<<query(1,0,crd[x],0,N)<<'\n';
        }
    }


}
int main()
{
    owo
    cin>>n>>k>>q;
    vector<int>comp = {0};
    for(int i=0;i<n;i++){
        int x,t,a,b;
        cin>>x>>t>>a>>b;
        pt[a].pb({t,x}); //add
        pt[b+1].pb({-t,x}); //remove
    }
    for(int i=0;i<q;i++){
        int l,y;
        cin>>l>>y;
        pt[y].pb({i+k+1,l});
    }
    solve();
    for(auto &z:pt){
        for(pii &x : z.se)x.se = 100000000 - x.se;
    }
    solve();

    
    for(int i=0;i<q;i++)cout<<ans[i]<<'\n';


}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 15708 KB Output is correct
2 Runtime error 17 ms 31580 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 15708 KB Output is correct
2 Runtime error 17 ms 31580 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 740 ms 224456 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1416 ms 321720 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 15708 KB Output is correct
2 Runtime error 17 ms 31580 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 15708 KB Output is correct
2 Runtime error 17 ms 31580 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -