Submission #393041

# Submission time Handle Problem Language Result Execution time Memory
393041 2021-04-22T15:29:02 Z nvmdava Road Construction (JOI21_road_construction) C++17
100 / 100
1383 ms 23984 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1000005;
const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
 
vector<pair<ll, int> > s, d;
 
ll a[N], b[N];
int loc[N], w[N], rev[N];
int n;
ll k;

int fen[N];

void update(int x, int v){
    while(x <= n){
        fen[x] += v;
        x += x & -x;
    }
}

int query(int x){
    int res = 0;
    while(x){
        res += fen[x];
        x -= x & -x;
    }
    return res;
}
 
vector<ll> ans;
int l1[N], r1[N];
bool count(ll len){
    ll cnt = 0;
    l1[0] = 1;
    r1[0] = 1;
    for(int i = 1; i <= n; ++i){
        l1[i] = l1[i - 1];
        while(a[l1[i]] < a[i] - len)
            ++l1[i];
        r1[i] = r1[i - 1];
        while(r1[i] < n && a[r1[i] + 1] <= a[i] + len)
            ++r1[i];
    }

    int l = 1;
    for(int i = 1; i <= n; ++i){
        while(b[l] < b[i] - len)
            update(w[l++], -1);
        cnt += query(r1[w[i]]) - query(l1[w[i]] - 1);
        update(w[i], 1);
    }
    for(; l <= n; ++l)
        update(w[l], -1);
    return cnt < k;
}

void find(ll len){
    set<pair<ll, int> > in;
    int l = 1;
    for(int i = 1; i <= n; ++i){
        while(a[l] < a[i] - len){
            in.erase({b[rev[l]], l});
            ++l;
        }
        auto it = in.lower_bound({b[rev[i]] - len, 0});
        while(it != in.end() && it -> ff <= b[rev[i]] + len){
            ans.push_back(max(abs(b[rev[i]] - it -> ff), abs(a[i] - a[it -> ss])));
            ++it;
        }
        in.insert({b[rev[i]], i});
    }
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    cin>>n>>k;
 
    for(int x, y, i = 1; i <= n; ++i){
        cin>>x>>y;
        s.push_back({x + y, i});
        d.push_back({x - y, i});
    }
    sort(s.begin(), s.end());
    sort(d.begin(), d.end());
 
    for(int i = 1; i <= n; ++i){
        a[i] = s[i - 1].ff;
        loc[s[i - 1].ss] = i;
    }
    for(int i = 1; i <= n; ++i){
        b[i] = d[i - 1].ff;
        w[i] = loc[d[i - 1].ss];
        rev[w[i]] = i;
    }
    
    ll len = 0;
    for(int i = 31; i >= 0; --i)
        if(count((1LL << i) | len))
            len |= 1LL << i;
    find(len);
    sort(ans.begin(), ans.end());
    ++len;
    while(ans.size() < k)
        ans.push_back(len);
    for(auto& x : ans)
        cout<<x<<'\n';
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:112:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  112 |     while(ans.size() < k)
      |           ~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 57 ms 5040 KB Output is correct
2 Correct 56 ms 5152 KB Output is correct
3 Correct 53 ms 5168 KB Output is correct
4 Correct 52 ms 5112 KB Output is correct
5 Correct 49 ms 3964 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 908 ms 23308 KB Output is correct
2 Correct 913 ms 23200 KB Output is correct
3 Correct 48 ms 5040 KB Output is correct
4 Correct 870 ms 23068 KB Output is correct
5 Correct 705 ms 23328 KB Output is correct
6 Correct 732 ms 23308 KB Output is correct
7 Correct 739 ms 22420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1160 ms 18188 KB Output is correct
2 Correct 1196 ms 18192 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 669 ms 18212 KB Output is correct
5 Correct 1114 ms 18208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1160 ms 18188 KB Output is correct
2 Correct 1196 ms 18192 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 669 ms 18212 KB Output is correct
5 Correct 1114 ms 18208 KB Output is correct
6 Correct 1226 ms 18200 KB Output is correct
7 Correct 1243 ms 18124 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1224 ms 18100 KB Output is correct
11 Correct 673 ms 18108 KB Output is correct
12 Correct 1132 ms 18120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 5040 KB Output is correct
2 Correct 56 ms 5152 KB Output is correct
3 Correct 53 ms 5168 KB Output is correct
4 Correct 52 ms 5112 KB Output is correct
5 Correct 49 ms 3964 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 532 ms 11528 KB Output is correct
8 Correct 542 ms 11568 KB Output is correct
9 Correct 51 ms 5196 KB Output is correct
10 Correct 508 ms 10756 KB Output is correct
11 Correct 458 ms 10624 KB Output is correct
12 Correct 422 ms 11532 KB Output is correct
13 Correct 426 ms 10456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 5040 KB Output is correct
2 Correct 56 ms 5152 KB Output is correct
3 Correct 53 ms 5168 KB Output is correct
4 Correct 52 ms 5112 KB Output is correct
5 Correct 49 ms 3964 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 908 ms 23308 KB Output is correct
8 Correct 913 ms 23200 KB Output is correct
9 Correct 48 ms 5040 KB Output is correct
10 Correct 870 ms 23068 KB Output is correct
11 Correct 705 ms 23328 KB Output is correct
12 Correct 732 ms 23308 KB Output is correct
13 Correct 739 ms 22420 KB Output is correct
14 Correct 1160 ms 18188 KB Output is correct
15 Correct 1196 ms 18192 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 669 ms 18212 KB Output is correct
18 Correct 1114 ms 18208 KB Output is correct
19 Correct 1226 ms 18200 KB Output is correct
20 Correct 1243 ms 18124 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1224 ms 18100 KB Output is correct
24 Correct 673 ms 18108 KB Output is correct
25 Correct 1132 ms 18120 KB Output is correct
26 Correct 532 ms 11528 KB Output is correct
27 Correct 542 ms 11568 KB Output is correct
28 Correct 51 ms 5196 KB Output is correct
29 Correct 508 ms 10756 KB Output is correct
30 Correct 458 ms 10624 KB Output is correct
31 Correct 422 ms 11532 KB Output is correct
32 Correct 426 ms 10456 KB Output is correct
33 Correct 1383 ms 23984 KB Output is correct
34 Correct 1364 ms 23952 KB Output is correct
35 Correct 1307 ms 23176 KB Output is correct
36 Correct 1090 ms 23736 KB Output is correct
37 Correct 1142 ms 23984 KB Output is correct
38 Correct 1186 ms 22448 KB Output is correct