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"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 5e5+5, oo = 1e9;
#define sz(x) size(x)
struct FT {
vector<ll> s;
FT(int n) : s(n) {}
void update(int pos, ll dif) { // a[pos] += dif
for (; pos < sz(s); pos |= pos + 1) s[pos] += dif;
}
ll query(int pos) { // sum of values in [0, pos)
ll res = 0;
for (; pos > 0; pos &= pos - 1) res += s[pos-1];
return res;
}
int lower_bound(ll sum) {// min pos st sum of [0, pos] >= sum
// Returns n if no sum is >= sum, or -1 if empty sum is.
if (sum <= 0) return -1;
int pos = 0;
for (int pw = 1 << 25; pw; pw >>= 1) {
if (pos + pw <= sz(s) && s[pos + pw-1] < sum)
pos += pw, sum -= s[pos-1];
}
return pos;
}
};
struct FT2 {
vector<vi> ys; vector<FT> ft;
FT2(int limx) : ys(limx) {}
void fakeUpdate(int x, int y) {
for (; x < sz(ys); x |= x + 1) ys[x].push_back(y);
}
void init() {
for (vi& v : ys) sort(all(v)), ft.emplace_back(sz(v));
}
int ind(int x, int y) {
return (int)(lower_bound(all(ys[x]), y) - ys[x].begin()); }
void update(int x, int y, ll dif) {
for (; x < sz(ys); x |= x + 1)
ft[x].update(ind(x, y), dif);
}
ll query(int x, int y) {
ll sum = 0;
for (; x; x &= x - 1)
sum += ft[x-1].query(ind(x-1, y));
return sum;
}
ll query(int l, int r, int a, int b) {
return query(r,b)-query(r,a)-query(l,b)+query(l,a);
}
};
#include "teams.h"
int pref[mxN] = {}, pref2[mxN];
FT2 fen(mxN);
void init(int N, int A[], int B[]) {
for(int i=0;i<N;++i) {
pref[A[i]]++;
pref2[B[i]+1]++;
fen.fakeUpdate(A[i],B[i]);
}
fen.init();
for(int i=0;i<N;++i) {
fen.update(A[i],B[i],1);
}
partial_sum(pref,pref+mxN,pref);
partial_sum(pref2,pref2+mxN,pref2);
}
int can(int M, int K[]) {
sort(K,K+M);
int from=0, opens=0, closes=0;
for(int i=0;i<M;++i) {
int at = K[i];
opens+=pref[at]-pref[from];
int tmp = min(closes,pref2[at]-pref2[from]-(int)fen.query(from+1,mxN-1,0,at-1));
// not all closing brackets are from before, need to have extra condition.
// count number of intervals inside a region...
// Some 2D data structure solves this in log^2(n)
closes-=tmp;
opens-=pref2[at]-pref2[from]-tmp;
if(opens<at) return false;
opens-=at;
closes+=at;
from = at;
}
return true;
}
Compilation message (stderr)
teams.cpp: In member function 'void FT::update(int, ll)':
teams.cpp:17:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (; pos < sz(s); pos |= pos + 1) s[pos] += dif;
| ^
teams.cpp: In member function 'int FT::lower_bound(ll)':
teams.cpp:29:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if (pos + pw <= sz(s) && s[pos + pw-1] < sum)
| ^
teams.cpp: In member function 'void FT2::fakeUpdate(int, int)':
teams.cpp:39:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (; x < sz(ys); x |= x + 1) ys[x].push_back(y);
| ^
teams.cpp: In member function 'void FT2::update(int, int, ll)':
teams.cpp:47:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (; x < sz(ys); x |= x + 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... |