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 线段树的更多相关文章

  1. Codeforces 739C - Alyona and towers(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 可能有人会问我为什么为这道 *2500 的 D1C 写题解,我觉得大概是想要在写题解数量上 dd ycx 吧,因为 ycx 到目前为止写了 ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  4. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  5. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  6. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  7. Codeforces 444C DZY Loves Colors(线段树)

    题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...

  8. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  9. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

随机推荐

  1. 解决IntelliJ IDEA无法读取配置*.properties文件的问题

    idea对这些配置的文件方式很明显和eclipse是不同的.在idea中有一个 Content Roots的概念.需要为每一个folder配置相应的Content Roots.Content Root ...

  2. dense prediction

    Dense prediction  fully convolutional network for sementic segmentation 先用feature extractor 提特征,然后再使 ...

  3. iOS视频流开发(1)—视频基本概念

    iOS视频流开发(1)-视频基本概念 手机比PC的优势除了便携外,她最重要特点就是可以快速方便的创作多媒体作品.照片分享,语音输入,视频录制,地理位置.一个成功的手机APP从产品形态上都有这其中的一项 ...

  4. Android ROM资源文件存放位置

    位于目录:framework/core/res/res /frameworks/base/core/res/res/values/public.xml 上面的文件中公开了上层(也就是第三方应用或者系统 ...

  5. 关于django1.7.7使用ajax后出现“CSRF token missing or incorrect”问题的解决办法

    最近使用Python3.3.25和django1.7.7开发公司项目,在使用ajax来post数据时,居然一直提示:403错误,原因是“CSRF token missing or incorrect” ...

  6. Mybatis入门学习笔记

    1.定义别名 在sqlMapConfig.xml中,编写如下代码: <!-- 定义别名 --> <typeAliases> <!-- type: 需要映射的类型 alia ...

  7. 一步步使用Code::Blocks进行设置断点调试程序

    一.调试之前要做的工作 首先,我们要确保Code::Blocks的配置正确,调试工作才能进行得更顺利 为此,我们需要生成调试符号.调试符号可以让调试器知道代码的哪一行正在执行,这样你就可以知道程序运行 ...

  8. SpringBoot拦截器的注册

    (1).编写拦截器 package cn.coreqi.config; import org.springframework.util.StringUtils; import org.springfr ...

  9. 嵌入式Linux驱动笔记(十八)------浅析V4L2框架之ioctl【转】

    转自:https://blog.csdn.net/Guet_Kite/article/details/78574781 权声明:本文为 风筝 博主原创文章,未经博主允许不得转载!!!!!!谢谢合作 h ...

  10. python 中is和== 的理解

    Python中的对象包含三要素:id.type.value其中id用来唯一标识一个对象,type标识对象的类型,value是对象的值is判断的是a对象是否就是b对象,是通过id来判断的==判断的是a对 ...