이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 = 5e5+10;
int n;
pii range[maxn];
int ind[maxn];
int opt[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];
vector<int> V;
void solve(int l, int r)
{
if (l > r) return;
int mid = (l+r)>>1;
int k = V[mid];
dp[mid] = 1ll*seg.get(0, k, k) - 1ll*fq[k]*k;
opt[mid] = mid;
for (int i = 0; i < mid; i++)
{
ll x = dp[i] + 1ll*seg.get(V[i], k, k) - 1ll*fq[k]*k;
if (x < dp[mid])
dp[mid] = x, opt[mid] = i;
}
solve(l, mid-1); solve(mid+1, r);
}
int can(int M, int K[])
{
int m = M;
V.clear();
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;
opt[i] = i;
pii pp = {dp[i], i};
for (int j = (i == 0 ? 0 : opt[i-1]); j < i; j++)
pp = min(pp, {dp[j] + seg.get(V[j], k, k) - 1ll*fq[k]*k, j});
dp[i] = pp.ff;
opt[i] = pp.ss;
}
bool ok = 1;
for (int j = 0; j < m; j++)
{
if (dp[j] < 0) ok = 0;
dp[j] = fq[V[j]] = opt[j] = 0;
}
return ok;
}
컴파일 시 표준 에러 (stderr) 메시지
teams.cpp: In function 'int can(int, int*)':
teams.cpp:156:12: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
156 | m = V.size();
| ~~~~~~^~
# | 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... |