Educational Codeforces Round 7(C)线段树

来源:互联网 发布:大纲写作软件 编辑:程序博客网 时间:2024/05/23 01:51

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 41 2 1 1 3 51 4 12 6 23 4 13 4 2
output
26-14



题意:给你N个数字,m次查询,每次问在[LI,RI]区间有没有不等于的XI这个数字,如果有输出任意一个数字的下标



题解:这里可是考虑使用线段树,我们更新最值(MAX,MIN),如果这个区间的最值不等于XI,那么必定是有解的,否则肯定是无解的,那么我们直接愉快的上线段树就好了,更新最值。



#include<iostream>#include<cstdio>using namespace std;#define lson L,mid,rt<<1#define  rson mid+1,R,rt<<1|1#define N 300005struct point{int x,org;bool operator<(const point &x1)const{return this->x<x1.x;}point (int _x,int _org):x(_x),org(_org){}point (){}};struct segment{int lc,rc,rt;point mx,mi;int mid(){return (lc+rc)>>1;}}tree[N<<2];point ans;void pushup(int rt){tree[rt].mx=max(tree[rt<<1].mx,tree[rt<<1|1].mx);tree[rt].mi=min(tree[rt<<1].mi,tree[rt<<1|1].mi);}void build(int L,int R,int rt){tree[rt].lc=L;tree[rt].rc=R;if(L==R){scanf("%d",&tree[rt].mi.x);tree[rt].mi.org=L;tree[rt].mx=tree[rt].mi;return ;}int mid=tree[rt].mid();build(lson);build(rson);pushup(rt);}void  query(int L,int R,int rt, int num){ if(tree[rt].lc==L&&tree[rt].rc==R)  {if(num!=tree[rt].mi.x){ans=tree[rt].mi;}if(num!=tree[rt].mx.x){ans=tree[rt].mx;}return ;}int mid=tree[rt].mid();if(R<=mid)query(L,R,rt<<1,num);else if(L>mid)query(L,R,rt<<1|1,num);else{query(lson,num);query(rson,num);}}int main(){#ifdef CDZSCfreopen("i.txt","r",stdin);#endifint n,m,xi,ri,li;scanf("%d%d",&n,&m);build(1,n,1);while(m--){scanf("%d%d%d",&li,&ri,&xi);ans=point(-1,-1);query(li,ri,1,xi);printf("%d\n",ans.org);}return 0;}







0 0
原创粉丝点击