제출 #945787

#제출 시각아이디문제언어결과실행 시간메모리
945787beepbeepsheepRoad Construction (JOI21_road_construction)C++17
0 / 100
4210 ms45408 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>

#ifndef DEBUG
#define cerr if (0) cerr
#define endl '\n'
#endif // DEBUG

const ll maxn=2e5+5e4+5;
const ll inf=1e15;
const ll mod=1e9+7;

ll n;
ll x[maxn],y[maxn];
ii ranges[maxn];
vector<ll> pos;
vector<ii> updates;
ll fen[maxn];
void upd(ll x, ll v){
    while (x<maxn){
        fen[x]+=v;
        x+=(x&-x);
    }
}
ll query(ll x){
    ll ans=0;
    while (x){
        ans+=fen[x];
        x-=(x&-x);
    }
    return ans;
}
ll query(ll x, ll y){
    return query(y)-query(x-1);
}
ll check(ll len){
    vector<tuple<ll,ll,ll>> queries;
    for (int i=1;i<=n;i++){
        queries.emplace_back(y[i]-len-1,i,-1);
        //exclusive
        queries.emplace_back(y[i]+len,i,1);
        //inclusive
        ranges[i].first=lower_bound(pos.begin(),pos.end(),x[i]-len)
                        -pos.begin();
        ranges[i].second=upper_bound(pos.begin(),pos.end(),x[i]+len)
                        -pos.begin()-1;
    }
    sort(queries.begin(),queries.end());
    ll ans=0,ptr=0;
    memset(fen,0,sizeof(fen));
    for (auto [a,b,c]:queries){
        while (ptr!=updates.size() &&
               updates[ptr].first<=a){
            upd(updates[ptr].second,1);
            ptr++;
        }
        ans+=c*(query(ranges[b].first,ranges[b].second));
    }
    return (ans-n)/2;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll a,b,k;
    cin>>n>>k;
    assert(k==1);
    for (int i=1;i<=n;i++){
        cin>>a>>b;
        x[i]=a+b,y[i]=a-b;
        pos.emplace_back(x[i]);
    }
    pos.emplace_back(-inf);
    sort(pos.begin(),pos.end());
    pos.erase(unique(pos.begin(),pos.end()),pos.end());
    for (int i=1;i<=n;i++){
        ll res=lower_bound(pos.begin(),pos.end(),x[i])-pos.begin();
        updates.emplace_back(y[i],res);
        cerr<<res<<endl;
    }
    sort(updates.begin(),updates.end());
    ll l=1,r=2'000'000'005,m;
    while (l!=r-1){
        ll m=(l+r)>>1;
        if (check(m)) r=m;
        else l=m;
    }
    cout<<r;
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

road_construction.cpp: In function 'long long int check(long long int)':
road_construction.cpp:56:19: 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]
   56 |         while (ptr!=updates.size() &&
      |                ~~~^~~~~~~~~~~~~~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:85:28: warning: unused variable 'm' [-Wunused-variable]
   85 |     ll l=1,r=2'000'000'005,m;
      |                            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...