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 ...
随机推荐
- Python基础(正则、序列化、常用模块和面向对象)-day06
写在前面 上课第六天,打卡: 天地不仁,以万物为刍狗: 一.正则 - 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法: - 在线正则工具:http://tool ...
- Python官方操作Excel文档
xlwt 1.3.0 Downloads ↓ Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 ...
- luogu 2294 狡猾的商人 带权并查集
此题做法多啊 带权并查集,区间dp,前缀和,差分约束 1.自己写的前缀和, 11 #include<bits/stdc++.h> #define rep(i,x,y) for(regist ...
- luogu P2779 [AHOI2016初中组]黑白序列
传送门 注:本题解中下标从1开始 这题可以想出一个\(O(n^2)\)的dp,只要考虑每个偶数位置可以从前面的哪个位置加上一个"B...W..."转移过来 然而数据范围有5e5,, ...
- vue学习一:新建或打开vue项目(vue-cli2)
vue-cli3的操作参考文章:vue/cli 3.0脚手架搭建,浅谈vue-cli 3 和 vue-cli 2的区别 1.前期准备: node.js环境,安装node npm或者cnpm(npm的淘 ...
- Django搭建简易博客教程(四)-Models
原文链接: http://www.jianshu.com/p/dbc4193b4f95 Django Model 每一个Django Model都继承自django.db.models.Model 在 ...
- Python 入门基础19 --面向对象、封装
2019.04.17 一.面向对象与面向过程 二.名称空间操作 三.类与对象的概念 四.语法 五.对象查找属性的顺序 2019.04.18 1.类与对象的所有概念:__init__方法 2.类的方法与 ...
- android的五个进程优先级,内存不足时被清理的顺序
Android操作系统尝试尽可能长时间的保持应用的进程,但当可用内存很低时最终要移走一部分进程.怎样确定那些程序可以运行,那些要被销毁,Android让每一个进程在一个重要级的基础上运行,重要级低的进 ...
- C++11 多线程编程
http://blog.csdn.net/column/details/ccia.html?&page=1
- [bzoj3123][洛谷P3302] [SDOI2013]森林(树上主席树+启发式合并)
传送门 突然发现好像没有那么难……https://blog.csdn.net/stone41123/article/details/78167288 首先有两个操作,一个查询,一个连接 查询的话,直接 ...