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>
#include "teams.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
int n;
vector<int> a, b;
vector<pii> st;
void init(int N, int A[], int B[]){
n = N;
a.resize(n + 1);
for (int i = 1; i <= n; i++) a[i] = A[i - 1];
b.resize(n + 1);
for (int i = 1; i <= n; i++) b[i] = B[i - 1];
for (int i = 1; i <= n; i++) st.pb({a[i], b[i]});
sort(st.begin(), st.end());
}
int pr(int x, int y){
int out = 0;
for (auto [u, v]: st){
out += (u <= x && v <= y);
}
return out;
}
int sum(int x1, int y1, int x2, int y2){
return pr(x2, y2) + pr(x1, y1) - pr(x1 - 1, y2) - pr(x2, y1 - 1);
}
int can(int m, int K[]){
vector<int> k(m + 1);
for (int i = 1; i <= m; i++) k[i] = K[i - 1];
sort(k.begin() + 1, k.end());
vector<pii> st;
vector<int> pr = {0};
auto get = [&](int x, int y){
if (st.empty()) return sum(0, x, x, y);
int l = 0, r = (int) st.size() - 1;
while (l + 1 < r){
int m = (l + r) / 2;
if (st[m].ss >= y){
l = m;
}
else {
r = m - 1;
}
}
if (st[r].ss >= y) l = r;
if (st[l].ss < y) l = -1;
int out = sum(0, x, x, y);
for (int i = 0; i + 1 < st.size(); i++){
out -= sum(st[i].ff + 1, x, st[i + 1].ff, min(st[i + 1].ss, y));
}
out -= sum(0, x, st[0].ff, min(st[0].ss, y));
// if (l != -1) out -= sum(0, x, st[l].ff, y);
// if (l + 1 < pr.size()) out -= (pr.back() - pr[l + 1] - sum((l != -1) ? (st[l].ff + 1) : 0, 0, x, x - 1));
return out;
};
auto add = [&](int x, int y){
while (!st.empty() && st.back().ss < y){
st.pop_back();
pr.pop_back();
}
if (st.empty()) pr.pb(sum(0, 0, x, y));
else pr.pb(pr.back() + sum(st.back().ff + 1, 0, x, y));
st.pb({x, y});
};
for (int i = 1; i <= m; i++){
while (!st.empty() && st.back().ss < k[i]){
st.pop_back();
pr.pop_back();
}
if (get(k[i], n) < k[i]) return 0;
int l = k[i], r = n;
while (l + 1 < r){
int m = (l + r) / 2;
if (get(k[i], m) < k[i]){
l = m + 1;
}
else {
r = m;
}
}
if (get(k[i], l) >= k[i]) r = l;
add(k[i], r);
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'int can(int, int*)':
teams.cpp:43:17: warning: declaration of 'st' shadows a global declaration [-Wshadow]
43 | vector<pii> st;
| ^~
teams.cpp:13:13: note: shadowed declaration is here
13 | vector<pii> st;
| ^~
teams.cpp: In lambda function:
teams.cpp:50:17: warning: declaration of 'int m' shadows a parameter [-Wshadow]
50 | int m = (l + r) / 2;
| ^
teams.cpp:38:13: note: shadowed declaration is here
38 | int can(int m, int K[]){
| ~~~~^
teams.cpp:62:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i + 1 < st.size(); i++){
| ~~~~~~^~~~~~~~~~~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:94:17: warning: declaration of 'int m' shadows a parameter [-Wshadow]
94 | int m = (l + r) / 2;
| ^
teams.cpp:38:13: note: shadowed declaration is here
38 | int can(int m, int K[]){
| ~~~~^
# | 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... |