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
思路:求要询问的区间的最大和最小数,求出它们的下标,和所问的数比较,如果两个都和之相等则在这个区间无和他不同的,否则取最大和最小与之不同的下标即可,用线段树去维护每个区间最大最小值
复杂度为(n*logn);
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<math.h>
7 #include<cstdio>
8 #include<queue>
9 #include<stack>
10 #include<map>
11 int ask(int l,int r,int aa,int bb,int k);
12 int ask1(int l,int r,int aa,int bb,int k);
13 void build(int l,int r,int k);
14 using namespace std;
15 typedef long long ll;
16 int tree[1000000];
17 int tree2[1000000];
18 int a[400005];
19 int main(void)
20 {
21 int i,j,k,p,q;
22 tree[0]=0;
23 int nn,mm,kk;
24 while(scanf("%d %d",&p,&q)!=EOF)
25 {
26 for(i=1; i<=p; i++)
27 {
28 scanf("%d",&a[i]);
29 }
30 build(1,p,0);
31 while(q--)
32 {
33 scanf("%d %d %d",&nn,&mm,&kk);
34 a[0]=0;
35 int maxxid=ask(nn,mm,1,p,0);
36 a[0]=1e8;
37 int minnid=ask1(nn,mm,1,p,0);
38 int maxx=a[maxxid];
39 int minn=a[minnid];
40 if(kk==maxx&&kk==minn)
41 {
42 printf("-1\n");
43 }
44 else
45 {
46 if(kk!=maxx)
47 {
48 printf("%d\n",maxxid);
49 }
50 else printf("%d\n",minnid);
51 }
52 }
53 }
54 return 0;
55 }
56
57 void build(int l,int r,int k)
58 {
59 if(l==r)
60 {
61 tree[k]=l;
62 tree2[k]=l;
63 return ;
64 }
65 else
66 {
67 build(l,(l+r)/2,2*k+1);
68 build((l+r)/2+1,r,2*k+2);
69 tree[k]=a[tree[2*k+1]]>a[tree[2*k+2]]?tree[2*k+1]:tree[2*k+2];
70 tree2[k]=a[tree2[2*k+1]]<a[tree2[2*k+2]]?tree2[2*k+1]:tree2[2*k+2];
71 }
72 }
73 int ask(int l,int r,int aa,int bb,int k)
74 {
75 if(l>bb||r<aa)
76 {
77 return 0;
78 }
79 else if(l<=aa&&r>=bb)
80 {
81 return tree[k];
82 }
83 else
84 {
85 int ll=ask(l,r,aa,(aa+bb)/2,2*k+1);
86 int uu=ask(l,r,(aa+bb)/2+1,bb,2*k+2);
87 return a[ll]>a[uu]?ll:uu;
88 }
89
90 }
91 int ask1(int l,int r,int aa,int bb,int k)
92 {
93 if(l>bb||r<aa)
94 {
95 return 0;
96 }
97 else if(l<=aa&&r>=bb)
98 {
99 return tree2[k];
100 }
101 else
102 {
103 int ll=ask1(l,r,aa,(aa+bb)/2,2*k+1);
104 int uu=ask1(l,r,(aa+bb)/2+1,bb,2*k+2);
105 return a[ll]<a[uu]?ll:uu;
106 }
107 }

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

  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. codeforces 622C C. Not Equal on a Segment

    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. CF622C Not Equal on a Segment

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

  6. HZNU 2019 Summer training 6 -CodeForces - 622

    A - Infinite Sequence  CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++ ...

  7. CF242E XOR on Segment

    CF242E XOR on Segment codeforces 洛谷 关于异或,无法运用懒标记实现区间异或: 可以像trie树一样拆位,将每个值拆成二进制数,对此建相应个数的线段树. 0 1与 0异 ...

  8. Educational Codeforces Round 7

    622A - Infinite Sequence    20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...

  9. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

随机推荐

  1. 【转载】HBase基本数据操作详解【完整版,绝对精品】

    转载自: http://blog.csdn.net/u010967382/article/details/37878701 概述 对于建表,和RDBMS类似,HBase也有namespace的概念,可 ...

  2. my37_MGR流控对数据库性能的影响以及MGR与主从的性能对比

    mysql> show variables like 'group_replication_flow_control_applier_threshold'; +----------------- ...

  3. Docker的常用命令总结

    一.普通指令 启动 Docker sudo systemctl start docker 停止 Docker sudo systemctl stop docker 普通重启 Docker sudo s ...

  4. 面向切面编程(Spring AOP)

    一.什么是AOP AOP即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.主要体现在日志记录.性能统计.安全控制.事务处理和异常处理等. 1.相关概念 二.切面.切入点配 ...

  5. Lock 锁的实现

    锁的种类 自旋锁(spinlock):无法获得锁,就一直循环获取,适合短时间的加锁 睡眠锁(sleeplock):为了防止长时间的循环等待,在获取不到锁时,进程陷入睡眠,当锁释放时对睡眠进程进行唤醒 ...

  6. Flutter 中如何优雅的实现多渠道打包(埋点统计系列)

    我是 Zero,脑图先奉上 先赞后看,更新永不断 只要你关注 Flutter,这篇文章你绝对用得着,==> 强烈建议收藏 多渠道打包介绍 多渠道打包的主要作用是满足产品的运营需求,统计渠道和活动 ...

  7. shell脚本 awk实现查看ip连接数

    一.简介 处理文本,是awk的强项了. 无论性能已经速度都是让人惊叹! 二.使用 适用:centos6+ 语言:英文 注意:无 awk 'BEGIN{ while("netstat -an& ...

  8. vue双向绑定和深浅拷贝

    现象描述: vue 在使用的时候,当table绑定了某个data的时候.假如某个el-table-column下面的有个方法传参(data.row),然后在方法中用一个obj=data.row.(这里 ...

  9. Sentry 开发者贡献指南 - 前端(ReactJS生态)

    内容整理自官方开发文档 系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Map ...

  10. Windows FILETIME 与UNIX时间的转换

    windows FILETIME时间从1601/01/01 零时零分零秒开始计时,windows每个时钟滴答将计数加一,每个时钟滴答的间隔是100 nanoseconds(纳秒,1秒=10的九次方纳秒 ...