3218: a + b Problem
Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1144 Solved: 443
[ Submit][ Status][ Discuss]
Description
Input
Output
Sample Input
Sample Output
HINT
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 5E3 + 50;
const int maxm = 2E6 + 5E5;
const int INF = ~0U>>1;
const int T = 22;
struct E{
int to,cap,flow;
E(){}
E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];
int n,m,s,t,tot,cnt,Cnt,Cur = 1,ans,cur[maxn*T],L[maxn*T],l[maxn],p[maxn]
,Root[maxn],lc[maxn*T],rc[maxn*T],r[maxn],a[maxn],vis[maxn*T],A[maxn*T];
queue <int> Q;
vector <int> v[maxn*T];
void Add(int x,int y,int cap)
{
v[x].push_back(cnt);
edgs[cnt++] = E(y,cap,0);
v[y].push_back(cnt);
edgs[cnt++] = E(x,0,0);
}
int Insert(int o,int l,int r,int pos,int Num)
{
int ret = ++tot;
if (l == r) {
Add(ret,Num,INF);
if (o) Add(ret,o,INF);
return ret;
}
int mid = (l + r) >> 1;
if (pos <= mid) {
rc[ret] = rc[o];
lc[ret] = Insert(lc[o],l,mid,pos,Num);
Add(ret,lc[ret],INF);
if (rc[ret]) Add(ret,rc[ret],INF);
if (lc[o]) Add(ret,lc[o],INF);
}
else {
lc[ret] = lc[o];
rc[ret] = Insert(rc[o],mid+1,r,pos,Num);
Add(ret,rc[ret],INF);
if (lc[ret]) Add(ret,lc[ret],INF);
if (rc[o]) Add(ret,rc[o],INF);
}
return ret;
}
void Build(int o,int l,int r,int bl,int br,int Now)
{
if (!o) return;
if (bl <= l && r <= br) {
Add(Now,o,INF);
return;
}
int mid = (l + r) >> 1;
if (bl <= mid) Build(lc[o],l,mid,bl,br,Now);
if (br > mid) Build(rc[o],mid + 1,r,bl,br,Now);
}
bool BFS()
{
vis[s] = ++Cnt; L[s] = 1;
Q.push(s);
while (!Q.empty()) {
int k = Q.front(); Q.pop();
for (int i = 0; i < v[k].size(); i++) {
E e = edgs[v[k][i]];
if (e.cap == e.flow) continue;
if (vis[e.to] == Cnt) continue;
vis[e.to] = Cnt;
L[e.to] = L[k] + 1;
Q.push(e.to);
}
}
return vis[t] == Cnt;
}
int Dicnic(int x,int a)
{
if (x == t) return a;
int flow = 0;
for (int &i = cur[x]; i < v[x].size(); i++) {
E &e = edgs[v[x][i]];
if (e.cap == e.flow) continue;
if (L[e.to] != L[x] + 1) continue;
int f = Dicnic(e.to,min(a,e.cap - e.flow));
if (!f) continue;
e.flow += f;
edgs[v[x][i]^1].flow -= f;
a -= f;
flow += f;
if (!a) return flow;
}
if (!flow) L[x] = -1;
return flow;
}
int getint()
{
char ch = getchar();
int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint();
s = 0; t = n + 1;
for (int i = 1; i <= n; i++) {
int b,w;
a[i] = getint();
b = getint();
w = getint();
l[i] = getint();
r[i] = getint();
p[i] = getint();
//scanf("%d%d%d%d%d%d",&a[i],&b,&w,&l[i],&r[i],&p[i]);
A[++tot] = a[i];
A[++tot] = l[i];
A[++tot] = r[i];
Add(s,i,b);
Add(i,t,w);
ans += b;
ans += w;
}
sort(A + 1,A + tot + 1);
for (int i = 2; i <= tot; i++)
if (A[i] != A[i-1])
A[++Cur] = A[i];
tot = n + 1;
for (int i = 1; i <= n; i++) {
int pos = lower_bound(A + 1,A + Cur + 1,a[i]) - A;
Root[i] = Insert(Root[i-1],1,Cur,pos,i);
int New = ++tot;
Add(i,New,p[i]);
int posl = lower_bound(A + 1,A + Cur + 1,l[i]) - A;
int posr = lower_bound(A + 1,A + Cur + 1,r[i]) - A;
Build(Root[i-1],1,Cur,posl,posr,New);
}
int MaxFlow = 0;
while (BFS()) {
for (int i = 0; i <= tot; i++) cur[i] = 0;
MaxFlow += Dicnic(s,INF);
}
cout << ans - MaxFlow;
return 0;
}