CodeForces 622 C. Not Equal on a Segment(水~)
来源:互联网 发布:ios开发需要mac吗 编辑:程序博客网 时间:2024/05/21 02:49
Description
给出一个长度为n的序列,m次查询,每次查询询问区间[l,r]中是否存在不等于x的数,如果不存在则输出-1,存在则任意输出区间中任意一个不等于x的值
Input
第一行为两个整数n和m分别表示序列长度和查询次数,第二行为n个整数ai表示该序列,之后m行每行三个整数li,ri,xi表示一次查询(1<=n,m<=2*10^5,1<=ai<=10^6,1<=li<=ri<=n,1<=xi<=10^6)
Output
对于每次查询,如果区间[li,ri]中存在不等于xi的数则任意输出一个,否则输出-1
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
Solution
简单题,每次直接暴力枚举会超时,但是如果把数值相同的连续子串合并就可以了
Code
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define maxn 222222int n,m,a[maxn],pos[maxn];struct node{ int l,r,v;}p[maxn];int main(){ while(~scanf("%d%d",&n,&m)) { int l,r,x,res; for(int i=1;i<=n;i++) scanf("%d",&a[i]); res=0,l=1; for(;l<=n;l++) { r=l; while(r<=n&&a[l]==a[r])r++; if(r==n+1)r=n; else if(a[l]!=a[r])r--; for(int i=l;i<=r;i++)pos[i]=res; p[res].l=l,p[res].r=r,p[res++].v=a[l]; l=r; } while(m--) { scanf("%d%d%d",&l,&r,&x); int flag=0; for(int i=pos[l];i<res;i++) { if(p[i].r<l)continue; if(p[i].l>r)break; if(p[i].v!=x) { if(p[i].r<=r)printf("%d\n",p[i].r); else if(p[i].l>=l)printf("%d\n",p[i].l); else printf("%d\n",r); flag=1; break; } } if(!flag)printf("-1\n"); } } return 0;}
0 0
- CodeForces 622 C. Not Equal on a Segment(水~)
- Codeforces 622 C. Not Equal on a Segment
- CodeForces 622C Not Equal on a Segment
- CodeForces 622C Not Equal on a Segment
- Codeforces 622C Not Equal on a Segment
- CodeForces 622C F - Not Equal on a Segment
- Codeforces 622C Not Equal on a Segment(线段树)
- CodeForces 622C Not Equal on a Segment(并查集)
- Codeforces 622C Not Equal on a Segment (并查集思想)
- Educational Codeforces Round 7--C. Not Equal on a Segment
- Educational Codeforces Round 7 C. Not Equal on a Segment
- Educational Codeforces Round 7-C. Not Equal on a Segment
- Not Equal on a Segment CodeForces
- Not Equal on a Segment CodeForces
- Not Equal on a Segment CodeForces
- Not Equal on a Segment CodeForces
- Codeforces 622C Not Equal on a Segment 【线段树 or dp】
- Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
- Sql 变量声明
- 制作帮助文档的好助手- WORD2CHM WORD2HTML
- 【JavaWeb Structs2学习】Struts2_标签,property、iterator
- Spring 4.2.4.RELEASE MVC 学习笔记 - 4 - FreeMarker(咋个办呢 zgbn)
- 整数转换成罗马数字java实现
- CodeForces 622 C. Not Equal on a Segment(水~)
- UIViewController过多问题
- IQ 域名被删除事件 的前因后果
- 对于RGB-D相机,视觉密集型下的SLAM算法(Dense Visual SLAM for RGB-D Cameras)
- mysql最大连接数
- nagios,ganglia,mysql构建服务器集群监控体系【todo】
- mysql常用函数汇总(分享)
- keepalived,ipvsadm进行负载分担【todo】
- Win10 专业版完美激活