Submission #861207

#TimeUsernameProblemLanguageResultExecution timeMemory
861207TahirAliyevMatryoshka (JOI16_matryoshka)C++17
100 / 100
467 ms35136 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> using namespace std; struct DATA{ int h, id, r; }; const int MAX = 2e5 + 5, AMAX = 8e5 + 5; int n, q; pii arr[MAX]; pii Q[MAX]; vector<DATA> E; int tree[4 * AMAX]; void update(int node, int l, int r, int pos, int val){ if(l == r){ tree[node] = val; return; } int mid = (l + r) / 2; if(pos <= mid){ update(2 * node, l, mid, pos, val); } else{ update(2 * node + 1, mid + 1, r, pos, val); } tree[node] = max(tree[2 * node], tree[2 * node + 1]); } int ask(int node, int l, int r, int ql, int qr){ if(qr < l || r < ql) return 0; if(ql <= l && r <= qr) return tree[node]; int mid = (l + r) / 2; return max(ask(2 * node, l, mid, ql, qr), ask(2 * node + 1, mid + 1, r, ql, qr)); } void INPUT(){ cin >> n >> q; vector<array<int, 3>> v; for(int i = 1; i <= n; i++){ int a, b; cin >> a >> b; v.push_back({a, i, 0}); v.push_back({b, -i, 0}); } for(int i = 1; i <= q; i++){ int a, b; cin >> a >> b; v.push_back({a, i, 1}); v.push_back({b, -i, 1}); } sort(v.begin(), v.end()); int t = 0; for(int i = 0; i < 2 * n + 2 * q; i++){ if(i == 0 || v[i][0] != v[i - 1][0]){ t++; } if(v[i][2]){ if(v[i][1] > 0){ Q[v[i][1]].first = t; } else{ Q[-v[i][1]].second = t; } } else{ if(v[i][1] > 0){ arr[v[i][1]].first = t; } else{ arr[-v[i][1]].second = t; } } } } bool cmp(DATA a, DATA b){ if(a.h == b.h){ if(a.id == b.id){ return a.r > b.r; } else{ return a.id < b.id; } } return a.h < b.h; } int ans[MAX]; int main(){ INPUT(); for(int i = 1; i <= n; i++){ E.push_back({arr[i].second, 0, arr[i].first}); } for(int i = 1; i <= n; i++){ E.push_back({Q[i].second, i, Q[i].first}); } sort(E.begin(), E.end(), cmp); for(DATA e : E){ if(e.id){ ans[e.id] = ask(1, 1, AMAX, e.r, AMAX); } else{ update(1, 1, AMAX, e.r, ask(1, 1, AMAX, e.r, AMAX) + 1); } } for(int i = 1; i <= q; i++){ cout << ans[i] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...