传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3638

【题解】

看到k<=20就感觉很py了啊

我们用一棵线段树维护选段的过程,能选到>0的段就一直选,直到选到<0的段,每次选完把段内的数全部取相反数,意为下次取是“不取”的意思。

用线段树维护左边/右边/中间的max/min

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + ;
const int mod = 1e9+; # define RG register
# define ST static int n; struct pa {
int l, r, x;
pa() {}
pa(int l, int r, int x) : l(l), r(r), x(x) {}
friend pa operator + (pa a, pa b) {
pa c; c.x = a.x + b.x;
c.l = a.l, c.r = b.r;
return c;
}
friend bool operator < (pa a, pa b) {
return a.x < b.x;
}
friend bool operator > (pa a, pa b) {
return a.x > b.x;
}
}; struct querys {
pa lmx, rmx, mx, s;
querys() {}
querys(pa lmx, pa rmx, pa mx, pa s) : lmx(lmx), rmx(rmx), mx(mx), s(s) {}
}; namespace SMT {
const int Ms = 1e6 + ;
pa lmx[Ms], rmx[Ms], lmi[Ms], rmi[Ms], mx[Ms], mi[Ms], s[Ms];
bool tag[Ms]; // -1
# define ls (x<<)
# define rs (x<<|)
inline void up(int x) {
if(!x) return ;
lmx[x] = max(lmx[ls], s[ls] + lmx[rs]);
lmi[x] = min(lmi[ls], s[ls] + lmi[rs]);
rmx[x] = max(rmx[rs], rmx[ls] + s[rs]);
rmi[x] = min(rmi[rs], rmi[ls] + s[rs]);
mx[x] = max(mx[ls], mx[rs]);
mx[x] = max(mx[x], rmx[ls] + lmx[rs]);
mi[x] = min(mi[ls], mi[rs]);
mi[x] = min(mi[x], rmi[ls] + lmi[rs]);
s[x] = s[ls] + s[rs];
}
inline void pushtag(int x) {
if(!x) return ;
lmx[x].x = -lmx[x].x;
rmx[x].x = -rmx[x].x;
lmi[x].x = -lmi[x].x;
rmi[x].x = -rmi[x].x;
mx[x].x = -mx[x].x;
mi[x].x = -mi[x].x;
s[x].x = -s[x].x;
swap(mx[x], mi[x]);
swap(lmx[x], lmi[x]);
swap(rmx[x], rmi[x]);
tag[x] ^= ;
}
inline void down(int x) {
if(!x) return ;
if(!tag[x]) return ;
pushtag(ls); pushtag(rs);
tag[x] = ;
}
inline void change(int x, int l, int r, int ps, int d) {
if(l == r) {
s[x].l = s[x].r = lmx[x].l = lmx[x].r = rmx[x].l = rmx[x].r = lmi[x].l = lmi[x].r = rmi[x].l = rmi[x].r = l;
mx[x].l = mx[x].r = mi[x].l = mi[x].r = l;
s[x].x = mx[x].x = mi[x].x = lmx[x].x = lmi[x].x = rmx[x].x = rmi[x].x = d;
tag[x] = ;
return ;
}
down(x);
int mid = l+r>>;
if(ps <= mid) change(ls, l, mid, ps, d);
else change(rs, mid+, r, ps, d);
up(x);
} inline void change2(int x, int l, int r, int L, int R) {
if(L <= l && r <= R) {
pushtag(x);
return ;
}
down(x);
int mid = l+r>>;
if(L <= mid) change2(ls, l, mid, L, R);
if(R > mid) change2(rs, mid+, r, L, R);
up(x);
} inline querys merge(querys a, querys b) {
querys c;
c.lmx = max(a.lmx, a.s+b.lmx);
c.rmx = max(b.rmx, a.rmx+b.s);
c.s = a.s + b.s;
c.mx = max(a.mx, b.mx);
c.mx = max(c.mx, a.rmx + b.lmx);
return c;
} inline querys query(int x, int l, int r, int L, int R) {
if(L <= l && r <= R) return querys(lmx[x], rmx[x], mx[x], s[x]);
down(x);
int mid = l+r>>;
if(R <= mid) return query(ls, l, mid, L, R);
else if(L > mid) return query(rs, mid+, r, L, R);
else return merge(query(ls, l, mid, L, mid), query(rs, mid+, r, mid+, R));
} inline void debug(int x, int l, int r) {
printf("x = %d, l = %d, r = %d : mx = %d, lmx = %d, rmx = %d\n", x, l, r, mx[x].x, mx[x].l, mx[x].r);
if(l == r) return ;
int mid = l+r>>;
debug(ls, l, mid);
debug(rs, mid+, r);
}
} int Left[], Right[], m; int main() {
cin >> n;
for (int i=, t; i<=n; ++i) {
scanf("%d", &t);
SMT::change(, , n, i, t);
}
int Q, a, b, c;
querys t;
cin >> Q;
while(Q--) {
int opt; scanf("%d", &opt);
if(!opt) {
scanf("%d%d", &a, &b);
SMT::change(, , n, a, b);
} else {
scanf("%d%d%d", &a, &b, &c);
int s = ; m = ;
while(c--) {
t = SMT::query(, , n, a, b);
if(t.mx.x < ) break;
else s += t.mx.x;
// cout << t.mx.l << " " << t.mx.r << " " << t.mx.x << endl;
SMT::change2(, , n, t.mx.l, t.mx.r);
++m; Left[m] = t.mx.l, Right[m] = t.mx.r;
}
printf("%d\n", s);
for (int i=m; i; --i) SMT::change2(, , n, Left[i], Right[i]);
}
// SMT::debug(1, 1, n);
}
return ;
}

bzoj3638 Cf172 k-Maximum Subsequence Sum的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  2. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  4. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  6. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  7. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  8. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  9. pat1007. Maximum Subsequence Sum (25)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  10. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

随机推荐

  1. PHP设计者---composer

    Composer 是 PHP5以上 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 "packages&q ...

  2. laravel通过make auth实现手机号登录

    首先按照Laravel的教程,安装认证系统. php artisan make:auth php artisan migrate laravel已经安装完成认证系统,默认注册和登录都是用邮箱. 如果想 ...

  3. thinkphp5控制器向+vue的data里传值

    传一维数组传值 $array=['id'=>40,"cat_name"=>"明星产品"]; $MenuCats_info=json_encode($ ...

  4. python——标准异常总结

    请参考此网站: Python 标准异常总结 https://fishc.com.cn/forum.php?mod=viewthread&tid=45814&extra=page%3D1 ...

  5. 笔记-git-协作开发

    笔记-git-协作开发 1.      git协作开发 git协作的典型做法是,创建一个git服务器,被多个人操作. 示意图如下: 一般来说协作分为如下几个步骤: 创建一个git裸服务器 (git i ...

  6. Android面试收集录14 Android进程间通信方式

    一.使用 Intent Activity,Service,Receiver 都支持在 Intent 中传递 Bundle 数据,而 Bundle 实现了 Parcelable 接口,可以在不同的进程间 ...

  7. AD15添加导入组件

  8. MAC中mongodb的连接遇到的问题及调试

    今天在MAC环境下连接mongodb,遇到了一些报错,最终调试全部搞定.在此特做记录! 首先,mongod启动失败 上面有一句话是 exception in initAndListen: 20 Att ...

  9. MyEclipse - 问题集

    1. !MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.ui.workbench". ...

  10. Jenkins拾遗--第五篇-git插件填坑

    Jenkins使用过程中,大部分Job的第一项就行从源码库里签出代码.由于git越来越流行,所以,稍微新一些的项目的源码管理都是基于git的.对应的,jenkins的git plugin几乎是大部分j ...