C. Not Equal on a Segment
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
output
2
6
-1
4

题意:给你一个数组,然后m条询问,在l[i]到r[i]之间是否存在一个数不等于x[i],存在输出这个数的位置,不然输出-1;

思路:开一个fa数组,记录与这个数一直不间断相同的数的位置,相当于把相同的数合并,在查找的时候就省事了,开始直接暴力是过不了的;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,a[N],fa[N],l,r,x;
int solve(int le,int ri,int ans)
{
int fx;
for(int i=le;i<=ri;)
{
if(fa[i]<=ri)fx=fa[i];
else fx=ri;
if(a[fx]!=ans)return fx;
i=fx+1;
}
return -1;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
fa[n]=n;
for(int i=n-1;i>0;i--)
{
if(a[i]==a[i+1])fa[i]=fa[i+1];
else fa[i]=i;
}
while(m--)
{
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",solve(l,r,x));
}
return 0;
}

codeforces 622C C. Not Equal on a Segment的更多相关文章

  1. 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 ...

  2. C. Not Equal on a Segment(codeforces)

    C. Not Equal on a Segment time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. Codeforces 622C Not Equal on a Segment 【线段树 Or DP】

    题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...

  4. CodeForces 622C Not Equal on a Segment

    预处理p[i],p[i]表示:[p[i],i]这段闭区间上所有数字都是a[i] 询问的时候,如果xi==a[ri]并且p[ri]<=li,一定无解 剩下的情况都是有解的,如果xi!=a[ri], ...

  5. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...

  6. CF622C Not Equal on a Segment

    题目链接: http://codeforces.com/problemset/problem/622/C 题目大意: 给定一个长度为n(n不超过200000)的序列,有m(m不超过200000)次询问 ...

  7. Codeforces 1154B Make Them Equal

    题目链接:http://codeforces.com/problemset/problem/1154/B 题意:给定数组,可以给任意的的元素加上D 或者 减去D,如果能 使数组元素都相等,输出最小的D ...

  8. codeforces 622C. Optimal Number Permutation 构造

    题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...

  9. CodeForces 622C

    题意: 给你一个数组,m个询问,l,r,x;让你输出在区间[ l , r ]上哪个位置不等于x. 思路: 额..我这个思路还是剽来的...不过真心赞啊. 开个p数组,直接记录数组每个元素的位置,并且实 ...

随机推荐

  1. 阿里云服务器---centos编译安装ffmpeg

    环境 系统环境:CentOS release 6.7 (Final) 需求 编译安装ffmpeg 获取依赖 安装依赖包 yum install -y autoconf automake cmake f ...

  2. Lumen开发:如何向 IoC 容器中添加自己定义的类

    版权声明:本文为博主原创文章,未经博主允许不得转载. 先在起始文件bootstrap/app.php加上$app->register(App\Providers\User\UserService ...

  3. vscode 和 atom 全局安装和配置 eslint 像 webstorm 等 ide 一样使用 standard标准 来检查项目

    首先你要安装了 nodejs ,然后在终端命令行输入下面的这堆 npm install eslint eslint-plugin-standard eslint-config-standard esl ...

  4. [转】IIS:Do not nest virtual directories

    原文:https://msdn.microsoft.com/en-us/library/ms178685.aspx#Anchor_6 Configuration settings for virtua ...

  5. spring AOP理解和相关术语

    一.AOP理解 AOP:横向抽取机制,底层使用代理方式实现. 示例: 现有LogDAO接口以及实现Log接口的Log类.类有add的方法,现在要打印add方法的开始时间和结束时间.(即增强Log的ad ...

  6. 15.Django添加一个功能模块的步骤(和SpringMVC类比)

    这里介绍如何在Django里新建一个模块,这个例子还是最简单的例子 通过浏览器访问 http://localhost:8000/hello/然后返回一个欢迎页 我是做java web出身的,这里用py ...

  7. (转)Javascript模块化编程(一):模块的写法

    随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个团队分工协作.进度管理.单元测试等等......开发者 ...

  8. Nvidia NVENC 硬编码预研总结

    本篇博客记录NVENC硬编码的预研过程 github:  https://github.com/MarkRepo/NvencEncoder 步骤如下: (1)环境搭建 (2)demo编译,测试,ARG ...

  9. memcpy使用

    void memcpy(void dest, const void *src, size_t n); 功能编辑 从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置 ...

  10. sublime 添加 颜色插件 colorcoder

    高亮所有变量,因此可以极大的简化代码定位.尤其是对那些有阅读障碍的程序员非常有帮助.