Submission #709432

#TimeUsernameProblemLanguageResultExecution timeMemory
709432cig32Road Construction (JOI21_road_construction)C++17
5 / 100
10102 ms292956 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update #include <ext/pb_ds/detail/standard_policies.hpp> using namespace std; #define int long long using namespace __gnu_pbds; typedef tree<pair<pair<int,int>,int>,null_type,less<pair<pair<int,int>,int> >,rb_tree_tag,tree_order_statistics_node_update>ordered_set; const int MAXN = 250010; const int MOD = 1e9 + 7; mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x, int y) { int u = uniform_int_distribution<int>(x, y)(rng); return u; } int bm(int b, int p) { if(p==0) return 1 % MOD; int r = bm(b, p >> 1); if(p&1) return (((r*r) % MOD) * b) % MOD; return (r*r) % MOD; } int inv(int b) { return bm(b, MOD-2); } int fastlog(int x) { return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1); } void printcase(int i) { cout << "Case #" << i << ": "; } bool cmp1(pair<int,int> x, pair<int,int> y) { return x.first+x.second < y.first+y.second; } bool cmp2(pair<int,int> x, pair<int,int> y) { return x.first-x.second < y.first-y.second; } ordered_set bit[MAXN]; // 2D bit in regular matrices but only supporting single add/del int id = 0; map<pair<int, int>,vector<int> > mp; void insert(int x, int y) { id++; mp[{y, x}].push_back(id); int fixx = x; for(;x<MAXN;x+=x&-x) bit[x].insert({{y, fixx}, id}); } void erase(int x, int y) { int ono = mp[{y, x}].back(); mp[{y, x}].pop_back(); int fixx = x; for(;x<MAXN;x+=x&-x) bit[x].erase({{y, fixx}, ono}); } int query(int x, int y) { int sum = 0; for(;x;x-=x&-x) sum += bit[x].order_of_key({{y+1, 0}, 0}); return sum; } vector<pair<int,int>> exhaust(int x, int y) { vector<pair<int, int> > g; for(;x;x-=x&-x) { if(bit[x].empty()) continue; int b = bit[x].order_of_key({{y+1, 0}, 0}); auto it = bit[x].begin(); for(int i=0; i<b; i++) { pair<int, int> get = (*bit[x].find_by_order(i)).first; swap(get.first, get.second); if(i+1 < b) it++; g.push_back(get); } } return g; } void clear() { for(int i=0; i<MAXN; i++) bit[i].clear(); mp.clear(); id = 0; } int count(vector<pair<int,int> >v, int d) { int n = v.size(); set<int>xx,yy; for(int i=0; i<n; i++) { xx.insert(v[i].first); yy.insert(v[i].second); } map<int,int>isx,isy; int it=1; for(int x: xx) { isx[x] = it++; } it = 1; for(int y: yy) { isy[y] = it++; } vector<pair<int,int> > w = v; int ans=0; sort(v.begin(), v.end(), cmp1); for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second]; clear(); int j=0; for(int i=0; i<v.size(); i++) { while(j<i && (v[i].first+v[i].second)-(v[j].first+v[j].second) > d) { erase(w[j].first, w[j].second); j++; } ans += query(w[i].first, w[i].second); insert(w[i].first, w[i].second); } sort(v.begin(), v.end(), cmp2); for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second]; clear(); j=0; for(int i=0; i<v.size(); i++) { while(j<i && (v[i].first-v[i].second)-(v[j].first-v[j].second) > d) { erase(w[j].first, n+1-w[j].second); j++; } ans += query(w[i].first, n+1-w[i].second); insert(w[i].first, n+1-w[i].second); } vector<int> c[n+1]; for(int i=1; i<=n; i++) c[i].clear(); for(int i=0; i<v.size(); i++) c[w[i].first].push_back(v[i].second); for(int i=1; i<=n; i++) { sort(c[i].begin(), c[i].end()); for(int j=0; j+1<c[i].size(); j++) { int lb = j+1, rb = c[i].size() - 1; while(lb < rb) { int mid = (lb + rb + 1) >> 1; if(c[i][mid] - c[i][j] <= d) lb = mid; else rb = mid - 1; } if(c[i][lb] - c[i][j] <= d) { ans -= (lb - j); } } } for(int i=1; i<=n; i++) c[i].clear(); for(int i=0; i<v.size(); i++) c[w[i].second].push_back(v[i].first); for(int i=1; i<=n; i++) { sort(c[i].begin(), c[i].end()); for(int j=0; j+1<c[i].size(); j++) { int lb = j+1, rb = c[i].size() - 1; while(lb < rb) { int mid = (lb + rb + 1) >> 1; if(c[i][mid] - c[i][j] <= d) lb = mid; else rb = mid - 1; } if(c[i][lb] - c[i][j] <= d) ans -= (lb - j); } } return ans; } vector<int> all(vector<pair<int,int> >v, int d) { vector<int> result; int n = v.size(); set<int>xx,yy; for(int i=0; i<n; i++) { xx.insert(v[i].first); yy.insert(v[i].second); } map<int,int>isx,isy; int it=1; int realx[n+1], realy[n+1]; for(int x: xx) { isx[x] = it; realx[it] = x; it++; } it = 1; for(int y: yy) { isy[y] = it; realy[it] = y; it++; } vector<pair<int,int> > w = v; int ans=0; sort(v.begin(), v.end(), cmp1); for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second]; clear(); int j=0; for(int i=0; i<v.size(); i++) { while(j<i && (v[i].first+v[i].second)-(v[j].first+v[j].second) > d) { erase(w[j].first, w[j].second); j++; } vector<pair<int, int> > newadd = exhaust(w[i].first, w[i].second); int chek = query(w[i].first, w[i].second); for(pair<int, int> x: newadd) { result.push_back(v[i].first + v[i].second - realx[x.first] - realy[x.second]); } insert(w[i].first, w[i].second); } sort(v.begin(), v.end(), cmp2); for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second]; clear(); j=0; for(int i=0; i<v.size(); i++) { while(j<i && (v[i].first-v[i].second)-(v[j].first-v[j].second) > d) { erase(w[j].first, n+1-w[j].second); j++; } vector<pair<int, int> > newadd = exhaust(w[i].first, n+1-w[i].second); for(pair<int, int> x: newadd) { result.push_back(v[i].first - v[i].second - realx[x.first] + realy[n+1-x.second]); } insert(w[i].first, n+1-w[i].second); } vector<int> c[n+1]; for(int i=1; i<=n; i++) c[i].clear(); for(int i=0; i<v.size(); i++) c[w[i].first].push_back(v[i].second); for(int i=1; i<=n; i++) { sort(c[i].begin(), c[i].end()); for(int j=0; j+1<c[i].size(); j++) { int lb = j+1, rb = c[i].size() - 1; while(lb < rb) { int mid = (lb + rb + 1) >> 1; if(c[i][mid] - c[i][j] <= d) lb = mid; else rb = mid - 1; } if(c[i][lb] - c[i][j] <= d) { for(int k=j+1; k<=lb; k++) { result.push_back(-(c[i][k] - c[i][j])); } } } } for(int i=1; i<=n; i++) c[i].clear(); for(int i=0; i<v.size(); i++) c[w[i].second].push_back(v[i].first); for(int i=1; i<=n; i++) { sort(c[i].begin(), c[i].end()); for(int j=0; j+1<c[i].size(); j++) { int lb = j+1, rb = c[i].size() - 1; while(lb < rb) { int mid = (lb + rb + 1) >> 1; if(c[i][mid] - c[i][j] <= d) lb = mid; else rb = mid - 1; } if(c[i][lb] - c[i][j] <= d) { for(int k=j+1; k<=lb; k++) { result.push_back(-(c[i][k] - c[i][j])); } } } } return result; } void solve(int tc) { int n, k; cin >> n >> k; vector<pair<int,int> > points; for(int i=0; i<n; i++) { int x, y; cin >> x >> y; points.push_back({x, y}); } int lb = 0, rb = 4e9; while(lb < rb) { int mid = (lb + rb + 1) >> 1; int c = count(points, mid); if(c <= k) lb = mid; else rb = mid - 1; } vector<int> res = all(points, lb); sort(res.begin(), res.end()); vector<int> finres; map<int,int> cnt; for(int x: res) { if(x < 0) cnt[x]++; if(cnt[-x] > 0) { cnt[-x]--; } else if(x > 0) { finres.push_back(x); } } for(int x: finres) cout << x << "\n"; int finc = count(points, lb); if(finc < k) { rb = 4e9; lb++; while(lb < rb) { int mid = (lb + rb) >> 1; int c = count(points, mid); if(c != finc) rb = mid; else lb = mid + 1; } for(int i=finc+1; i<=k; i++) cout << lb << "\n"; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; for(int i=1; i<=t; i++) solve(i); }

