noi.ac 257 B
题目
区间[l,r]是连续满足,[l,r]中的数字的权值区间是一段连续的。多次询问可以完包含一个区间的连续区间。区间长度尽量小,如果有多个输出左端点靠左的。
分析:
[l,r]区间是连续的,当且仅当区间内有(r-l)*2个相邻的关系,即(2,3),(6,5)都是相邻关系。那么将询问离线,不断维护左端点到当前点的区间内的相邻关系的数量。
即当前点是i,那么如果pos[a[i]-1]<=i的话,在1~pos[a[i]-1]这些位置+1,表示从这些位置到i的区间,增加一个相邻关系。
如果一个点j开始到i的相邻关系的数量等于(i-j),那么说明(j~i)区间是连续区间,这里两个相邻关系只算了一个。所以初始时在每个位置增加数字下标即可。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
#define pa pair<int,int>
using namespace std;
typedef long long LL; inline LL read() {
LL x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
pa T[N << ];
int tag[N << ], pos[N], a[N], ans1[N], ans2[N], n;
set< pa > s;
vector< pa > q[N]; pa operator + (pa A, pa B) { return A.first > B.first ? A : B; } inline void col(int x,int y) { T[x].first += y, tag[x] += y; }
inline void pushdown(int rt) { col(rt << , tag[rt]); col(rt << | , tag[rt]); tag[rt] = ; } void build(int l,int r,int rt) {
if (l == r) { T[rt] = pa(l, l); return ; }
int mid = (l + r) >> ;
build(l, mid, rt << ); build(mid + , r, rt << | );
T[rt] = T[rt << ] + T[rt << | ];
}
void update(int l,int r,int rt,int L,int R) { if (L <= l && r <= R) { T[rt].first ++, tag[rt] ++; return ; }
int mid = (l + r) >> ;
if (tag[rt]) pushdown(rt);
if (L <= mid) update(l, mid, rt << , L, R);
if (R > mid) update(mid + , r, rt << | , L, R);
T[rt] = T[rt << ] + T[rt << | ];
}
pa query(int l,int r,int rt,int L,int R) {
if (L <= l && r <= R) return T[rt];
if (tag[rt]) pushdown(rt);
int mid = (l + r) >> ;
if (R <= mid) return query(l, mid, rt << , L, R);
else if (L > mid) return query(mid + , r, rt << | , L, R);
else return query(l, mid, rt << , L, R) + query(mid + , r, rt << | , L, R);
}
bool check(pa x,int i) {
pa now = query(, n, , , -x.first);
if (now.first == i) {
ans1[x.second] = now.second, ans2[x.second] = i;
return ;
}
return ;
}
int main() {
n = read();
for (int i = ; i <= n; ++i) a[i] = read(), pos[a[i]] = i;
int m = read();
for (int i = ; i <= m; ++i) {
int l = read(), r = read(); q[r].push_back(pa(-l, i));
}
build(, n, );
for (int i = ; i <= n; ++i) {
for (int j = ; j < (int)q[i].size(); ++j) s.insert(q[i][j]);
if (a[i] > && pos[a[i] - ] <= i) update(, n, , , pos[a[i] - ]);
if (a[i] < n && pos[a[i] + ] <= i) update(, n, , , pos[a[i] + ]);
while (!s.empty())
if (check(*s.begin(), i)) s.erase(s.begin());
else break;
}
for (int i = ; i <= m; ++i) printf("%d %d\n", ans1[i], ans2[i]);
return ;
}
noi.ac 257 B的更多相关文章
- # NOI.AC省选赛 第五场T1 子集,与&最大值
NOI.AC省选赛 第五场T1 A. Mas的童年 题目链接 http://noi.ac/problem/309 思路 0x00 \(n^2\)的暴力挺简单的. ans=max(ans,xor[j-1 ...
- NOI.ac #31 MST DP、哈希
题目传送门:http://noi.ac/problem/31 一道思路好题考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个 ...
- NOI.AC NOIP模拟赛 第五场 游记
NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...
- NOI.AC NOIP模拟赛 第六场 游记
NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...
- NOI.AC NOIP模拟赛 第二场 补记
NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...
- NOI.AC NOIP模拟赛 第一场 补记
NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...
- NOI.AC NOIP模拟赛 第四场 补记
NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...
- NOI.AC NOIP模拟赛 第三场 补记
NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...
- NOI.AC WC模拟赛
4C(容斥) http://noi.ac/contest/56/problem/25 同时交换一行或一列对答案显然没有影响,于是将行列均从大到小排序,每次处理限制相同的一段行列(呈一个L形). 问题变 ...
随机推荐
- sql Server 查询方法的优化
在使用SQL语句查询数据库记录时,如果要查询相同的内容,有着不同的多种方法. 仍然,尽管使用多种方法可以得到相同的结果,但是,如果您使用不同的方法,在执行效益上是截然不同的.因此,我们得仔细考虑,如果 ...
- ecplice 中添加JavaFX插件学习
fxml文件使用SceneBuilder打开报错 解决方法:Window-->Preferences-->JavaFX-->browse 路径是可执行的JavaFX Scene Bu ...
- OpenResty 安装配置
0. 说明 1. Windows 下安装 下载软件包 openresty-1.13.6.1-win32.zip ,解压即可食用. [开启] 直接运行 nginx.exe 在 Windows 的命令窗口 ...
- Python pandas & numpy 笔记
记性不好,多记录些常用的东西,真·持续更新中::先列出一些常用的网址: 参考了的 莫烦python pandas DOC numpy DOC matplotlib 常用 习惯上我们如此导入: impo ...
- SQL SERVER 2005镜像配置(有无见证服务器都行)
我用的是没有见证的,但找的文章里有镜像,所以都做一下补充,两个网址做的参考, 之所以在从他们那再补充一次是为了怕有一天他们的文章被删了我这还有个备用的,这两篇写的不错 其他的都不行 特别乱,这是找的最 ...
- 投稿核心期刊、中文重要期刊、SCI二区及以上期刊目录
大家在研究生期间想必均经历过投稿核心期刊的烦恼,不知道哪些是核心期刊,那些是普通期刊,万一选的不对岂不是浪费了时间,因此小顾在网络上收集了了2018北大核心期刊目录及全国中文重要期刊目录和SCI二区及 ...
- 常用npm 命令
npm 官方网站:npm的使用说明 安装模块 npm install 安装当前目录package.json文件中配置的dependencies模块 安装本地的模块文件 npm install ...
- Android (争取做到)最全的底部导航栏实现方法
本文(争取做到)Android 最全的底部导航栏实现方法. 现在写了4个主要方法. 还有一些个人感觉不完全切题的方法也会简单介绍一下. 方法一. ViewPager + List<View> ...
- 借助强大的IDEA开发ide高效实现equals,hashcode以及toString方法
IDEA工具提供多种生成hashCode与equals的代码方案,注意:尽量不要使用第一个方案,第一个方案对于null不做判空处理,容易NNP问题. 对于生成toString方法方案,默认使用的是+拼 ...
- 数据同步canal服务端介绍
1.下载安装包 canal&github的地址,最权威的学习canal相关知识的地方 https://github.com/alibaba/canal 在下面的wiki列表中找到AdminGu ...