답안 #757602

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757602 2023-06-13T12:18:38 Z Sam_a17 Road Construction (JOI21_road_construction) C++17
0 / 100
10000 ms 714820 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <atcoder/all>
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
 
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(Tree <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 

 
// for random generations
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
// mt19937 myrand(131);
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
 
long long ka() {
	long long x = 0;
	bool z = false;
	while (1)
	{
		char y = getchar();
		if (y == '-')
			z = true;
		else if ('0' <= y && y <= '9')
			x = x * 10 + y - '0';
		else
		{
			if (z)
				x *= -1;
			return x;
		}
	}
}
 
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  } else if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
}
 
const long long inf = 1e10;
const int N = 3e5 + 10;
int n, k;
pair<long long, long long> pr[N];
long long p[N];

bool is_construct = false;;
vector<long long> pit;

struct segTree {
  Tree<pair<long long, long long>> tree[2 * N];
  int size = 1;

  void init(int s) {
    while(size < s) {
      size *= 2;
    }

    for(int i = 0; i < 2 * size; i++) {
      tree[i].clear();
    }
  }

  void upd(int u, long long val, int ind, int x, int lx, int rx) {
    tree[x].insert({val, ind});

    if(rx - lx == 1) {
      return;
    }

    int mid = (lx + rx) / 2;
    if(u < mid) {
      upd(u, val, ind, 2 * x + 1, lx, mid);
    } else {
      upd(u, val, ind, 2 * x + 2, mid, rx);
    }
  }

  void upd(int u, long long val, int ind) {
    upd(u, val, ind, 0, 0, size);
  }

  long long qry(int l, int r, long long value, long long xx, int x, int lx, int rx) {
    if(lx >= r || rx <= l) {
      return 0;
    }

    if(lx >= l && rx <= r) {
      long long pat = tree[x].order_of_key({value, inf});
      if(is_construct) {
        for(auto i: tree[x]) {
          if(i.first <= value) {
            pit.push_back(i.first + xx);
          } else {
            break;
          }
        }
      }

      return pat;
    }

    int mid = (lx + rx) / 2;
    long long s1 = qry(l, r, value, xx, 2 * x + 1, lx, mid);
    long long s2 = qry(l, r, value, xx, 2 * x + 2, mid, rx);
    return s1 + s2;
  }

  long long qry(int l, int r, long long value, long long xx) {
    return qry(l, r, value, xx, 0, 0, size);
  }
};

vector<int> compress;
segTree seg_left, seg_right;

long long check(long long mid) {
  long long cnt = 0;

  seg_left.init(n + 1);
  seg_right.init(n + 1);
  
  for(int i = 1; i <= n; i++) {
    long long need_left = mid - (pr[i].first + pr[i].second);
    long long need_right = mid - (pr[i].first - pr[i].second);

    cnt += seg_left.qry(0, p[i] + 1, need_left, pr[i].first + pr[i].second);
    cnt += seg_right.qry(p[i] + 1, n + 1, need_right, pr[i].first - pr[i].second);
    
    seg_left.upd(p[i], -pr[i].first - pr[i].second, i);
    seg_right.upd(p[i], -pr[i].first + pr[i].second, i);
  }

  return cnt;
}

void construct(long long mid) {
  long long cnt = 0;

  seg_left.init(sz(compress) + 1);
  seg_right.init(sz(compress) + 1);
  
  is_construct = true;
  for(int i = 1; i <= n; i++) {
    long long need_left = mid - (pr[i].first + pr[i].second);
    long long need_right = mid - (pr[i].first - pr[i].second);

    seg_left.qry(0, p[i] + 1, need_left, pr[i].first + pr[i].second);
    seg_right.qry(p[i] + 1, n + 1, need_right, pr[i].first - pr[i].second);

    seg_left.upd(p[i], -pr[i].first - pr[i].second, i);
    seg_right.upd(p[i], -pr[i].first + pr[i].second, i);
  }
}


void solve_() {
  cin >> n >> k;

  for(int i = 1; i <= n; i++) {
    cin >> pr[i].first >> pr[i].second;
    compress.push_back(pr[i].second);
  }

  sort(pr + 1, pr + 1 + n);
  sort(all(compress));
  uniq(compress);

  for(int i = 1; i <= n; i++) {
    p[i] = lower_bound(all(compress), pr[i].second) - compress.begin();
  }

  long long ina = 0, inb = 2e9, ans = 0;
  while(ina <= inb) {
    long long mid = (ina + inb) / 2;
    if(check(mid) < k) {
      ans = mid;
      ina = mid + 1;
    } else {
      inb = mid - 1;
    }
  }

  construct(ans);
  vector<long long> pat = pit;
  sort(all(pat));
  while(sz(pat) < k) {
    pat.push_back(ans + 1);
  }

  for(auto i: pat) {
    cout << i << '\n';
  }
}
 
int main() {
  setIO();
 
  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1; 
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
}

Compilation message

road_construction.cpp: In function 'void construct(long long int)':
road_construction.cpp:199:13: warning: unused variable 'cnt' [-Wunused-variable]
  199 |   long long cnt = 0;
      |             ^~~
road_construction.cpp: In function 'void setIO(std::string)':
road_construction.cpp:94:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
road_construction.cpp:95:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
road_construction.cpp:97:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
road_construction.cpp:98:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 120980 KB Output is correct
2 Correct 302 ms 121060 KB Output is correct
3 Incorrect 225 ms 120328 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10097 ms 714820 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10092 ms 629736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10092 ms 629736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 120980 KB Output is correct
2 Correct 302 ms 121060 KB Output is correct
3 Incorrect 225 ms 120328 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 285 ms 120980 KB Output is correct
2 Correct 302 ms 121060 KB Output is correct
3 Incorrect 225 ms 120328 KB Output isn't correct
4 Halted 0 ms 0 KB -