Submission #1109997

# Submission time Handle Problem Language Result Execution time Memory
1109997 2024-11-08T11:50:37 Z tosivanmak Road Construction (JOI21_road_construction) C++17
100 / 100
2228 ms 399972 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long

struct Persist{
    int seg[10000000],lc[10000000],rc[10000000];
    ll ptr=1;
    ll build(ll l, ll r){
        if(l==r){
            seg[ptr]=1e18,lc[ptr]=rc[ptr]=-1; ptr++; return ptr-1;
        }
        else{
            ll mid=(l+r)>>1;
            ll curptr=ptr; ptr++;
            lc[curptr]=build(l,mid); rc[curptr]=build(mid+1,r);
            seg[curptr]=1e18;
            return curptr;
        }
    }
    ll upd(ll ul, ll l, ll r, ll val, ll id){
        if(l==r){
            seg[ptr]=val; lc[ptr]=rc[ptr]=-1; ptr++; return ptr-1;
        }
        else{
            ll mid=(l+r)>>1;
            ll curp=ptr; ptr++;
            if(ul<=mid){
                lc[curp]=upd(ul,l,mid,val,lc[id]); rc[curp]=rc[id];
            }
            else{
                rc[curp]=upd(ul,mid+1,r,val,rc[id]); lc[curp]=lc[id];
            }
            seg[curp]=min(seg[lc[curp]],seg[rc[curp]]);
            return curp;
        }
    }
    ll qry(ll ql, ll qr, ll l, ll r, ll id){
        if(ql>qr)return 1e18;
        if(l>qr||r<ql)return 1e18;
        if(l>=ql&&r<=qr){
            return seg[id];
        }
        ll mid=(l+r)>>1;
        return min(qry(ql,qr,l,mid,lc[id]),qry(ql,qr,mid+1,r,rc[id]));
    }
    ll bs(ll l, ll r, ll k, ll id){
        if(l==r)return l;
        ll mid=(l+r)>>1;
        if(seg[lc[id]]==k)return bs(l,mid,k,lc[id]);
        return bs(mid+1,r,k,rc[id]);
    }
    ll find(ll ql, ll qr, ll l, ll r, ll id, ll k){
        if(ql>qr)return -1;
        if(l>qr||r<ql)return -1;
        if(l>=ql&&r<=qr){
            if(seg[id]!=k) return -1;
            return bs(l,r,k,id);
        }
        ll mid=(l+r)>>1;
        ll bruh=find(ql,qr,l,mid,lc[id],k);
        if(bruh!=-1)return bruh;
        bruh=find(ql,qr,mid+1,r,rc[id],k);
        return bruh;
    }
}st,st2;
struct THING{
    ll val,verx,very,insegpos;
    bool operator<(const THING& th)const{
        return val>th.val;
    }
};
priority_queue<THING>pq;
int version[250005][3];
int prevver,prevver2;
vector<pair<ll,ll> >disc;
ll pos(pair<ll,ll>x){
    return lower_bound(disc.begin(),disc.end(),x)-disc.begin()+1;
}
int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n,k;
    cin>>n>>k;
    ll x[n+5],y[n+5];
    for(int i=1;i<=n;i++)cin>>x[i]>>y[i];
    vector<pair<ll,ll> >pts;
    for(int i=1;i<=n;i++)pts.push_back({x[i],y[i]});
    sort(pts.begin(),pts.end());
    for(int i=0;i<pts.size();i++){
        disc.push_back({pts[i].second,i});
    }
    sort(disc.begin(),disc.end());
    prevver=st.build(1,disc.size()); prevver2=st2.build(1,disc.size());
    for(int i=0;i<pts.size();i++){
        int xx=pts[i].first,yy=pts[i].second;
        version[i][1]=prevver; version[i][2]=prevver2;
        prevver=st.upd(pos(make_pair(yy,i)),1,disc.size(),-xx-yy,prevver);
        prevver2=st2.upd(pos(make_pair(yy,i)),1,disc.size(),-xx+yy,prevver2);
    }
    for(int i=0;i<pts.size();i++){
        int xx=pts[i].first,yy=pts[i].second;
        ll posi=pos(make_pair(yy,i));
        ll for1=st.qry(1,posi,1,disc.size(),version[i][1]);
        ll segtreebst=st.find(1,posi,1,disc.size(),version[i][1],for1);
        pq.push({for1+xx+yy,i,1,segtreebst});
        ll for2=st2.qry(posi+1,disc.size(),1,disc.size(),version[i][2]);
        segtreebst=st2.find(posi+1,disc.size(),1,disc.size(),version[i][2],for2);
        pq.push({for2+xx-yy,i,2,segtreebst});
    }
    for(int i=1;i<=k;i++){
        THING lol=pq.top();
        ll val=lol.val,verx=lol.verx,very=lol.very,insegpos=lol.insegpos;
        ll xx=pts[verx].first,yy=pts[verx].second;
        pq.pop();
        cout<<val<<'\n';
        ll posi=pos(make_pair(yy,verx));
        if(very==1){
            version[verx][very]=st.upd(insegpos,1,disc.size(),1e18,version[verx][very]);
            ll for1=st.qry(1,posi,1,disc.size(),version[verx][1]);
            ll segtreebst=st.find(1,posi,1,disc.size(),version[verx][1],for1);
            pq.push({for1+xx+yy,verx,1,segtreebst});
        }
        else{
            version[verx][very]=st2.upd(insegpos,1,disc.size(),1e18,version[verx][very]);
            ll for2=st2.qry(posi+1,disc.size(),1,disc.size(),version[verx][2]);
            ll segtreebst=st2.find(posi+1,disc.size(),1,disc.size(),version[verx][2],for2);
            pq.push({for2+xx-yy,verx,2,segtreebst});
        }
    }
}

