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 ...
随机推荐
- pycharm sql语句警告
产生原因为没有配置数据库,配置数据库,似乎没什么作用 那么,直接去掉他的警告提示 找到setting->editor->inspections,然后找到右边sql下的 No data so ...
- JAVA迭代器学习--在JAVA中实现线性表的迭代器
1,迭代器是能够对数据结构如集合(ADT的实现)进行遍历的对象.在遍历过程中,可以查看.修改.添加以及删除元素,这是它与一般的采用循环来遍历集合中的元素不同的地方.因为,通常用循环进行的遍历操作一般是 ...
- JS知识整理之 Call&Apply方法
JavaScript中的函数也是对象,和其他JS对象一样也可以包含方法,其中Call和Apply就是其中比较重要的方法,可以用来间接的调用函数.这两个方法允许显式制定调用所需的this值,也就是说所有 ...
- The android command is deprecated
新版的SDK tools中的android命令已经不支持 android create project,用起来很不顺手. The "android" command is depr ...
- C# 简单的反射机制实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- POJ2031 Building a Space Station【最小生成树】
题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再搭桥.求搭建通路的最小边长总和是多少. 思路: 先处理空间点之间的距离 ...
- D - Laying Cables Gym - 100971D (单调栈)
题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...
- 【windows核心编程】注入DLL时BUG排除与调试
DLL注入排除bug的思路步骤. 1.在VS中监视输入err,hr检查DLL是否注入成功 2.OD断点loadlibraryW,loadlibraryA是否已经注入成功,eax是否有值. 3.检查路径 ...
- C++学习5-面向对象编程基础(构造函数、转换构造、静态数据成员、静态成员函数、友元)
知识点学习 类 const作用 C语言的const限定符的含义为"一个不能改变值的变量",C++的const限定符的含义为"一个有类型描述的常量": const ...
- Day5------------系统启动流程
一.引导顺序 BIOS--------------------->MBR-------------------->boot loader------------------------&g ...