D - Zip-line

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int> using namespace std; const int N = 4e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ; int n, m, tot, h[N], hs[N], dp[][N], stk[N], cnt[N], LIS;
bool is[N]; struct ChairmanTree {
struct node {
int mx, l, r;
} a[N * ];
int tot, root[N];
void update(int p, int val, int l, int r, int &x, int y) {
x = ++tot; a[x] = a[y];
a[x].mx = max(a[x].mx, val);
if(l == r) return ;
int mid = l + r >> ;
if(p <= mid) update(p, val, l, mid, a[x].l, a[y].l);
else update(p, val, mid + , r, a[x].r, a[y].r);
}
int query(int L, int R, int l, int r, int x) {
if(hs[r] < L || hs[l] > R) return ;
if(hs[l] >= L && hs[r] <= R) return a[x].mx;
int ans = , mid = l + r >> ;
if(L <= hs[mid]) ans = query(L, R, l, mid, a[x].l);
if(R > hs[mid]) ans = max(ans, query(L, R, mid + , r, a[x].r));
return ans;
}
}ct[]; void getLIS() {
memset(stk, inf, sizeof(stk));
for(int i = ; i <= n; i++) {
dp[][i] = lower_bound(stk, stk + N, h[i]) - stk + ;
stk[dp[][i] - ] = h[i];
LIS = max(LIS, dp[][i]);
}
memset(stk, inf, sizeof(stk));
for(int i = n; i >= ; i--) {
dp[][i] = lower_bound(stk, stk + N, -h[i]) - stk + ;
stk[dp[][i] - ] = -h[i];
}
for(int i = ; i <= n; i++) {
if(dp[][i] + dp[][i] == LIS + ) {
is[i] = true;
cnt[dp[][i]]++;
}
}
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &h[i]);
hs[++tot] = h[i];
}
sort(hs + , hs + + tot);
tot = unique(hs + , hs + + tot) - hs - ; for(int i = ; i <= n; i++)
h[i] = lower_bound(hs + , hs + + tot, h[i]) - hs;
getLIS(); for(int i = ; i <= n; i++)
ct[].update(h[i], dp[][i], , tot, ct[].root[i], ct[].root[i-]);
for(int i = n; i >= ; i--)
ct[].update(h[i], dp[][i], , tot, ct[].root[i], ct[].root[i+]); while(m--) {
int a, b;
scanf("%d%d", &a, &b);
int ans = is[a] ? LIS - : LIS;
ans = max(ans, ct[].query(, b - , , tot, ct[].root[a-])
+ ct[].query(b + , inf, , tot, ct[].root[a+]) + );
if(is[a] && cnt[dp[][a]] > ) {
ans = max(ans, LIS);
}
printf("%d\n", ans);
}
return ;
} /*
*/

Codeforces Round #345 (Div. 1) D - Zip-line 带单点修改的LIS 主席树 | 离线树状数组的更多相关文章

  1. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  2. Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树

    D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...

  3. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  5. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  6. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  7. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  8. Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分

    D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...

  9. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

随机推荐

  1. jsp 的 4 种基本语法

    1.JSP 注释 2.JSP 声明 3.JSP 表达式 4.JSP 脚本 JSP 注释: 注释格式: <%-- 注释内容 --%> 需要注意的是,JSP 的注释不会输出到 HTML 中. ...

  2. STL源码分析-function

    http://note.youdao.com/noteshare?id=269e16541b43095eaf97636d4e046b1d

  3. 题解【CF277E Binary Tree on Plane】

    Description 给你平面上 \(n\) 个点 \((2 \leq n \leq 400)\),要求用这些点组成一个二叉树(每个节点的儿子节点不超过两个),定义每条边的权值为两个点之间的欧几里得 ...

  4. logstash 中配置GeoIP解析地理信息

    logstash中配置的GeoIP的数据库解析ip了,这里是用了开源的ip数据源,用来分析客户端的ip归属地.官网在这里:MAXMIND 下载GeoLiteCity数据库 wget http://ge ...

  5. Mac(Linux)上安装memcached步骤

    Mac上安装memcached类似于在Linux平台上安装memcached. 主要需要做两块: 一.安装libevent库: 二.安装memcached; 一.安装libevent库 libeven ...

  6. [转]hadoop2.x常用端口

    原文地址:http://www.zhixing123.cn/ubuntu/40649.html HDFS DataNode 50010 dfs.datanode.address datanode服务端 ...

  7. 【BZOJ】1087: [SCOI2005]互不侵犯King

    [算法]状态压缩型DP [题解]http://www.cnblogs.com/xtx1999/p/4620227.html (orz) https://www.cnblogs.com/zbtrs/p/ ...

  8. 爬虫--Scrapy之Downloader Middleware

    下载器中间件(Downloader Middleware) 下载器中间件是介于Scrapy的request/response处理的钩子框架. 是用于全局修改Scrapy request和respons ...

  9. itext 生成pdf文件添加页眉页脚

    原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...

  10. docker之安装和基本使用(一)

    前言 开始折腾docker. 主要概念 容器:独立运行的一个或一组应用,与其他应用完全独立. 镜像:用于创建 Docker容器的模板. 仓库:用于收纳镜像文件,可以理解为代码控制中的代码仓库 注意: ...