Submission #622788

# Submission time Handle Problem Language Result Execution time Memory
622788 2022-08-04T14:31:29 Z dantoh000 Road Construction (JOI21_road_construction) C++14
100 / 100
5887 ms 31144 KB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii, int> iii;
int n, k;
int x[250005], y[250005];
vector<iii> P;
vector<int> ys;
int fw[250005];
void up(int x, int nv){
    while (x <= 250000){
        fw[x] += nv;
        x += x&-x;
    }
}
int qu(int x){
    int ret = 0;
    while (x){
        ret += fw[x];
        x -= x&-x;
    }
    return ret;
}
inline int lb(int y){
    return lower_bound(ys.begin(), ys.end(), y) - ys.begin() + 1;
}
inline int ub(int y){
    return upper_bound(ys.begin(), ys.end(), y) - ys.begin() + 1;
}


int test(int D){
    int c = 0;
    int ret = 0;
    for (auto it : P){
        int X = it.fi.fi, Y = it.fi.se;
        while (c < P.size() && P[c].fi.fi <= X + D){
            up(lb(P[c].fi.se), 1);
            c++;
        }
        up(lb(Y), -1);

        ret += qu(ub(Y + D) - 1) - qu(lb(Y-D) - 1);
        ///printf("%d %d %d\n",X,Y,id);
    }
    ///printf("%lld %lld\n",D,ret);
    return ret;
}
vector<int> solve(int D){
    int c = 0;
    vector<int> ret;
    set<ii> S;
    for (auto it : P){
        int X = it.fi.fi, Y = it.fi.se, id = it.se;
        while (c < P.size() && P[c].fi.fi <= X + D){
            S.insert({lb(P[c].fi.se), P[c].se});
            c++;
        }
        S.erase({lb(Y), id});
        for (auto it = S.lower_bound({lb(Y-D), -1}); it != S.end() && it != S.upper_bound({ub(Y+D),-1}); it++){
            int idx = it->se;
            ret.push_back(abs(x[idx] - x[id]) + abs(y[idx] - y[id]));
        }
        ///printf("%d %d %d\n",X,Y,id);
    }
    ///printf("%lld %lld\n",D,ret);
    return ret;

}



main(){
    scanf("%lld%lld",&n,&k);
    for (int i = 0; i < n; i++){
        scanf("%lld%lld",&x[i],&y[i]);
        int nx = x[i]+y[i];
        int ny = x[i]-y[i];
        ys.push_back(ny);
        P.push_back({{nx, ny}, i});
    }
    sort(ys.begin(), ys.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    sort(P.begin(), P.end());

    int lo = 0, hi = 4000000000ll;
    while (lo < hi){
        int mid = (lo+hi)/2;
        if (test(mid) < k) lo = mid+1;
        else hi = mid;
    }
    vector<int> ans = solve(lo);
    sort(ans.begin(), ans.end());
    for (int i = 0; i < k; i++){
        printf("%lld\n",ans[i]);
    }


}

Compilation message

road_construction.cpp: In function 'long long int test(long long int)':
road_construction.cpp:40:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         while (c < P.size() && P[c].fi.fi <= X + D){
      |                ~~^~~~~~~~~~
road_construction.cpp: In function 'std::vector<long long int> solve(long long int)':
road_construction.cpp:58:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         while (c < P.size() && P[c].fi.fi <= X + D){
      |                ~~^~~~~~~~~~
road_construction.cpp: At global scope:
road_construction.cpp:76:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   76 | main(){
      | ^~~~
road_construction.cpp: In function 'int main()':
road_construction.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |     scanf("%lld%lld",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
road_construction.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         scanf("%lld%lld",&x[i],&y[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 60 ms 5088 KB Output is correct
2 Correct 69 ms 5044 KB Output is correct
3 Correct 54 ms 5104 KB Output is correct
4 Correct 84 ms 5228 KB Output is correct
5 Correct 56 ms 3968 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2200 ms 22212 KB Output is correct
2 Correct 2234 ms 22220 KB Output is correct
3 Correct 72 ms 5056 KB Output is correct
4 Correct 2161 ms 24528 KB Output is correct
5 Correct 2109 ms 26084 KB Output is correct
6 Correct 2114 ms 26184 KB Output is correct
7 Correct 2110 ms 24256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5059 ms 19272 KB Output is correct
2 Correct 5236 ms 19280 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2271 ms 20816 KB Output is correct
5 Correct 1896 ms 25416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5059 ms 19272 KB Output is correct
2 Correct 5236 ms 19280 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2271 ms 20816 KB Output is correct
5 Correct 1896 ms 25416 KB Output is correct
6 Correct 5390 ms 19276 KB Output is correct
7 Correct 5687 ms 19272 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 5113 ms 19072 KB Output is correct
11 Correct 2115 ms 20840 KB Output is correct
12 Correct 1808 ms 17616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 5088 KB Output is correct
2 Correct 69 ms 5044 KB Output is correct
3 Correct 54 ms 5104 KB Output is correct
4 Correct 84 ms 5228 KB Output is correct
5 Correct 56 ms 3968 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 1999 ms 13804 KB Output is correct
8 Correct 2232 ms 13808 KB Output is correct
9 Correct 55 ms 5160 KB Output is correct
10 Correct 1836 ms 13068 KB Output is correct
11 Correct 1818 ms 12748 KB Output is correct
12 Correct 647 ms 14204 KB Output is correct
13 Correct 737 ms 13404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 5088 KB Output is correct
2 Correct 69 ms 5044 KB Output is correct
3 Correct 54 ms 5104 KB Output is correct
4 Correct 84 ms 5228 KB Output is correct
5 Correct 56 ms 3968 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 2200 ms 22212 KB Output is correct
8 Correct 2234 ms 22220 KB Output is correct
9 Correct 72 ms 5056 KB Output is correct
10 Correct 2161 ms 24528 KB Output is correct
11 Correct 2109 ms 26084 KB Output is correct
12 Correct 2114 ms 26184 KB Output is correct
13 Correct 2110 ms 24256 KB Output is correct
14 Correct 5059 ms 19272 KB Output is correct
15 Correct 5236 ms 19280 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2271 ms 20816 KB Output is correct
18 Correct 1896 ms 25416 KB Output is correct
19 Correct 5390 ms 19276 KB Output is correct
20 Correct 5687 ms 19272 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 5113 ms 19072 KB Output is correct
24 Correct 2115 ms 20840 KB Output is correct
25 Correct 1808 ms 17616 KB Output is correct
26 Correct 1999 ms 13804 KB Output is correct
27 Correct 2232 ms 13808 KB Output is correct
28 Correct 55 ms 5160 KB Output is correct
29 Correct 1836 ms 13068 KB Output is correct
30 Correct 1818 ms 12748 KB Output is correct
31 Correct 647 ms 14204 KB Output is correct
32 Correct 737 ms 13404 KB Output is correct
33 Correct 5887 ms 25032 KB Output is correct
34 Correct 5528 ms 25016 KB Output is correct
35 Correct 5262 ms 23812 KB Output is correct
36 Correct 1887 ms 31144 KB Output is correct
37 Correct 1733 ms 31120 KB Output is correct
38 Correct 2216 ms 25196 KB Output is correct