# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
598304 | jeroenodb | 팀들 (IOI15_teams) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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+2, 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;
}