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 ...
随机推荐
- C++ Primer 学习笔记_Chapter4 数组和指针–指针
一.什么是指针? 指针与迭代器一样,指针提供对其所指对象的间接访问,指针保存的是另一个对象的地址: string s("hello"); string *ps = &s; ...
- MySQL的隐式类型转换整理总结
当我们对不同类型的值进行比较的时候,为了使得这些数值「可比较」(也可以称为类型的兼容性),MySQL会做一些隐式转化(Implicit type conversion). 比如下面的例子: 1 2 ...
- 裸机——I2C 2
前面的随笔完成了I2C时序分析(不涉及仲裁) 现在可以学使用控制器的I2C了. 1.先回顾I2C的基础知识 (1)总线包括SCL + SDA. (2)通信的特点: 同步,串行,电平 所以决定了 I2C ...
- 裸机——SD卡
1.首先要对SD卡有个基础知识 (1) SD = nandflash + 主控IC. 主控IC负责了校验和坏块管理,所以SoC只需要依照时序就可以和SD卡上的主控IC进行数据交换等操作. (2) SD ...
- 11-Json文件配置
1-新建json文件, 设置json文件生成的方式 { "ClassNo": "1", "ClassDesc": "Asp.net ...
- c语言printf()输出格式大全(转载)
1.转换说明符 %a(%A) 浮点数.十六进制数字和p-(P-)记数法(C99) %c 字符 %d 有符号十进制整 ...
- JS 金钱格式化
JavaScript Money Format(用prototype对Number进行扩展) Number.prototype.formatMoney = function (places, symb ...
- Pycharm的使用一
一.编辑器的选择 Python 的学习过程少不了集成开发环境(IDE)或者代码编辑器,这些 Python 开发工具帮助开发者加快使用 Python 开发的速度,提高效率. 高效的代码编辑器或者 IDE ...
- Kafka实践、升级和新版本(0.10)特性预研
本文来自于网易云社区 一.消息总线MQ和Kafka (挡在请求的第一线) 1. 几个应用场景 case a:上游系统往下游系统推送消息,而不关心处理结果: case b:一份新数据生成,需要实时保存到 ...
- Python学习-django-ModelForm组件
ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 labels=None, # 提示信 ...