BZOJ3218:a + b problem 主席树维护网络流建图

版权声明:xgc原创文章,未经允许不得转载。 https://blog.csdn.net/xgc_woker/article/details/82254958

Description
有n个方格,每个方格有6个属性:ai,bi,wi,li,ri,pi。
当方格涂白色时获得wi的值,方格涂黑色获得bi的值。
当方格涂黑色是若有方格满足:
1 < j < i,li<=a[j]<=ri,且方格j为白色时。
则方格i的权值需减去pi。
求一种染色方案使结果最大。


Sample Input
10
0 1 7 3 9 2
7 4 0 9 10 5
1 0 4 2 10 2
7 9 1 5 7 2
6 3 5 3 6 2
6 6 4 1 8 1
6 1 6 0 6 5
2 2 5 0 9 3
5 1 3 0 2 5
5 6 7 1 1 2


Sample Output
55


好题啊!
首先可以想到用总权值减去不合法方案值,那你就要做一个最小割。
先拆一下点,建出如下的图:
ins(st,i,w[i]),ins(i,ed,b[i]),ins(i’,i,p[i])
对于所有满足i的条件的点j:ins(j,i’,inf)
假设我们割掉了i的白边,若任意一个j的黑边被割掉了,那就会形成有可能一条路径使i权值累加pi。
若割掉了i的黑边,则不会累加pi的值。
考虑完建图之后,我们发现规模达到了n^2……
于是就要用线段树辅助建图,对于所有的i我们将他指向他所要求的区间,
但由于要满足j < i,所以你就要可持久化。。。


#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int _min(int x, int y) {return x < y ? x : y;}
int read() {
    int s = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * f;
}

struct node {
    int x, id;
} d[16000];
struct tnode {
    int lc, rc;
} t[310000]; int cnt, rt[5100];
struct edge {
    int x, y, c, next;
} e[810000]; int len, last[210000];
int L[5100], R[5100], a[5100];
int st, ed, h[210000];
queue<int> q;

bool cmp(node a, node b) {return a.x < b.x;}

void ins(int x, int y, int c) {
    e[++len].x = x; e[len].y = y; e[len].c = c;
    e[len].next = last[x]; last[x] = len;
    e[++len].x = y; e[len].y = x; e[len].c = 0;
    e[len].next = last[y]; last[y] = len;
}

bool bfs() {
    memset(h, 0, sizeof(h)); h[st] = 1; q.push(st);
    while(!q.empty()) {
        int x = q.front(); q.pop();
        for(int k = last[x]; k; k = e[k].next) {
            int y = e[k].y;
            if(!h[y] && e[k].c) h[y] = h[x] + 1, q.push(y);
        }
    } if(!h[ed]) return 0;
    return 1;
}

int dfs(int x, int flow) {
    if(x == ed) return flow;
    int tt = 0, minf;
    for(int k = last[x]; k; k = e[k].next) {
        int y = e[k].y;
        if(h[y] == h[x] + 1 && e[k].c && tt < flow) {
            minf = dfs(y, _min(e[k].c, flow - tt));
            e[k].c -= minf; e[k ^ 1].c += minf;
            tt += minf;
        }
    } if(!tt) h[x] = 0;
    return tt;
}

void Link(int &u, int l, int r, int p, int x) {
    if(!u) u = ++cnt;
    ins(x, u + ed, 999999999);
    if(l == r) return ;
    int mid = (l + r) / 2;
    if(p <= mid) Link(t[u].lc, l, mid, p, x);
    else Link(t[u].rc, mid + 1, r, p, x);
}

void Merge(int &u1, int u2) {
    if(!u1 || !u2) {u1 = u1 + u2; return ;}
    ins(u2 + ed, u1 + ed, 999999999);
    Merge(t[u1].lc, t[u2].lc);
    Merge(t[u1].rc, t[u2].rc);
}

void Ins(int u, int l, int r, int ll, int rr, int x) {
    if(!u) return ;
    if(l == ll && r == rr) {ins(u + ed, x, 999999999); return ;}
    int mid = (l + r) / 2;
    if(rr <= mid) Ins(t[u].lc, l, mid, ll, rr, x);
    else if(ll > mid) Ins(t[u].rc, mid + 1, r, ll, rr, x);
    else Ins(t[u].lc, l, mid, ll, mid, x), Ins(t[u].rc, mid + 1, r, mid + 1, rr, x);
}

int main() {
    int n = read();
    st = 0, ed = n * 2 + 1; len = 1;
    int hh = 0;
    for(int i = 1; i <= n; i++) {
        a[i] = read(); int b = read(), w = read();
        L[i] = read(), R[i] = read(); int p = read();
        hh += b + w;
        ins(st, i, w), ins(i, ed, b);
        ins(i + n, i, p);
        d[i * 3 - 2].id = i * 3 - 3;
        d[i * 3 - 1].id = i * 3 - 2;
        d[i * 3].id = i * 3 - 1;
        d[i * 3 - 2].x = L[i];
        d[i * 3 - 1].x = R[i];
        d[i * 3].x = a[i];
    } int tp = 0;
    sort(d + 1, d + 3 * n + 1, cmp);
    for(int i = 1; i <= 3 * n; i++) {
        if(d[i].x != d[i - 1].x || i == 1) tp++;
        if(d[i].id % 3 == 0) L[d[i].id / 3 + 1] = tp;
        if(d[i].id % 3 == 1) R[d[i].id / 3 + 1] = tp;
        if(d[i].id % 3 == 2) a[d[i].id / 3 + 1] = tp;
    }
    for(int i = 1; i <= n; i++) {
        Link(rt[i], 1, tp, a[i], i);
        Merge(rt[i], rt[i - 1]);
        Ins(rt[i - 1], 1, tp, L[i], R[i], i + n);
    }
//  for(int i = 1; i <= n; i++) {
//      for(int j = 1; j < i; j++) if(a[j] >= L[i] && a[j] <= R[i]){
//          ins(j, i + n, 999999999);
//      }
//  }//n^2建图
    int ans = 0;
    while(bfs()) ans += dfs(st, 999999999);
    printf("%d\n", hh - ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xgc_woker/article/details/82254958
今日推荐