Codeforces 739C Alyona and towers 线段树
这个题写起来真的要人命。。。
我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了。
如果用差分的话会简单一些, 就变成了求前一段是负数,后一段是正数的最长段多长。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 3e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, m; LL lazy[N << ];
struct Node {
LL lv, rv;
int mx, rup, rdn, lup, ldn;
bool up, dn, mon;
void print() {
puts("");
printf("lv: %lld rv: %lld\n", lv, rv);
printf("mx: %d\n", mx);
printf("rup: %d rdn: %d lup: %d ldn: %d\n", rup, rdn, lup, ldn);
printf("up: %d dn: %d mon: %d\n", up, dn, mon);
puts("");
}
} a[N << ]; Node operator + (const Node& a, const Node& b) {
Node c;
c.lv = a.lv; c.rv = b.rv;
c.mx = max(a.mx, b.mx);
c.rup = b.rup;
c.rdn = b.rdn;
c.lup = a.lup;
c.ldn = a.ldn; if(a.rv < b.lv) c.mx = max(c.mx, a.rup + b.lup);
if(a.rv > b.lv) c.mx = max(c.mx, a.rdn + b.ldn);
if(a.rv != b.lv) c.mx = max(c.mx, a.rup + b.ldn); if(b.up && a.rv < b.lv) c.rup = max(c.rup, a.rup + b.rup); if(b.mon && a.rv < b.lv) c.rdn = max(c.rdn, a.rup + b.rdn);
if(b.dn && a.rv > b.lv) c.rdn = max(c.rdn, a.rdn + b.rdn); if(a.mon && a.rv > b.lv) c.lup = max(c.lup, a.lup + b.ldn);
if(a.up && a.rv < b.lv) c.lup = max(c.lup, a.lup + b.lup); if(a.dn && a.rv > b.lv) c.ldn = max(c.ldn, a.ldn + b.ldn); c.up = a.up && b.up && a.rv < b.lv;
c.dn = a.dn && b.dn && a.rv > b.lv;
c.mon = false;
if(c.up || c.dn) c.mon = true;
else {
if(a.up && b.dn && a.rv != b.lv) c.mon = true;
if(a.mon && b.dn && a.rv > b.lv) c.mon = true;
if(b.mon && a.up && a.rv < b.lv) c.mon = true;
}
return c;
} #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
void push(int rt) {
if(lazy[rt]) {
a[rt << ].lv += lazy[rt]; a[rt << ].rv += lazy[rt];
a[rt << | ].lv += lazy[rt]; a[rt << | ].rv += lazy[rt];
lazy[rt << ] += lazy[rt];
lazy[rt << | ] += lazy[rt];
lazy[rt] = ;
}
} void build(int l, int r, int rt) {
if(l == r) {
int x; scanf("%d", &x);
a[rt] = Node{x, x, , , , , , , , };
return;
}
int mid = l + r >> ;
build(lson); build(rson);
a[rt] = a[rt << ] + a[rt << | ];
} void update(int L, int R, int val, int l, int r, int rt) {
if(l >= L && r <= R) {
a[rt].lv += val; a[rt].rv += val;
lazy[rt] += val;
return;
}
int mid = l + r >> ;
push(rt);
if(L <= mid) update(L, R, val, lson);
if(R > mid) update(L, R, val, rson);
a[rt] = a[rt << ] + a[rt << | ];
} int main() {
scanf("%d", &n);
build(, n, );
scanf("%d", &m);
while(m--) {
int L, R, d;
scanf("%d%d%d", &L, &R, &d);
update(L, R, d, , n, );
printf("%d\n", a[].mx);
}
return ;
} /*
*/
Codeforces 739C Alyona and towers 线段树的更多相关文章
- Codeforces 739C - Alyona and towers(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 可能有人会问我为什么为这道 *2500 的 D1C 写题解,我觉得大概是想要在写题解数量上 dd ycx 吧,因为 ycx 到目前为止写了 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
随机推荐
- JavaScript 无刷新修改浏览器URL地址栏
//发现地址栏已改为:newUrlvar stateObject = {}; var title = "Wow Title"; var newUrl = "/my/awe ...
- ELF格式探析之三:sections
前文链接: ELF格式探析之一:Segment和Section ELF格式探析之二:文件头ELF Header详解 今天我们讲对目标文件(可重定位文件)和可执行文件都很重要的section. 我们在讲 ...
- checklistboxx 多选取值 和选中
for (int i = 0; i < cklist.Items.Count; i++) { if (cklist.GetItemChecked(i)) { //修改子菜单的父节点为此菜单的id ...
- Kafka-Monitor
kafka Monitor 监测Kafka集群状态 Topic.Consumer Group列表 图形化展示 topic 和 consumer 之间的关系 图形化展示 consumer 的 Offse ...
- [C++]栈区(栈)与堆区(类链表)[转/摘]
一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其 操 ...
- CF28D Don't fear, DravDe is kind
传送门 题意:\(n\)个位置,每个位置有价值\(v_i\)和重量\(p_i\),要选出一些位置,如果要选位置\(i\),那么前面选的重量之和要为\(l_i\),后面选的重量之和要为\(r_i\),求 ...
- luogu P1979 [NOIP2013] 华容道
传送门 这道题中,棋子的移动是要移动到空格上去,所以空格要在棋子旁边才能移动棋子;而棋子移动的方向由空格决定 所以我们可以记三维状态\(di_{i,j,k}\),表示状态为棋子在\((i,j)\),空 ...
- 记录一个PHP安装redis扩展时的问题
安装过程:https://www.cnblogs.com/pengyunjing/p/8688320.html 由于我之前安装过该扩展,重新安装时没有执行make clean命令,所以安装好出现了下面 ...
- 2017-2018-2 165X 『Java程序设计』课程每周成绩公布
2017-2018-2 165X 『Java程序设计』课程 每周成绩公布 本博客将跟随教学进度不定期更新,每次更新后将在课程群公布.如对成绩有疑问,请于公布成绩后的1天之内联系助教,进行审核确认. - ...
- libSVM在matlab下的使用安装
1) 从LIBSVM的官网http://www.csie.ntu.edu.tw/~cjlin/libsvm/上下载最新版本的LIBSVM,当前版本为libsvm-3.18.zip 2) 解压压缩包到电 ...