Submission #386475

#TimeUsernameProblemLanguageResultExecution timeMemory
386475VROOM_VARUNEvent Hopping 2 (JOI21_event2)C++14
100 / 100
633 ms43968 KiB
/* ID: varunra2 LANG: C++ TASK: hopping2 */ #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define debug_arr(...) \ cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__) #pragma GCC diagnostic ignored "-Wsign-compare" //#pragma GCC diagnostic ignored "-Wunused-parameter" //#pragma GCC diagnostic ignored "-Wunused-variable" #else #define debug(...) 42 #endif #define EPS 1e-9 #define IN(A, B, C) assert(B <= A && A <= C) #define INF (int)1e9 #define MEM(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define MP make_pair #define PB push_back #define all(cont) cont.begin(), cont.end() #define rall(cont) cont.end(), cont.begin() #define x first #define y second const double PI = acos(-1.0); typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef map<int, int> MPII; typedef multiset<int> MSETI; typedef set<int> SETI; typedef set<string> SETS; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<VI> VVI; typedef vector<string> VS; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto& a : x) #define sz(x) (int)(x).size() typedef pair<int, int> pii; typedef vector<int> vi; #pragma GCC diagnostic ignored "-Wsign-compare" // util functions int n, k; VII vals; struct fenwick { int n; VI bit; void init(int _n) { n = _n; bit.assign(n + 1, 0); } void upd(int ind, int val) { while (ind <= n) { bit[ind] += val; ind += (ind & (-ind)); } } int qry(int ind) { int ret = 0; while (ind >= 1) { ret += bit[ind]; ind -= (ind & (-ind)); } return ret; } int qry(int l, int r) { return qry(r) - qry(l - 1); } }; MPII compress(VI pnts) { sort(all(pnts)); pnts.erase(unique(all(pnts)), pnts.end()); int cnt = 0; MPII ret; for (int i = 0; i < sz(pnts); i++) { ret[pnts[i]] = cnt++; } return ret; } MPII ans; MPII cmp; VVI lft; const int bits = 20; void init() { vals.resize(n); lft.resize(n); trav(x, lft) x.resize(bits); } bool ok(int ind, int r) { if (ind == -1) return false; return vals[ind].y <= r; } int calc(int l, int r) { // calculate answer for this range int start = ans[l]; if (!ok(start, r)) return 0; int ret = 0; for (int i = bits - 1; i >= 0; i--) { if (ok(lft[start][i], r)) { start = lft[start][i]; ret += (1 << i); } } return ret + 1; } int main() { // #ifndef ONLINE_JUDGE // freopen("hopping2.in", "r", stdin); // freopen("hopping2.out", "w", stdout); // #endif cin.sync_with_stdio(0); cin.tie(0); cin >> n >> k; init(); trav(x, vals) cin >> x.x >> x.y; VI pnts; trav(x, vals) pnts.PB(x.x), pnts.PB(x.y); cmp = compress(pnts); trav(x, vals) x.x = cmp[x.x], x.y = cmp[x.y]; PII bst(INF, -1); VVI evnts; rep(i, 0, n) { evnts.PB({vals[i].x, 1, i}); evnts.PB({vals[i].y, 0, i}); } sort(all(evnts), greater<VI>()); for (auto& x : evnts) { if (x[1] == 1) { bst = min(bst, MP(vals[x[2]].y, x[2])); } else { ans[vals[x[2]].y] = bst.y; } } ans[0] = bst.y; for (int i = 0; i < n; i++) { lft[i][0] = ans[vals[i].y]; } for (int j = 1; j < bits; j++) { for (int i = 0; i < n; i++) { int to = lft[i][j - 1]; if (to == -1) lft[i][j] = -1; else lft[i][j] = lft[to][j - 1]; } } VI ret; int cnt = 0; set<PII> gps; gps.insert(MP(0, 2 * n)); int gpcnt = calc(0, 2 * n); for (int i = 0; i < n; i++) { // debug(i); if (cnt == k) break; int l = vals[i].x; int r = vals[i].y; auto it = gps.upper_bound(MP(l, INF)); if (it == gps.begin()) continue; it--; auto x = *it; // debug(gps); int rl = x.x; int rr = x.y; if (!(l >= rl and r <= rr)) continue; // rl, rr -> (rl, l), (r, rr) // calculate contribution of rl, rr (a) // calculate contribution of rl, l (b) // calculate contribution of r, rr (c) // this is a good range, add this mf int a = calc(rl, rr); int b = calc(rl, l); int c = calc(r, rr); // debug(a, b, c, cnt, gpcnt); // debug(l, r); // debug(rl, rr); // debug(cnt + gpcnt - a + b + c + 1); if (cnt + gpcnt - a + b + c + 1 >= k) { ret.PB(i + 1); cnt++; gpcnt -= a; gpcnt += (b + c); gps.erase(it); PII pl(rl, l); PII pr(r, rr); if (pl.x != pl.y) { gps.insert(pl); } if (pr.x != pr.y) { gps.insert(pr); } } } // debug(ret); if (sz(ret) != k) { cout << "-1\n"; return 0; } trav(x, ret) cout << x << '\n'; return 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...