链接

题目

  区间[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的更多相关文章

  1. # NOI.AC省选赛 第五场T1 子集,与&最大值

    NOI.AC省选赛 第五场T1 A. Mas的童年 题目链接 http://noi.ac/problem/309 思路 0x00 \(n^2\)的暴力挺简单的. ans=max(ans,xor[j-1 ...

  2. NOI.ac #31 MST DP、哈希

    题目传送门:http://noi.ac/problem/31 一道思路好题考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个 ...

  3. NOI.AC NOIP模拟赛 第五场 游记

    NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...

  4. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  5. NOI.AC NOIP模拟赛 第二场 补记

    NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...

  6. NOI.AC NOIP模拟赛 第一场 补记

    NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...

  7. NOI.AC NOIP模拟赛 第四场 补记

    NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...

  8. NOI.AC NOIP模拟赛 第三场 补记

    NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...

  9. NOI.AC WC模拟赛

    4C(容斥) http://noi.ac/contest/56/problem/25 同时交换一行或一列对答案显然没有影响,于是将行列均从大到小排序,每次处理限制相同的一段行列(呈一个L形). 问题变 ...

随机推荐

  1. 'adb remount'的作用是什么?在什么情况下有用?

    'adb remount' 将 '/system' 部分置于可写入的模式,默认情况下 '/system' 部分是只读模式的.这个命令只适用于已被 root 的设备. 在将文件 push 到 '/sys ...

  2. LeetCode题解之Convert BST to Greater Tree

    1.题目描述 2.问题分析 使用一个vector将所有节点的值以升序排列.然后比较,求和,加上. 3.代码 TreeNode* convertBST(TreeNode* root) { if (roo ...

  3. MapReduce文件配置和测试

    1.前提:MapReduce能配置的前提是hdfs能够正常运行 2.在1的基础上,配置两个文件:    在hadoop文件夹下配置两个文件mapred-site.xml(由mapred-site.xm ...

  4. Can't locate Data/Dumper.pm in perl5的处理

    Can't locate Data/Dumper.pm in perl5的处理 wget http://www.cpan.org/modules/by-module/Data/Data-Dumper- ...

  5. [Redis_1] Redis 介绍 && 安装

    0. 说明 Redis 介绍 && 安装 1. Redis 介绍 2. Redis 安装(Windows 10) [2.1 解压 redis-2.2.2-win32-win64.rar ...

  6. Android高级_第三方下载工具Volley

    Volley下载主要应用于下载文本数据和图片数据两个方向,下面分别介绍: 一.使用Volley开启下载,首先要做的是导包和添加权限: (1)在build.gradle文件中导入依赖包:compile ...

  7. cocos2d-x2.2.3 Layer分析

    <pre name="code" class="cpp">Layer CCLayerColor: 能够改变Layer的背景,能够设置大小 CCLay ...

  8. 死磕nginx系列--使用nginx做负载均衡

    使用nginx做负载均衡的两大模块: upstream 定义负载节点池. location 模块 进行URL匹配. proxy模块 发送请求给upstream定义的节点池. upstream模块解读 ...

  9. ES6标准入门之数值的拓展解说

    ES6提供了二进制和八进制数值的新写法,分别用前缀0b(或0B)和0o(或0O)表示. 0b111110111 === 503                    // true 0o767 === ...

  10. tsconfig.json

    概述 如果一个目录下存在一个tsconfig.json文件,那么它意味着这个目录是TypeScript项目的根目录. tsconfig.json文件中指定了用来编译这个项目的根文件和编译选项. 一个项 ...