Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
C. Not Equal on a Segment
题目连接:
http://www.codeforces.com/contest/622/problem/C
Description
You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi.
For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements in a and the number of queries.
The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.
Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106) — the parameters of the i-th query.
Output
Print m lines. On the i-th line print integer pi — the position of any number not equal to xi in segment [li, ri] or the value - 1 if there is no such number.
Sample Input
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
Sample Output
2
6
-1
4
Hint
题意
有n个数,然后m个询问,每次询问给你l,r,x
让你找到一个位置k,使得a[k]!=x,且l<=k<=r
题解:
并查集,我们将a[i]=a[i-1]的合并在一起,这样,我们就能一下子跳很多了
由于是找到不相等的,所以最多跳一步就能出结果,所以复杂度应该是比nlogn小的
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int a[maxn];
int fa[maxn];
int fi(int x)
{
return x == fa[x]?x:fa[x]=fi(fa[x]);
}
int uni(int x,int y)
{
int p = fi(x),q = fi(y);
if(p != q)
{
fa[p]=fa[q];
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==a[i-1])
uni(i,i-1);
}
for(int i=1;i<=m;i++)
{
int flag = 0;
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
int p = r;
while(p>=l)
{
if(a[p]!=x)
{
printf("%d\n",p);
flag = 1;
break;
}
p=fi(p)-1;
}
if(flag==0)printf("-1\n");
}
}
Educational Codeforces Round 7 C. Not Equal on a Segment 并查集的更多相关文章
- Educational Codeforces Round 1 D. Igor In the Museum bfs 并查集
D. Igor In the Museum Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598 ...
- Educational Codeforces Round 78 (Rated for Div. 2)D(并查集+SET)
连边的点用并查集检查是否有环,如果他们的fa是同一个点说明绕了一圈绕回去了.n个点一共能连n-1条边,如果小于n-1条边说明存在多个联通块. #define HAVE_STRUCT_TIMESPEC ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs
F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集
题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...
- 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 ...
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
随机推荐
- 让Fragment监听返回键
Activity可以很容易的得到物理返回键的监听事件,而Fragment却不能.所以使用到了以下的方法. 首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPre ...
- Loadrunner中与事务相关的概念及函数
一.事务 事务是指用户在客户端做一种或多种业务所需要的操作集,通过事务函数可以标记完成该业务所需要的操作内容:另一方面可以用来统计用户操作的相应时间.事务响应时间是指通过记录用户请求的开始时间和服务器 ...
- 【转】linux下cpio命令使用
转自:http://www.51testing.com/html/32/498132-816949.html 功能说明:备份文件. 补充说明:cpio是用来建立,还原备份档的工具程序,它可以加入,解开 ...
- <转>揭秘DNS后台文件:DNS系列之五
揭秘DNS后台文件 在前面的博文中我们介绍了DNS的体系结构,常用记录,还介绍了辅助服务器的配置,今天我们来介绍一下DNS服务器背后的几个文件.其实DNS服务器的工作完全依靠这几个文件,了解了DNS的 ...
- 我的日常工具——gdb篇
我的日常工具——gdb篇 03 Apr 2014 1.gdb的原理 熟悉linux的同学面试官会问你用过gdb么?那好用过,知道gdb是怎么工作的么?然后直接傻眼... gdb是怎么接管一个进程?并且 ...
- 开源框架DNN简介以及安装
donetnuke 是一款免费的开源cms框架,目前也有收费版,不过免费版也可以适应大家大部分的需求.我前些阵子是老板让我在20天内,做好一个官网并且发布,并且指定使用dnn这个框架,考虑到又可以学习 ...
- es6转码器-babel
babel 基本使用 安装转码规则 # ES2015转码规则 $ npm install --save-dev babel-preset-es2015 # react转码规则 $ npm instal ...
- MYSQL数据库重点:自定义函数、存储过程、触发器、事件、视图
一.自定义函数 mysql自定义函数就是实现程序员需要sql逻辑处理,参数是IN参数,含有RETURNS字句用来指定函数的返回类型,而且函数体必须包含一个RETURN value语句. 语法: 创建: ...
- HD1064Financial Management
Problem Description Larry graduated this year and finally has a job. He's making a lot of money, but ...
- JS初识(着重讲解Date函数)
查看类型:typeof() 转换为int类型:parseInt() isNaN() 函数用于检查其参数是否是非数字值. NaN,是Not a Number的缩写.一种计算机用语.NaN 用于处理计算中 ...