CodeForces 622C Not Equal on a Segment
2016-04-06 23:16
507 查看
Not Equal on a Segment
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d& %I64u
Description
You are given array a with
n integers and m queries. Thei-th query is given with three integers
li, ri, xi.
For the i-th query find any position
pi (li ≤ pi ≤ ri) so thatapi ≠ xi.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements ina
and the number of queries.
The second line contains n integers
ai (1 ≤ ai ≤ 106) — the elements of the arraya.
Each of the next m lines contains three integersli, 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 toxi in segment[li, ri]
or the value - 1 if there is no such number.
Sample Input
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次询问,每次询问区间中与给定的数不相同的数的下标 分析: 对于一个区间来说,如果我们知道两个值,这两个值一定不同,并且在区间中,那么对与这个区间的询问来说, 指定的数要么在这两个数中,要么不在,如果在,则取另一个数,这样就解决了选取的数的问题。 一开始想的是离线处理询问,再O(n)遍历整个数列,更新这两个不同值,结果不知道哪里写挫了,WA了。 后来想了想,对于一个区间右端点来说,如果知道了这个端点对应左边不同值的下标,那么问题就就解决了么? 如此一来只需要预处理一下连续相同数字的对应下标值就行了#include<cstring> #include<string> #include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<map> #include<cstdlib> #include<cmath> #include<vector> //#pragma comment(linker, "/STACK:1024000000,1024000000"); using namespace std; #define INF 0x3f3f3f3f #define maxn 200005 int pos[maxn]; int a[maxn]; int main() { int n,q; while(scanf("%d%d",&n,&q)!=EOF) { memset(pos,0,sizeof pos); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int p=n; for(int i=n-1;i>=1;i--) { if(a[i]==a[p]) continue; for(int j=p;j>i;j--) pos[j]=i; p=i; } for(int i=0;i<q;i++) { int l,r,c; scanf("%d%d%d",&l,&r,&c); if(l==r||a[r]!=c) { if(a[r]!=c) printf("%d\n",r); else printf("-1\n"); continue; } if(pos[r]!=0&&pos[r]>=l&&a[pos[r]]!=c) { printf("%d\n",pos[r]); } else printf("-1\n"); } } return 0; }
相关文章推荐
- 《常见算法和数据结构》元素排序(3)——洗牌算法
- 《Algorithms算法》笔记:元素排序(3)——洗牌算法
- 数据库常用sql语句
- Android加载大图避免OOM
- mysql必知必会--第一回 检索单个表列 多个表列
- 【BZOJ-1497】最大获利 最大流
- POJ1080Human Gene Functions(LCS变形)
- 浅谈web语义化
- [转]红帽 Red Hat Linux相关产品iso镜像下载【百度云】
- Ajax方法实现登录页面
- 复利结对项目
- Linux内核第七节 20135332武西垚
- 百度定位实现操作
- Ubuntu 11.04 安装后要做的20件事情 -----------转载
- Uva 10462 Is There A Second Way Left? Kruskal求次小生成树
- Android客户端实现注册/登录详解(二)
- LintCode-不同的子序列
- 结对编程四则运算
- 快速定制SolidWorks Electrical清单模板
- 深入了解Struts2返回JSON数据的原理及具体应用范例