Submission #747815

#TimeUsernameProblemLanguageResultExecution timeMemory
747815Sam_a17Event Hopping 2 (JOI21_event2)C++17
100 / 100
373 ms39268 KiB
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> //#include "temp.cpp" #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 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(long double t) {cerr << t;} void print(unsigned long long t) {cerr << t;} 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(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 << "]";} #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define nl '\n' // 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}; // 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); } } // Indexed Set template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 1e5 + 10, LOG = 21; int up[N][LOG]; int n, k; // segment Tree Max // Range Queries struct segTree { vector<pair<long long, long long>> mTree; int size; void init(long long n) { size = 1; while(size < n) { size *= 2; } mTree.assign(2 * size - 1, {INT64_MAX, -1}); } pair<long long, long long> combine(pair<long long, long long> a, pair<long long, long long> b) { if(a.first < b.first) { return a; } else if(a.first > b.first) { return b; } else { return {a.first, min(a.second, b.second)}; } } void upd(int u, long long v, int ind, int x, int lx, int rx) { // set value at pos u if(rx - lx == 1) { if(v < mTree[x].first) { mTree[x].first = v; mTree[x].second = ind; } return; } int m = (lx + rx) / 2; if(u < m) { upd(u, v, ind, 2 * x + 1, lx, m); }else { upd(u, v, ind, 2 * x + 2, m, rx); } mTree[x] = combine(mTree[2 * x + 1], mTree[2 * x + 2]); } void upd(int u, long long v, int ind) { upd(u, v, ind, 0, 0, size); } pair<long long, long long> qry (int l, int r, int x, int lx, int rx) { // range queries if(l >= rx || lx >= r) { return {INT64_MAX, -1}; } if(lx >= l && r >= rx) { return mTree[x]; } int m = (rx + lx) / 2; auto s1 = qry(l, r, 2 * x + 1, lx, m); auto s2 = qry(l, r, 2 * x + 2, m, rx); return combine(s1, s2); } pair<long long, long long> qry(int l, int r) { return qry(l, r, 0, 0, size); } }; segTree seg_max; struct segmTree { struct node { long long value, lazy; }; int size = 1; vector<node> tree; void init(int s) { while(s > size) { size *= 2; } tree.assign(2 * size - 1, {0, 0}); } void propagate(int x, int lx, int rx) { if(rx - lx == 1 || !tree[x].lazy) { tree[x].lazy = 0; return; } int mid = (lx + rx) / 2; tree[2 * x + 1].value += tree[x].lazy * (mid - lx); tree[2 * x + 1].lazy += tree[x].lazy; tree[2 * x + 2].value += tree[x].lazy * (rx - mid); tree[2 * x + 2].lazy += tree[x].lazy; tree[x].lazy = 0; } void upd(int l, int r, long long value, int x, int lx, int rx) { propagate(x, lx, rx); if(lx >= r || rx <= l) { return; } if(lx >= l && rx <= r) { tree[x] = {tree[x].value + value * (rx - lx), value}; propagate(x, lx, rx); return; } int mid = (lx + rx) / 2; upd(l, r, value, 2 * x + 1, lx, mid); upd(l, r, value, 2 * x + 2, mid, rx); tree[x].value = tree[2 * x + 1].value + tree[2 * x + 2].value; } void upd(int l, int r, long long value) { upd(l, r, value, 0, 0, size); } long long qry(int l, int r, int x, int lx, int rx) { propagate(x, lx, rx); if(lx >= r || rx <= l) { return 0; } if(lx >= l && rx <= r) { return tree[x].value; } int mid = (lx + rx) / 2; long long s1 = qry(l, r, 2 * x + 1, lx, mid); long long s2 = qry(l, r, 2 * x + 2, mid, rx); return s1 + s2; } long long qry(int l, int r) { return qry(l, r, 0, 0, size); } }; segmTree seg, del; vector<pair<pair<int, int>, int>> a; pair<int, int> arr[N]; int calc(int l, int r) { auto u = seg_max.qry(l, r + 1); if(u.first > r) { return 0; } int curr = u.second, go = 1; for(int i = LOG - 1; i >= 0; i--) { if(up[curr][i] < n) { int ind = up[curr][i]; if(arr[ind].second <= r) { curr = up[curr][i]; go += (1 << i); } } } return go; } void solve_() { cin >> n >> k; vector<int> compress; for(int i = 1; i <= n; i++) { int l, r; cin >> l >> r; a.push_back({{r, l}, i - 1}); // compress.push_back(l); compress.push_back(r); } sort(all(a)); sort(all(compress)); uniq(compress); seg_max.init(sz(compress) + 4); for(int i = 0; i < n; i++) { a[i].first.first = lower_bound(all(compress), a[i].first.first) - compress.begin() + 1; a[i].first.second = lower_bound(all(compress), a[i].first.second) - compress.begin() + 1; arr[a[i].second] = {a[i].first.second, a[i].first.first}; seg_max.upd(a[i].first.second, a[i].first.first, a[i].second); } int it = 0; for(int i = 0; i < n; i++) { while(it < n && a[it].first.second < a[i].first.first) { it++; } if(it == n) { up[a[i].second][0] = n; } else { up[a[i].second][0] = a[it].second; } } for(int i = 0; i < LOG; i++) { up[n][i] = n; } for(int j = 1; j < LOG; j++) { for(int i = 0; i < n; i++) { up[i][j] = up[up[i][j - 1]][j - 1]; } } set<int> st; st.insert(0); st.insert(sz(compress) + 2); if(calc(0, sz(compress) + 2) < k) { cout << -1 << '\n'; return; } seg.init(sz(compress) + 5); vector<int> answ; int all = calc(0, sz(compress) + 2); for(int i = 0; i < n; i++) { // dbg(arr[i]) int s1 = seg.qry(arr[i].first, arr[i].second); if(s1) continue; st.insert(arr[i].first); st.insert(arr[i].second); auto itl = prev(st.find(arr[i].first)); auto itr = next(st.find(arr[i].second)); int new_all = all - calc(*itl, *itr); new_all += calc(*itl, arr[i].first) + 1 + calc(arr[i].second, *itr); if(new_all >= k) { answ.push_back(i + 1); // dbg(make_pair(arr[i].first, arr[i].second)) seg.upd(arr[i].first, arr[i].second, 1); all = new_all; } else { if(!seg.qry(arr[i].first, arr[i].first + 1)) st.erase(arr[i].first); if(!seg.qry(arr[i].second, arr[i].second + 1)) st.erase(arr[i].second); } if(sz(answ) == k) break; } assert(sz(answ) == k); for(auto i: answ) { 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 (stderr)

event2.cpp: In function 'void setIO(std::string)':
event2.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
event2.cpp:67:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...