Compilation message (stderr)

road_construction.cpp: In function 'long long int count(std::vector<std::pair<long long int, long long int> >, long long int)':
road_construction.cpp:94:17: 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]
   94 |   for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second];
      |                ~^~~~~~~~~
road_construction.cpp:98:17: 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]
   98 |   for(int i=0; i<v.size(); i++) {
      |                ~^~~~~~~~~
road_construction.cpp:107:17: 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]
  107 |   for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second];
      |                ~^~~~~~~~~
road_construction.cpp:111:17: 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]
  111 |   for(int i=0; i<v.size(); i++) {
      |                ~^~~~~~~~~
road_construction.cpp:121:17: 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]
  121 |   for(int i=0; i<v.size(); i++) c[w[i].first].push_back(v[i].second);
      |                ~^~~~~~~~~
road_construction.cpp:124:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |     for(int j=0; j+1<c[i].size(); j++) {
      |                  ~~~^~~~~~~~~~~~
road_construction.cpp:137:17: 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]
  137 |   for(int i=0; i<v.size(); i++) c[w[i].second].push_back(v[i].first);
      |                ~^~~~~~~~~
road_construction.cpp:140:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |     for(int j=0; j+1<c[i].size(); j++) {
      |                  ~~~^~~~~~~~~~~~
road_construction.cpp: In function 'std::vector<long long int> all(std::vector<std::pair<long long int, long long int> >, long long int)':
road_construction.cpp:179:17: 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]
  179 |   for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second];
      |                ~^~~~~~~~~
