又是一道主席树优化网络流的好题
按约大爷的教导,源点为白,汇点为黑,搞成最小割
发现暴力连边要爆炸,但是要连的点在线段树中都构成了一个区间,果断主席树优化之
为什么不用一般线段树?
因为要满足 j<i ,这里的可持久化并不是为了查询过去的值,而是为了保留过去的值不与后来弄混~
如果有两个点的 a[i] 相同在线段树里怎么搞?
很简单,从 a[i] 向 a[j] 连一条 inf 的流即可
不过——为什么题目名字那么坑啊啊啊啊啊啊?!!!!!
这种题是不是非要来卡空间不然不痛快是吗?!!!!!
#include <cstdio>
#include <cstring>
const int sizeOfSegment=;
const int sizeOfEdge=;
const int sizeOfPoint=;
const int sizeOfCell=;
const int inf=; inline int min(int, int);
inline int getint();
inline void putint(int); int S, T;
int V;
int n;
int a[sizeOfCell], b[sizeOfCell], w[sizeOfCell], l[sizeOfCell], r[sizeOfCell], p[sizeOfCell]; struct edge {int point, flow; edge * next, * pair;};
edge memory_edge[sizeOfEdge], * port_edge=memory_edge;
edge * e[sizeOfPoint];
inline edge * newedge(int, int, edge * );
inline void link(int, int, int);
int h[sizeOfPoint], gap[sizeOfPoint];
inline bool bfs();
inline int isap(); struct seg {int p; seg * l, * r;};
seg memory_seg[sizeOfSegment], * port_seg=memory_seg;
seg * t;
inline seg * newseg(seg * =NULL);
seg * insert(seg * , int, int, int, int);
void query(seg * , int, int, int, int, int); int main()
{
int ans=; n=getint();
S=; T=n+; V=n+;
for (int i=;i<=n;i++)
{
a[i]=getint(), b[i]=getint(), w[i]=getint(), l[i]=getint(), r[i]=getint(), p[i]=getint();
ans+=b[i]+w[i];
link(S, i, b[i]); link(i, T, w[i]);
link(i, ++V, p[i]);
query(t, , inf, l[i], r[i], V);
t=insert(t, , inf, a[i], i);
} ans-=isap();
putint(ans); return ;
}
inline int min(int x, int y)
{
return x<y?x:y;
}
inline int getint()
{
register int num=;
register char ch;
do ch=getchar(); while (ch<'' || ch>'');
do num=num*+ch-'', ch=getchar(); while (ch>='' && ch<='');
return num;
}
inline void putint(int num)
{
char stack[];
register int top=;
if (num==) stack[top=]='';
for ( ;num;num/=) stack[++top]=num%+'';
for ( ;top;top--) putchar(stack[top]);
putchar('\n');
}
inline edge * newedge(int point, int flow, edge * next)
{
edge * ret=port_edge++;
ret->point=point; ret->flow=flow; ret->next=next;
return ret;
}
inline void link(int u, int v, int f)
{
e[u]=newedge(v, f, e[u]); e[v]=newedge(u, , e[v]);
e[u]->pair=e[v]; e[v]->pair=e[u];
}
inline bool bfs()
{
static int q[sizeOfPoint];
static int l, r;
memset(h, 0xFF, sizeof(h)); h[T]=;
l=r=;
for (q[r++]=T;l<r;l++)
{
int u=q[l];
++gap[h[u]];
for (edge * i=e[u];i;i=i->next) if (h[i->point]==-)
{
h[i->point]=h[u]+;
q[r++]=i->point;
}
}
return h[S]>-;
}
inline int isap()
{
static edge * t[sizeOfPoint], * p[sizeOfPoint];
static int aug[sizeOfPoint];
int flow=, hmin=; if (!bfs()) return ; memcpy(t, e, sizeof(e)); memset(p, , sizeof(p));
aug[S]=inf;
for (int u=S;h[S]<V; )
{
if (u==T)
{
flow+=aug[T];
for (edge * i=p[T];i;i=p[i->point])
aug[i->point]-=aug[T], i->pair->flow-=aug[T], i->flow+=aug[T];
for (edge * i=p[T];i;i=p[i->point]) if (aug[i->point])
{
u=i->point;
break;
}
} edge *& i=t[u];
for ( ;i && (!i->flow || h[i->point]+!=h[u]);i=i->next);
if (i)
{
aug[i->point]=min(aug[u], i->flow); p[i->point]=i->pair;
u=i->point;
}
else
{
if (!--gap[h[u]]) break;
hmin=V;
for (edge * j=e[u];j;j=j->next) if (j->flow && h[j->point]+<hmin)
{
hmin=h[j->point]+;
t[u]=j;
}
++gap[h[u]=hmin];
u=u==S?S:p[u]->point;
}
} return flow;
}
inline seg * newseg(seg * t)
{
seg * newt=port_seg++;
newt->p=++V; newt->l=t?t->l:NULL; newt->r=t?t->r:NULL;
return newt;
}
seg * insert(seg * t, int l, int r, int p, int v)
{
seg * newt=newseg(t);
if (l==r)
{
if (t) link(newt->p, t->p, inf);
link(newt->p, v, inf);
}
else
{
int m=(l+r)>>;
if (p<=m)
{
newt->l=insert(newt->l, l, m, p, v), link(newt->p, newt->l->p, inf);
if (newt->r) link(newt->p, newt->r->p, inf);
}
else
{
newt->r=insert(newt->r, m+, r, p, v), link(newt->p, newt->r->p, inf);
if (newt->l) link(newt->p, newt->l->p, inf);
}
} return newt;
}
void query(seg * t, int l, int r, int ql, int qr, int v)
{
if (!t) return ;
if (l==ql && r==qr) link(v, t->p, inf);
else
{
int m=(l+r)>>;
if (qr<=m) query(t->l, l, m, ql, qr, v);
else if (ql>=m+) query(t->r, m+, r, ql, qr, v);
else query(t->l, l, m, ql, m, v), query(t->r, m+, r, m+, qr, v);
}
}
改数组改到想去死
尝试着用了用 Data Display Debugger 觉得是很强大但是总觉得也很不顺手?
不过反正有了这个我是不想再去用 gdb 了……