bzoj3638

费用流+线段树

看见这个题我们马上就能想到费用流,设立源汇,分别向每个点连接容量为1费用为0的边,然后相邻的点之间连边,费用为点权,跑费用流就行了,但是很明显这样会超时,那么我们要优化一下,我们观察费用流的过程,发现对于点与点之间的边,每次取一段区间相当于把正向边改为反向边,费用变负,于是我们可以用线段树来模拟这个过程,像费用流一样贪心地选取区间的最大子段和,然后取反,每次取k次,然后恢复。这样就好了

但是写的时候有很多问题,比如如何返回一个区间?结构体!参考了popoqqq大神的代码,发现我们可以通过重载小于号直接对结构体取max,这样就十分好写了

然后这道题有点卡常,一定要在重载的时候把传入参数变成const+引用,这样在cf上快了200ms

这就是传说中的五倍经验吗

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
namespace IO
{
    const int Maxlen = N * 50;
    char buf[Maxlen], *C = buf;
    int Len;
    inline void read_in()
    {
        Len = fread(C, 1, Maxlen, stdin);
        buf[Len] = ‘\0‘;
    }
    inline void fread(int &x)
    {
        x = 0;
        int f = 1;
        while (*C < ‘0‘ || ‘9‘ < *C) { if(*C == ‘-‘) f = -1; ++C; }
        while (‘0‘ <= *C && *C <= ‘9‘) x = (x << 1) + (x << 3) + *C - ‘0‘, ++C;
        x *= f;
    }
    inline void read(int &x)
    {
        x = 0;
        int f = 1; char c = getchar();
        while(c < ‘0‘ || c > ‘9‘) { if(c == ‘-‘) f = -1; c = getchar(); }
        while(c >= ‘0‘ && c <= ‘9‘) { x = (x << 1) + (x << 3) + c - ‘0‘; c = getchar(); }
        x *= f;
    }
    inline void read(long long &x)
    {
        x = 0;
        long long f = 1; char c = getchar();
        while(c < ‘0‘ || c > ‘9‘) { if(c == ‘-‘) f = -1; c = getchar(); }
        while(c >= ‘0‘ && c <= ‘9‘) { x = (x << 1ll) + (x << 3ll) + c - ‘0‘; c = getchar(); }
        x *= f;
    }
} using namespace IO;
struct data {
    int l, r, v;
    data() {}
    data(int l, int r, int v) : l(l), r(r), v(v) {}
    friend bool operator < (const data &a, const data &b) { return a.v < b.v; }
    friend data operator + (const data &a, const data &b) { return data(a.l, b.r, a.v + b.v); }
};
struct node {
    data lmax, rmax, mx, mn, lmin, rmin, sum;
    int tag;
    node() {}
    node(int x, int v) {
        lmax = rmax = mx = mn = lmin = rmin = sum = data(x, x, v);
    }
    friend node operator + (const node &a, const node &b) {
        node c;
        if(a.tag == -1) return b;
        if(b.tag == -1) return a;
        c.tag = 0;
        c.sum = a.sum + b.sum;
        c.lmax = max(a.lmax, a.sum + b.lmax);
        c.lmin = min(a.lmin, a.sum + b.lmin);
        c.rmax = max(b.rmax, a.rmax + b.sum);
        c.rmin = min(b.rmin, a.rmin + b.sum);
        c.mx = max(max(a.mx, b.mx), a.rmax + b.lmax);
        c.mn = min(min(a.mn, b.mn), a.rmin + b.lmin);
        return c;
    }
} tree[N << 2], st[21];
int n, q;
int a[N];
void paint(node &o)
{
    swap(o.lmax, o.lmin);
    swap(o.rmax, o.rmin);
    swap(o.mx, o.mn);
    o.sum.v *= -1;
    o.lmax.v *= -1;
    o.lmin.v *= -1;
    o.rmax.v *= -1;
    o.rmin.v *= -1;
    o.mx.v *= -1;
    o.mn.v *= -1;
    o.tag ^= 1;
}
void pushdown(int x)
{
    if(tree[x].tag <= 0) return;
    paint(tree[x << 1]);
    paint(tree[x << 1 | 1]);
    tree[x].tag ^= 1;
}
void build(int l, int r, int x)
{
    if(l == r)
    {
        tree[x] = node(l, a[l]);
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, x << 1);
    build(mid + 1, r, x << 1 | 1);
    tree[x] = tree[x << 1] + tree[x << 1 | 1];
}
node query(int l, int r, int x, int a, int b)
{
    if(l > b || r < a) return tree[0];
    if(l >= a && r <= b) return tree[x];
    pushdown(x);
    int mid = (l + r) >> 1;
    return (query(l, mid, x << 1, a, b)) + (query(mid + 1, r, x << 1 | 1, a, b));
}
void reverse(int l, int r, int x, int a, int b)
{
    if(l > b || r < a) return;
    if(l >= a && r <= b)
    {
        paint(tree[x]);
        return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    reverse(l, mid, x << 1, a, b);
    reverse(mid + 1, r, x << 1 | 1, a, b);
    tree[x] = tree[x << 1] + tree[x << 1 | 1];
}
void update(int l, int r, int x, int pos, int v)
{
    if(l == r)
    {
        tree[x] = node(l, v);
        return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if(pos <= mid) update(l, mid, x << 1, pos, v);
    else update(mid + 1, r, x << 1 | 1, pos, v);
    tree[x] = tree[x << 1] + tree[x << 1 | 1];
}
int main()
{
    read_in();
    fread(n);
    for(int i = 1; i <= n; ++i) fread(a[i]);
    tree[0].tag = -1;
    build(1, n, 1);
    fread(q);
    while(q--)
    {
        int opt, l, r, v;
        fread(opt);
        if(opt == 0)
        {
            fread(l);
            fread(v);
            update(1, n, 1, l, v);
        }
        if(opt == 1)
        {
            fread(l);
            fread(r);
            fread(v);
            int sum = 0, top = 0;
            while(v--)
            {
                node ans = query(1, n, 1, l, r);
                if(ans.mx.v <= 0) break;
                reverse(1, n, 1, ans.mx.l, ans.mx.r);
                node tmp = query(1, n, 1, ans.mx.l, ans.mx.r);
                st[++top] = ans;
                sum += ans.mx.v;
            }
            printf("%d\n", sum);
            for(int i = top; i; --i) reverse(1, n, 1, st[i].mx.l, st[i].mx.r);
        }
    }
    return 0;
} 

时间: 2024-11-06 18:25:55

bzoj3638的相关文章

【BZOJ3638】Cf172 k-Maximum Subsequence Sum 线段树区间合并(模拟费用流)

[BZOJ3638]Cf172 k-Maximum Subsequence Sum Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少.1 ≤ n ≤ 105,1 ≤ m ≤ 105,1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 20 Sample Input 9 9 -8 9 -1 -1 -1 9 -8 9 3 1 1 9 1 1 1 9 2 1 4 6 3 Sample Output 17 25

【BZOJ-3638&amp;3272&amp;3267&amp;3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广

3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 174  Solved: 92[Submit][Status][Discuss] Description 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. Input The first line contains integer n (1 ≤ n 

BZOJ3638缓存

#include<iostream> #include<cmath> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; inline int read() { int x=0,f=1; char ch=getchar(); while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar(

bzoj3638 Cf172 k-Maximum Subsequence Sum

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3638 [题解] 看到k<=20就感觉很py了啊 我们用一棵线段树维护选段的过程,能选到>0的段就一直选,直到选到<0的段,每次选完把段内的数全部取相反数,意为下次取是"不取"的意思. 用线段树维护左边/右边/中间的max/min # include <stdio.h> # include <string.h> # include <

【bzoj3638】Cf172 k-Maximum Subsequence Sum 模拟费用流+线段树区间合并

题目描述 给一列数,要求支持操作: 1.修改某个数的值 2.读入l,r,k,询问在[l,r]内选不相交的不超过k个子段,最大的和是多少. 输入 The first line contains integer n (1 ≤ n ≤ 105), showing how many numbers the sequence has. The next line contains n integers a1, a2, ..., an (|ai| ≤ 500). The third line contain

[转载]hzwer的bzoj题单

counter: 664BZOJ1601 BZOJ1003 BZOJ1002 BZOJ1192 BZOJ1303 BZOJ1270 BZOJ3039 BZOJ1191 BZOJ1059 BZOJ1202 BZOJ1051 BZOJ1001 BZOJ1588 BZOJ1208 BZOJ1491 BZOJ1084 BZOJ1295 BZOJ3109 BZOJ1085 BZOJ1041 BZOJ1087 BZOJ3038 BZOJ1821 BZOJ1076 BZOJ2321 BZOJ1934 BZOJ

瞎题表

数据结构小练习bzoj3221:[Codechef FEB13] Obserbing the tree树上询问 树剖+主席树(区间修改,加等差数列)bzoj2735:世博会 主席树+切比雪夫距离转曼哈顿距离+我最弱的数学推理bzoj3217:ALOEXT 替罪羊套01Trie(我的码力还是弱得不行,傻逼错误一大堆……)精细的实现果然很重要(对于数据结构里的点(比如……),可以直接提取其维护的区间然后操作的啊)替罪羊的插入重建好像使得重建点的祖先的cover不正确了?然而并没有什么影响?(zyf提