Compilation message

road_construction.cpp: In function 'int32_t main()':
road_construction.cpp:90:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for(int i=0;i<pts.size();i++){
      |                 ~^~~~~~~~~~~
road_construction.cpp:95:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |     for(int i=0;i<pts.size();i++){
      |                 ~^~~~~~~~~~~
road_construction.cpp:101:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for(int i=0;i<pts.size();i++){
      |                 ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 200 ms 80968 KB Output is correct
2 Correct 187 ms 81196 KB Output is correct
3 Correct 182 ms 77128 KB Output is correct
4 Correct 154 ms 77128 KB Output is correct
5 Correct 175 ms 79892 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 945 ms 395604 KB Output is correct
2 Correct 908 ms 395644 KB Output is correct
3 Correct 113 ms 76872 KB Output is correct
4 Correct 587 ms 395260 KB Output is correct
5 Correct 639 ms 395476 KB Output is correct
6 Correct 621 ms 395656 KB Output is correct
7 Correct 677 ms 394912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1135 ms 285196 KB Output is correct
2 Correct 1207 ms 285368 KB Output is correct
3 Correct 3 ms 12624 KB Output is correct
4 Correct 332 ms 285268 KB Output is correct
5 Correct 658 ms 285172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1135 ms 285196 KB Output is correct
2 Correct 1207 ms 285368 KB Output is correct
3 Correct 3 ms 12624 KB Output is correct
4 Correct 332 ms 285268 KB Output is correct
5 Correct 658 ms 285172 KB Output is correct
6 Correct 1285 ms 285148 KB Output is correct
7 Correct 1216 ms 285184 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 1258 ms 285240 KB Output is correct
11 Correct 353 ms 285300 KB Output is correct
12 Correct 645 ms 285200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 80968 KB Output is correct
2 Correct 187 ms 81196 KB Output is correct
3 Correct 182 ms 77128 KB Output is correct
4 Correct 154 ms 77128 KB Output is correct
5 Correct 175 ms 79892 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 1197 ms 222968 KB Output is correct
8 Correct 1230 ms 222896 KB Output is correct
9 Correct 157 ms 75036 KB Output is correct
10 Correct 617 ms 222384 KB Output is correct
11 Correct 725 ms 222136 KB Output is correct
12 Correct 462 ms 222944 KB Output is correct
13 Correct 633 ms 221372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 80968 KB Output is correct
2 Correct 187 ms 81196 KB Output is correct
3 Correct 182 ms 77128 KB Output is correct
4 Correct 154 ms 77128 KB Output is correct
5 Correct 175 ms 79892 KB Output is correct
6 Correct 4 ms 14940 KB Output is correct
7 Correct 945 ms 395604 KB Output is correct
8 Correct 908 ms 395644 KB Output is correct
9 Correct 113 ms 76872 KB Output is correct
10 Correct 587 ms 395260 KB Output is correct
11 Correct 639 ms 395476 KB Output is correct
12 Correct 621 ms 395656 KB Output is correct
13 Correct 677 ms 394912 KB Output is correct
14 Correct 1135 ms 285196 KB Output is correct
15 Correct 1207 ms 285368 KB Output is correct
16 Correct 3 ms 12624 KB Output is correct
17 Correct 332 ms 285268 KB Output is correct
18 Correct 658 ms 285172 KB Output is correct
19 Correct 1285 ms 285148 KB Output is correct
20 Correct 1216 ms 285184 KB Output is correct
21 Correct 2 ms 12624 KB Output is correct
22 Correct 2 ms 12624 KB Output is correct
23 Correct 1258 ms 285240 KB Output is correct
24 Correct 353 ms 285300 KB Output is correct
25 Correct 645 ms 285200 KB Output is correct
26 Correct 1197 ms 222968 KB Output is correct
27 Correct 1230 ms 222896 KB Output is correct
28 Correct 157 ms 75036 KB Output is correct
29 Correct 617 ms 222384 KB Output is correct
30 Correct 725 ms 222136 KB Output is correct
31 Correct 462 ms 222944 KB Output is correct
32 Correct 633 ms 221372 KB Output is correct
33 Correct 2228 ms 397760 KB Output is correct
34 Correct 2222 ms 399972 KB Output is correct
35 Correct 1498 ms 396948 KB Output is correct
36 Correct 956 ms 397440 KB Output is correct
37 Correct 948 ms 397652 KB Output is correct
38 Correct 1117 ms 396172 KB Output is correct