codeforces 622C C. Not Equal on a Segment
1 second
256 megabytes
standard input
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.
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.
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.
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
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的更多相关文章
- 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 ...
- 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 ...
- Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...
- 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], ...
- 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块
[题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...
- CF622C Not Equal on a Segment
题目链接: http://codeforces.com/problemset/problem/622/C 题目大意: 给定一个长度为n(n不超过200000)的序列,有m(m不超过200000)次询问 ...
- Codeforces 1154B Make Them Equal
题目链接:http://codeforces.com/problemset/problem/1154/B 题意:给定数组,可以给任意的的元素加上D 或者 减去D,如果能 使数组元素都相等,输出最小的D ...
- codeforces 622C. Optimal Number Permutation 构造
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...
- CodeForces 622C
题意: 给你一个数组,m个询问,l,r,x;让你输出在区间[ l , r ]上哪个位置不等于x. 思路: 额..我这个思路还是剽来的...不过真心赞啊. 开个p数组,直接记录数组每个元素的位置,并且实 ...
随机推荐
- saltstack内置state模块user
user 模块是用来创建用户和管理用户设定的,用户可以被设置成 present 状态或者 absent 状态. hwg: user.present: - fullname: Jim - shell: ...
- html5小趣味知识点系列(二)tabindex
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 驱动程序分层分离概念_总线驱动设备模型_P
分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...
- Educational Codeforces Round 1 (C) (atan2 + long double | 大数)
这题只能呵呵了. 东搞西搞,折腾快一天,最后用了一个800多行的代码AC了. 好好的题目你卡这种精度干啥. 还有要卡您就多卡点行不,为什么long double 又可以过... 废了N长时间写个了不管 ...
- 【BZOJ4004】[JLOI2015]装备购买 贪心+高斯消元
[BZOJ4004][JLOI2015]装备购买 Description 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 ( ...
- Django 基于Ajax & form 简单实现文件上传
前端实现 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="U ...
- Linux修改网络配置
修改:/etc/sysconfig/network-scripts/ifcfg-eth0 重启网卡/etc/rc.d/init.d/network restart
- JPA 多表分页查询
业务场景:大学生毕业后统计毕业去向.学生实体和毕业去向实体一对一关系. 实体: @Entity @Data @Table(name = "t_s_student") public ...
- python3 计算文件夹中所有py文件里面代码行数,注释行数,空行数
import os,re #代码所在位置 FILE_PATH = './' def analyze_code(codefilesource): ''' 打开一个py文件统计其中的代码行数,包括空格和注 ...
- 配置asp.net IIS服务器遇到的问题以及详细图例解决方案
原来配置过IIS服务,但是没有将自己的程序放在IIS服务器上,今天突发奇想,看看自己写的程序怎么样啊,能否在服务器上运行一下,说干就干,但是遇到了很多困难,今天把陪孩子过程以及遇到的问题给大家说说,希 ...