Submission #996436

# Submission time Handle Problem Language Result Execution time Memory
996436 2024-06-10T15:20:13 Z imarn Road Construction (JOI21_road_construction) C++14
0 / 100
5065 ms 2097152 KB
#include<bits/stdc++.h>
#define ll int
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int mxn=3e5+5;
int id[mxn]{0},rev[mxn]{0};
struct node{
    pll v;
    node*l,*r;
    node(): v({2e9,-1}),l(NULL),r(NULL){};
    node(ll x,ll y) : v({x,y}),l(NULL),r(NULL){};
    node(node*l,node*r) : v(min(l->v,r->v)),l(l),r(r){};
};map<pll,bool>mp;
node*pre[2][mxn],*suf[2][mxn];
pll p[mxn];
pair<pll,int>y[mxn];
node* build(node*t,int l,int r){
    t=new node();
    if(l==r)return new node(2e9,l);
    int m=(l+r)>>1;
    return new node(build(t->l,l,m),build(t->r,m+1,r));
}
node *update(node*t,int l,int r,int id,ll v){
    if(l==r)return new node(v,id);
    int m=(l+r)>>1;
    if(id<=m)return new node(update(t->l,l,m,id,v),t->r);
    else return new node(t->l,update(t->r,m+1,r,id,v));
}
pll qr(node*t,int l,int r,int tl,int tr){
    if(r<tl||l>tr)return {2e9,-1};
    if(r<=tr&&l>=tl)return t->v;
    int m=(l+r)>>1;
    return min(qr(t->l,l,m,tl,tr),qr(t->r,m+1,r,tl,tr));
}
bool cmp(pair<pll,ll>a,pair<pll,ll>b){
    if(a.f.s==b.f.s)return a<b;
    return a.f.s<b.f.s;
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,k;cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>p[i].f>>p[i].s,y[i].f=p[i],y[i].s=0;
    sort(y+1,y+n+1);for(int i=1;i<=n;i++)y[i].s=i;
    sort(y+1,y+n+1,cmp);for(int i=1;i<=n;i++)id[y[i].s]=i,rev[i]=y[i].s;
    sort(p+1,p+n+1);
    pre[0][1]=build(pre[0][1],1,n);
    pre[1][1]=build(pre[1][1],1,n);
    for(int i=2;i<=n;i++){
        pre[0][i]=update(pre[0][i-1],1,n,id[i-1],-p[i-1].f-p[i-1].s);
        pre[1][i]=update(pre[1][i-1],1,n,id[i-1],-p[i-1].f+p[i-1].s);
    }suf[0][n]=build(suf[0][n],1,n);suf[1][n]=build(suf[1][n],1,n);
    for(int i=n-1;i>=1;i--){
        suf[0][i]=update(suf[0][i+1],1,n,id[i+1],p[i+1].f-p[i+1].s);
        suf[1][i]=update(suf[1][i+1],1,n,id[i+1],p[i+1].f+p[i+1].s);
    }priority_queue<pair<ll,pll>,vector<pair<ll,pll>>,greater<pair<ll,pll>>>pq;
    for(int i=1;i<=n;i++){
        pll ans1 = qr(pre[0][i],1,n,1,id[i]);
        ans1.f+=p[i].f+p[i].s;
        pll ans2 = qr(pre[1][i],1,n,id[i],n);
        ans2.f+=p[i].f-p[i].s;
        pll ans3 = qr(suf[0][i],1,n,1,id[i]);
        ans3.f+=-p[i].f+p[i].s;
        pll ans4 = qr(suf[1][i],1,n,id[i],n);
        ans4.f+=-p[i].f-p[i].s;
        ll res=2e9;int mem=-1;
        if(res>ans1.f&&ans1.s!=-1)res=ans1.f,mem=ans1.s;
        if(res>ans2.f&&ans2.s!=-1)res=ans2.f,mem=ans2.s;
        if(res>ans3.f&&ans3.s!=-1)res=ans3.f,mem=ans3.s;
        if(res>ans4.f&&ans4.s!=-1)res=ans4.f,mem=ans4.s;
        pq.push({res,{i,mem}});
    }vector<ll>ans;ll cnt=k;
    while(cnt>0){
        auto u=pq.top();pq.pop();
        if(mp.find({u.s.f,rev[u.s.s]})==mp.end()){
            ans.pb(u.f);mp[{u.s.f,rev[u.s.s]}]=mp[{rev[u.s.s],u.s.f}]=1;cnt--;
        }
        int i=u.s.f;
        pre[0][i]=update(pre[0][i],1,n,u.s.s,2e9);
        pre[1][i]=update(pre[1][i],1,n,u.s.s,2e9);
        suf[0][i]=update(suf[0][i],1,n,u.s.s,2e9);
        suf[1][i]=update(suf[1][i],1,n,u.s.s,2e9);
        pll ans1 = qr(pre[0][i],1,n,1,id[i]);ans1.f+=p[i].f+p[i].s;
        pll ans2 = qr(pre[1][i],1,n,id[i],n);ans2.f+=p[i].f-p[i].s;
        pll ans3 = qr(suf[0][i],1,n,1,id[i]);ans3.f+=-p[i].f+p[i].s;
        pll ans4 = qr(suf[1][i],1,n,id[i],n);ans4.f+=-p[i].f-p[i].s;
        ll res=2e9;int mem=-1;
        if(res>ans1.f&&ans1.s!=-1)res=ans1.f,mem=ans1.s;
        if(res>ans2.f&&ans2.s!=-1)res=ans2.f,mem=ans2.s;
        if(res>ans3.f&&ans3.s!=-1)res=ans3.f,mem=ans3.s;
        if(res>ans4.f&&ans4.s!=-1)res=ans4.f,mem=ans4.s;
        if(mem==-1)continue;
        pq.push({res,{i,mem}});
    }
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]<<'\n';
    }
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:99:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |     for(int i=0;i<ans.size();i++){
      |                 ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2683 ms 728736 KB Output is correct
2 Correct 2626 ms 728712 KB Output is correct
3 Runtime error 3729 ms 2097152 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4932 ms 1961580 KB Output is correct
2 Correct 5065 ms 1961500 KB Output is correct
3 Runtime error 3126 ms 2097152 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2559 ms 744028 KB Output is correct
2 Correct 2420 ms 743420 KB Output is correct
3 Incorrect 1 ms 4444 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2559 ms 744028 KB Output is correct
2 Correct 2420 ms 743420 KB Output is correct
3 Incorrect 1 ms 4444 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2683 ms 728736 KB Output is correct
2 Correct 2626 ms 728712 KB Output is correct
3 Runtime error 3729 ms 2097152 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2683 ms 728736 KB Output is correct
2 Correct 2626 ms 728712 KB Output is correct
3 Runtime error 3729 ms 2097152 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -