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 <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的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
随机推荐
- iOS-UICollectionViewController 介绍
废话不多说,列几个列子 (几种情况下的做法): 情景一: 介绍:1. 在UIViewController 上加 UICollectionView (用代码 创建 UICollectionView). ...
- P2580 于是他错误的点名开始了(trie)
P2580 于是他错误的点名开始了 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉 ...
- cycling -avoid the vicious cycle
‘Numerous' studies in the past appear to have shown a link between cycling and ED. The researchers a ...
- how to export chrome speed dial extension?
locate chrome-extension_dgpdioedihjhncjafcpgbbjdpbbkikmi_0.localstorage, copy it to you want, everyt ...
- USACO Section1.4 Mother's Milk 解题报告
milk3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- 18、bootStap JavaScript插件
1.模态框 <!--模态框经过了优化,更加灵活,以弹出对话框的形式出现,具有最小和最实用的功能集.--> <button type="button" class= ...
- 每天一个Linux命令(9):cp命令
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...
- python-成员修饰符
python的面相对象中,拥有3个成员,字段.方法.属性 class Foo: def __init__(self,name): #公有字段name在类中与类外均能调用 self.name = nam ...
- hnust 档案管理
问题 E: 档案管理 时间限制: 1 Sec 内存限制: 128 MB提交: 274 解决: 105[提交][状态][讨论版] 题目描述 X老师管理着学校的档案室,经常会有其他的老师来档案室存文件 ...
- RelativeLayout布局属性
Android RelativeLayout属性 // 相对于给定ID控件 android:layout_above 将该控件的底部置于给定ID的控件之上; android:layout_below ...