# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
384552 | luciocf | Teams (IOI15_teams) | C++11 | 0 ms | 0 KiB |
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"
#define ff first
#define ss second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 1e5+10;
int n;
pii range[maxn];
int ind[maxn];
struct Node
{
Node *l, *r;
int v;
Node()
{
l = r = NULL;
v = 0;
}
} *version[maxn];
struct PersistentSegmentTree
{
void build(Node *node, int l, int r)
{
if (l == r) return;
int mid = (l+r)>>1;
node->l = new Node(); node->r = new Node();
build(node->l, l, mid); build(node->r, mid+1, r);
}
void upd(Node *prev, Node *node, int l, int r, int pos, int v)
{
if (l == r)
{
node->v = prev->v + v;
return;
}
int mid = (l+r)>>1;
if (pos <= mid)
{
if (!node->l) node->l = new Node();
node->r = prev->r;
upd(prev->l, node->l, l, mid, pos, v);
}
else
{
if (!node->r) node->r = new Node();
node->l = prev->l;
upd(prev->r, node->r, mid+1, r, pos, v);
}
node->v = node->l->v + node->r->v;
}
int query(Node *prev, Node *node, int l, int r, int a, int b)
{
if (l > b || r < a) return 0;
if (l >= a && r <= b) return node->v - prev->v;
int mid = (l+r)>>1;
return query(prev->l, node->l, l, mid, a, b) + query(prev->r, node->r, mid+1, r, a, b);
}
int get(int l, int r, int k)
{
return query(version[ind[l]], version[ind[r]], 1, n, k, n);
}
} seg;
void init(int N, int A[], int B[])
{
n = N;
for (int i = 1; i <= n; i++)
range[i] = {A[i-1], B[i-1]};
sort(range+1, range+n+1);
int ptr = 1, aux = 0;
version[0] = new Node();
seg.build(version[0], 1, n);
for (int i = 1; i <= n; i++)
{
while (ptr <= n && range[ptr].ff == i)
{
version[++aux] = new Node();
seg.upd(version[aux-1], version[aux], 1, n, range[ptr].ss, 1);
ptr++;
}
ind[i] = aux;
}
}
ll dp[maxn];
int fq[maxn];
int can(int M, int K[])
{
int m = M;
vector<int> V;
for (int i = 0; i < m; i++)
{
V.push_back(K[i]);
fq[K[i]]++;
}
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
m = V.size();
for (int i = 0; i < m; i++)
{
int k = V[i];
dp[i] = 1ll*seg.get(0, k, k) - 1ll*fq[k]*k;
for (int j = 0; j < i; j++)
dp[i] = min(dp[i], dp[j] + seg.get(V[j], k, k) - 1ll*fq[k]*k);
if (dp[i] < 0)
{
for (int j = 0; j < m; j++)
dp[i] = fq[V[i]] = 0;
return 0;
}
}
for (int j = 0; j < m; j++)
dp[i] = fq[V[i]] = 0;
return 1;
}