Submission #386292

#TimeUsernameProblemLanguageResultExecution timeMemory
386292model_codeEvent Hopping 2 (JOI21_event2)C++17
100 / 100
258 ms28800 KiB
#include <algorithm> #include <iostream> #include <set> #include <vector> using namespace std; constexpr int MAX_N = 100'000; constexpr int MAX_logN = 18; constexpr int MAX_LR = 1'000'000'000; int N, K; int L[MAX_N], R[MAX_N]; vector<int> z; vector<int> invL[MAX_N * 2 + 1]; int doubling[MAX_N * 2 + 1][MAX_logN]; int count(int l, int r) { int res = 0; for (int j = MAX_logN - 1; j >= 0; j--) { if (doubling[l][j] <= r) { l = doubling[l][j]; res += 1 << j; } } return res; } int main() { scanf("%d%d", &N, &K); for (int i = 0; i < N; i++) { scanf("%d%d", L + i, R + i); z.push_back(L[i]); z.push_back(R[i]); } sort(z.begin(), z.end()); z.erase(unique(z.begin(), z.end()), z.end()); for (int i = 0; i < N; i++) { L[i] = lower_bound(z.begin(), z.end(), L[i]) - z.begin(); R[i] = lower_bound(z.begin(), z.end(), R[i]) - z.begin(); } for (int i = 0; i < N; i++) invL[L[i]].push_back(i); int r = z.size(); for (int i = z.size(); i >= 0; i--) { for (int j = 0; j < invL[i].size(); j++) r = min(r, R[invL[i][j]]); doubling[i][0] = r; } for (int i = 1; i < MAX_logN; i++) { for (int j = 0; j <= z.size(); j++) { doubling[j][i] = doubling[doubling[j][i - 1]][i - 1]; } } int now = count(0, z.size() - 1); if (now < K) { printf("-1\n"); return 0; } set<pair<int, int>> used = {{0, 0}, {z.size() - 1, z.size() - 1}}; for (int i = 0; used.size() - 2 < K; i++) { int l = prev(used.lower_bound({R[i], 0}))->second; int r = used.lower_bound({R[i], 0})->first; if (L[i] < l) continue; int tmp = now - count(l, r) + count(l, L[i]) + 1 + count(R[i], r); if (tmp >= K) { printf("%d\n", i + 1); now = tmp; used.insert({L[i], R[i]}); } } }

Compilation message (stderr)

event2.cpp: In function 'int main()':
event2.cpp:45:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |         for (int j = 0; j < invL[i].size(); j++) r = min(r, R[invL[i][j]]);
      |                         ~~^~~~~~~~~~~~~~~~
event2.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         for (int j = 0; j <= z.size(); j++) {
      |                         ~~^~~~~~~~~~~
event2.cpp:60:37: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |     for (int i = 0; used.size() - 2 < K; i++) {
      |                     ~~~~~~~~~~~~~~~~^~~
event2.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   29 |     scanf("%d%d", &N, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~
event2.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   31 |         scanf("%d%d", L + i, R + i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...