bzoj3218 a + b Problem

http://www.elijahqi.win/archives/3293

考虑这题最小割怎么建图 源向每个点连 黑的贡献 每个点向汇连白的贡献然后每个点 新建一个点 向他连p的代价 这个每个p向一段区间连 这个区间连的话就直接线段树优化建图即可 但是因为他每次都只和前面的连 所以就被强行可持久化了 写个主席树即可 每次把a当作权值插到权值线段树里即可 mmp样例太水我主席树写错了都跑的出来

#include
#include
#include
#include
#include
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=175000;
const int inf=0x3f3f3f3f;
int num=1,h[N],cur[N],cnt,T,rt,level[N],n,a[20000],nn;
struct node{
    int y,next,z;
}data[2000000];
struct node1{
    int left,right;
}tree[N];
struct Q{
    int a,b,l,r,w,p;
}qr[5500];
inline void insert1(int x,int y,int z){
    data[++num].y=y;data[num].next=h[x];h[x]=num;data[num].z=z;
    data[++num].y=x;data[num].next=h[y];h[y]=num;data[num].z=0;
}
inline void modify(int x,int l,int r,int l1,int r1){
    if (!x) return;
    if (l1<=l&&r1>=r){insert1(cnt,x,inf);return;}int mid=l+r>>1;
    if (l1<=mid) modify(tree[x].left,l,mid,l1,r1);
    if (r1>mid) modify(tree[x].right,mid+1,r,l1,r1);    
}
inline void change(int &x,int l,int r,int p,int id){
    tree[++cnt]=tree[x];if (x) insert1(cnt,x,inf);x=cnt;
    if (l==r){insert1(x,id,inf);return;}int mid=l+r>>1;
    if(p<=mid) change(tree[x].left,l,mid,p,id),insert1(x,tree[x].left,inf);
    if(p>mid) change(tree[x].right,mid+1,r,p,id),insert1(x,tree[x].right,inf);
}
inline bool bfs(){
    queue<int>q;q.push(0);memset(level,0,sizeof(level));level[0]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z;
            if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==T) return 1;
        }
    }return 0;
}
inline int dfs(int x,int s){
    if (x==T) return s;int ss=s;
    for (int &i=cur[x];i;i=data[i].next){
        int y=data[i].y,z=data[i].z;
        if (level[x]+1==level[y]&&z){
            int xx=dfs(y,min(z,s));if (!xx) level[y]=0;
            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;
        }
    }return ss-s;
}
int main(){
    //freopen("bzoj3218.in","r",stdin);
    n=read();T=n+1;cnt=n+1;int ans=0;
    for (int i=1;i<=n;++i){
        qr[i].a=read(),qr[i].b=read(),qr[i].w=read(),qr[i].l=read(),qr[i].r=read(),qr[i].p=read();
        insert1(0,i,qr[i].b);insert1(i,T,qr[i].w);ans+=qr[i].w;ans+=qr[i].b;
        a[++nn]=qr[i].l;a[++nn]=qr[i].r;a[++nn]=qr[i].a;
    }sort(a+1,a+nn+1);nn=unique(a+1,a+nn+1)-a-1;
    for (int i=1;i<=n;++i){
        ++cnt;insert1(i,cnt,qr[i].p);
        int l=lower_bound(a+1,a+nn+1,qr[i].l)-a,r=lower_bound(a+1,a+nn+1,qr[i].r)-a;
        int aa=lower_bound(a+1,a+nn+1,qr[i].a)-a;
        modify(rt,1,nn,l,r);change(rt,1,nn,aa,i);
    }
    while(bfs()) memcpy(cur,h,sizeof(h)),ans-=dfs(0,inf);
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(线段树,网络流)