road_construction.cpp:182:17: 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]
  182 |   for(int i=0; i<v.size(); i++) {
      |                ~^~~~~~~~~
road_construction.cpp:188:9: warning: unused variable 'chek' [-Wunused-variable]
  188 |     int chek = query(w[i].first, w[i].second);
      |         ^~~~
road_construction.cpp:195:17: 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]
  195 |   for(int i=0; i<w.size(); i++) w[i].first = isx[v[i].first], w[i].second = isy[v[i].second];
      |                ~^~~~~~~~~
road_construction.cpp:198:17: 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]
  198 |   for(int i=0; i<v.size(); i++) {
      |                ~^~~~~~~~~
road_construction.cpp:211:17: 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]
  211 |   for(int i=0; i<v.size(); i++) c[w[i].first].push_back(v[i].second);
      |                ~^~~~~~~~~
road_construction.cpp:214:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  214 |     for(int j=0; j+1<c[i].size(); j++) {
      |                  ~~~^~~~~~~~~~~~
road_construction.cpp:229:17: 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]
  229 |   for(int i=0; i<v.size(); i++) c[w[i].second].push_back(v[i].first);
      |                ~^~~~~~~~~
road_construction.cpp:232:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  232 |     for(int j=0; j+1<c[i].size(); j++) {
      |                  ~~~^~~~~~~~~~~~
road_construction.cpp:177:7: warning: unused variable 'ans' [-Wunused-variable]
  177 |   int ans=0;
      |       ^~~
#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...