This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define REB(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 5e5 + 7;
const int Mod = 1e9 + 7;///lon
const ll INF = 1e18 + 7;
const int BASE = 137;
const int szBL = 350;
int n, Q, m;
pii seg[N], seg2[N];
vector<int> K;
struct Node {
Node *lc, *rc;
int val;
Node (int _val = 0) {
lc = rc = NULL;
val = _val;
}
Node (Node *L, Node *R) {
lc = L;
rc = R;
val = 0;
if (lc) val += lc->val;
if (rc) val += rc->val;
}
}*Ver[N];
struct Persistent_Segment_Tree {
int m;
int num_Ver;
Node *root[N];
Node *build (int l, int r) {
if (l == r) {
return new Node();
}
int mid = l + r >> 1;
return new Node(build (l, mid), build (mid + 1, r));
}
Node *init (int n) {
m = n;
return root[0] = build (1, m);
}
Node *update (Node *cur, int l, int r, int pos, int val) {
if (l == r) {
return new Node (cur->val + val);
}
int mid = l + r >> 1;
if (pos <= mid) return new Node (update (cur->lc, l, mid, pos, val), cur->rc);
else return new Node (cur->lc, update (cur->rc, mid + 1, r, pos, val));
}
int get (Node *cur, int l, int r, int u, int v) {
if (l > v || r < u) return 0;
if (l >= u && r <= v) return cur->val;
int mid = l + r >> 1;
return get (cur->lc, l, mid, u, v) + get (cur->rc, mid + 1, r, u, v);
}
Node *update (int pos, int val) {
++num_Ver;
return root[num_Ver] = update (root[num_Ver - 1], 1, m, pos, val);
}
int get (int req, int u, int v) { ///get các version mà chỉ có các con Y <= req
return get(Ver[req], 1, m, u, v);
}
}ST;
void init (int _n, int _A[], int _B[]) {
n = _n;
rep (i, 1, n) seg[i].fs = _A[i - 1], seg[i].se = _B[i - 1];
Ver[0] = ST.init(n);
sort (seg + 1, seg + 1 + n, [] (pii A, pii B) { return A.se < B.se; });
rep (i, 1, n) seg2[i] = seg[i];
sort (seg2 + 1, seg2 + 1 + n, [] (pii A, pii B) { return A.fs <B.fs; });
int ptr = 1;
rep (i, 1, n) {
while (ptr <= n && seg[ptr].se < i) ++ptr;
Ver[i] = Ver[i - 1];
while (ptr <= n && seg[ptr].se == i) {
Ver[i] = ST.update (seg[ptr].fs, 1);
++ptr;
}
}
}
bool process () {
sort (ALL(K));
vector<int> rem(m + 3, 0); {
rep (i, 0, m - 1) rem[i] = K[i];
}
K.pb(n + 1);
rep (i, 0, m - 1) {
int L = K[i], R = K[i + 1] - 1;
int cur_rem = 0;
rep (j, 0, i) {
int delta = ST.get(R, 1, K[j]) - ST.get(L - 1, 1, K[j]) - cur_rem;
cur_rem += min(rem[j], delta);
// cout << j <<" "<<i<< " "<<L<<" "<<R<<" "<<K[j]<<" "<<ST.get(R, 1, K[j]) <<"\n";
if (delta >= rem[j]) rem[j] = 0;
else rem[j] -= delta;
}
}
// rep (i, 0, m - 1) cout << rem[i] <<"\n";
rep (i, 0, m - 1) if (rem[i] != 0) return 0;
return 1;
}
bool process2 () {
sort (ALL(K));
priority_queue<int, vector<int>, greater<int>> pq;
int ptr = 1;
rep (i, 0, m - 1) {
while (ptr <= n && seg2[ptr].fs <= K[i]) {
if (seg2[ptr].se >= K[i]) pq.push(seg2[ptr].se);
++ptr;
}
int rem = K[i];
while (rem > 0 && !pq.empty()) {
int u = pq.top();
pq.pop();
if (u >= K[i]) --rem;
}
if (rem != 0) return 0;
}
return 1;
}
bool can (int _m, int _K[]) {
m = _m;
K.resize(m);
rep (i, 0, m - 1) K[i] = _K[i];
if (m <= szBL)
return process();
else
return process2 ();
}
//void processs () {
// cin >> n;
// rep (i, 1, n) cin >> seg[i].fs >> seg[i].se;
// Ver[0] = ST.init(n);
// sort (seg + 1, seg + 1 + n, [] (pii A, pii B) { return A.se < B.se; });
// rep (i, 1, n) seg2[i] = seg[i];
// sort (seg2 + 1, seg2 + 1 + n, [] (pii A, pii B) { return A.fs < B.fs; });
// int ptr = 1;
// rep (i, 1, n) {
// while (ptr <= n && seg[ptr].se < i) ++ptr;
// Ver[i] = Ver[i - 1];
// while (ptr <= n && seg[ptr].se == i) {
// Ver[i] = ST.update (seg[ptr].fs, 1);
// ++ptr;
// }
// }
// int Q;
// cin >> Q;
// rep (i, 1, Q) {
// int m;
// cin >> m;
// vector<int> cur(m);
// rep (i, 0, m - 1) cin >> cur[i];
// cout << can(m, cur) <<"\n";
// }
//}
//#define file(name) if(fopen(name".inp","r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
//main () {
//// file("c");
// ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int num_Test = 1;
//// cin >> num_Test;
// while (num_Test--) {
// processs();
// }
//}
/*
onepunch +25
chắt lọc thông tin để mô hình
cận trên dưới
*/
Compilation message (stderr)
teams.cpp: In member function 'Node* Persistent_Segment_Tree::build(int, int)':
teams.cpp:57:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int mid = l + r >> 1;
| ~~^~~
teams.cpp: In member function 'Node* Persistent_Segment_Tree::init(int)':
teams.cpp:60:21: warning: declaration of 'n' shadows a global declaration [-Wshadow]
60 | Node *init (int n) {
| ~~~~^
teams.cpp:27:5: note: shadowed declaration is here
27 | int n, Q, m;
| ^
teams.cpp: In member function 'Node* Persistent_Segment_Tree::update(Node*, int, int, int, int)':
teams.cpp:68:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
68 | int mid = l + r >> 1;
| ~~^~~
teams.cpp: In member function 'int Persistent_Segment_Tree::get(Node*, int, int, int, int)':
teams.cpp:75:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |