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数组,直接记录数组每个元素的位置,并且实 ...
随机推荐
- C# .Net 下 x86使用大内存的处理
/LARGEADDRESSAWARE 选项通知链接器应用程序可处理大于 2 GB 的地址. 在 64 位编译器中,默认情况下启用此选项. 在 32 位编译器中,如果未在链接器行上指定 /LARGEAD ...
- 三种光照模型的shader实现
1.Lambert模型,公式为I=Kd*Il(N*L): Shader "Custom/Lambert_A" { Properties { _Diffuse(,,,) } SubS ...
- Lumen开发:简单实现auth用户认证
打开bootstrap/app.php,取消下面两段代码的注释, $app->middleware([ App\Http\Middleware\ExampleMiddleware::class ...
- PHP邮箱的正则表达式
邮箱的正则:/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i
- 素数定理 nefu 117
素数定理: 随着x的增长,P(x) ≍x/ln(x) ,P(x)表示(1,x)内的素数的个数. 这个定理,说明在1-x中,当x大到一定程度时,素数分布的概率为ln(x) 竟然还有一道题目. 素数个数的 ...
- 九度OJ 1193:矩阵转置 (矩阵计算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1673 解决:1132 题目描述: 输入一个N*N的矩阵,将其转置后输出.要求:不得使用任何数组(就地逆置). 输入: 输入的第一行包括一个 ...
- Qt插件开发入门(两种方法:High-Level API接口,Low-Level API接口)
Qt中为我们提供了两种开发插件的方式.一种是使用High-Level API接口,一种是使用Low-Level API接口.所谓High-Level API 是指通过继承Qt为我们提供的特定的插件基类 ...
- html5 css3 进度条特效
https://www.html5tricks.com/tag/css3%E8%BF%9B%E5%BA%A6%E6%9D%A1/page/3
- 全自动安装mongoDB数据库的shell脚本
最近在研究mongoDB数据库,写了个全自动安装mongoDB数据库的shell脚本,仅供参考,欢迎拍砖,内容如下: #!/bin/bash # shell的执行选项: # -n 只读取shell脚本 ...
- PAT 天梯赛 L2-005. 集合相似度 【SET】
题目链接 https://www.patest.cn/contests/gplt/L2-005 思路 因为集合中的元素 是不重复的 所以用SET 来保存 集合 然后最后 查找一下 两个集合中共有元素 ...