Submission #724073

# Submission time Handle Problem Language Result Execution time Memory
724073 2023-04-14T16:49:04 Z sysia Road Construction (JOI21_road_construction) C++17
65 / 100
10000 ms 61616 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

struct Tree {
	vector<int> s;
    Tree(){}
	Tree(int n) : s(n) {}
    void clear(){ s.assign((int)s.size(), 0);}
	void update(int pos) { // a[pos] += dif
		for (; pos < (int)s.size(); pos |= pos + 1) s[pos]++;
	}
	int query(int pos) { // sum of values in [0, pos)
		int res = 0;
		for (; pos > 0; pos &= pos - 1) res += s[pos-1];
		return res;
	}
	int query(int l, int r){
        return query(r+1) - query(l);
    }
};

#define int long long
typedef pair<int, int> T;
const int oo2 = 1e9+7;

void solve(){
    int n, k; cin >> n >> k;
    vector<T>p, p2(n);
    for (auto &[x, y]: p2) {
        cin >> x >> y;
        p.emplace_back(x+y, y-x);
    }
    vector<tuple<int, int, int8_t>>sweep;
    vector<int>s;
    Tree t(3*n+3);
    auto check = [&](int m){
        // debug(p);
        sweep.clear();
        s.clear();
        for (auto [x, y]: p){
            sweep.emplace_back(y-m, x, -1);
            sweep.emplace_back(y+m, x, +1);
            sweep.emplace_back(y, x, 0);
            s.emplace_back(x-m);
            s.emplace_back(x);
            s.emplace_back(x+m);
        }
        stable_sort(s.begin(), s.end());
        s.erase(unique(s.begin(), s.end()), s.end());
        t.clear();
        auto get = [&](int x)->int {
            return lower_bound(s.begin(), s.end(), x) - s.begin();
        };
        stable_sort(sweep.begin(), sweep.end());
        int all = -n;
        for (auto &[y, x, what]: sweep){
            if (what == 0){
                t.update(get(x));
            } else {
                all += what * t.query(get(x-m), get(x+m));
                if (all/2 >= k) return true;
            }
        }
        return (all/2 >= k);
    };
    int l = 1, r = oo2 * 2;
    int d = oo2 * 2;
    while (r >= l){
        int m = (l+r)/2;
        if (check(m)){
            d = m;
            r = m-1;
        } else l = m+1;
    }
    d--;
    set<T>ss;
    vector<int>ret;
    stable_sort(p2.begin(), p2.end());
    int j = 0;
    for (int i = 0; i<n; i++){
        auto [x, y] = p2[i];
        while (j < i && p2[i].first - p2[j].first > d) {
            ss.erase({p2[j].second, p2[j].first});
            j++;
        }
        for (auto it = ss.lower_bound({y-d, x}); it != ss.upper_bound({y+d, x}); it++){
            ret.emplace_back(abs(x-it->second) + abs(y - it->first));
        }
        ss.insert({y, x});
    }
    while ((int)ret.size() < k) ret.emplace_back(d+1);
    stable_sort(ret.begin(), ret.end());
    for (int i = 0; i<k; i++) cout << ret[i] << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 82 ms 6108 KB Output is correct
2 Correct 90 ms 6128 KB Output is correct
3 Correct 59 ms 5224 KB Output is correct
4 Correct 68 ms 5212 KB Output is correct
5 Correct 76 ms 4012 KB Output is correct
6 Correct 14 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5137 ms 61568 KB Output is correct
2 Correct 5087 ms 61556 KB Output is correct
3 Correct 56 ms 5068 KB Output is correct
4 Correct 4919 ms 61316 KB Output is correct
5 Correct 5146 ms 61560 KB Output is correct
6 Correct 5150 ms 61616 KB Output is correct
7 Correct 5013 ms 60796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9443 ms 60408 KB Output is correct
2 Correct 9555 ms 60408 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4762 ms 60444 KB Output is correct
5 Correct 5410 ms 60436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9443 ms 60408 KB Output is correct
2 Correct 9555 ms 60408 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4762 ms 60444 KB Output is correct
5 Correct 5410 ms 60436 KB Output is correct
6 Correct 9069 ms 60408 KB Output is correct
7 Correct 9461 ms 60456 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 9313 ms 60404 KB Output is correct
11 Correct 4603 ms 60440 KB Output is correct
12 Correct 5373 ms 60408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 6108 KB Output is correct
2 Correct 90 ms 6128 KB Output is correct
3 Correct 59 ms 5224 KB Output is correct
4 Correct 68 ms 5212 KB Output is correct
5 Correct 76 ms 4012 KB Output is correct
6 Correct 14 ms 604 KB Output is correct
7 Correct 3940 ms 27112 KB Output is correct
8 Correct 3948 ms 27100 KB Output is correct
9 Correct 59 ms 5188 KB Output is correct
10 Correct 3422 ms 26460 KB Output is correct
11 Correct 3001 ms 26336 KB Output is correct
12 Correct 2011 ms 27188 KB Output is correct
13 Correct 2257 ms 25572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 6108 KB Output is correct
2 Correct 90 ms 6128 KB Output is correct
3 Correct 59 ms 5224 KB Output is correct
4 Correct 68 ms 5212 KB Output is correct
5 Correct 76 ms 4012 KB Output is correct
6 Correct 14 ms 604 KB Output is correct
7 Correct 5137 ms 61568 KB Output is correct
8 Correct 5087 ms 61556 KB Output is correct
9 Correct 56 ms 5068 KB Output is correct
10 Correct 4919 ms 61316 KB Output is correct
11 Correct 5146 ms 61560 KB Output is correct
12 Correct 5150 ms 61616 KB Output is correct
13 Correct 5013 ms 60796 KB Output is correct
14 Correct 9443 ms 60408 KB Output is correct
15 Correct 9555 ms 60408 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 4762 ms 60444 KB Output is correct
18 Correct 5410 ms 60436 KB Output is correct
19 Correct 9069 ms 60408 KB Output is correct
20 Correct 9461 ms 60456 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 9313 ms 60404 KB Output is correct
24 Correct 4603 ms 60440 KB Output is correct
25 Correct 5373 ms 60408 KB Output is correct
26 Correct 3940 ms 27112 KB Output is correct
27 Correct 3948 ms 27100 KB Output is correct
28 Correct 59 ms 5188 KB Output is correct
29 Correct 3422 ms 26460 KB Output is correct
30 Correct 3001 ms 26336 KB Output is correct
31 Correct 2011 ms 27188 KB Output is correct
32 Correct 2257 ms 25572 KB Output is correct
33 Execution timed out 10071 ms 60516 KB Time limit exceeded
34 Halted 0 ms 0 KB -