CF622C Not Equal on a Segment
题目链接:
http://codeforces.com/problemset/problem/622/C
题目大意:
给定一个长度为n(n不超过200000)的序列,有m(m不超过200000)次询问,第i次询问一个区间[li,ri]内是否存在一个数不等于一个给定值x。如果存在,就输出这个数的位置,否则输出-1。
解题思路:
预处理一个数组p[n]。p[i]表示从位置i向左一直到位置0,第一个不等于a[i]的数的位置。可以以o(n)的复杂度通过对递推实现。具体来说就是首先令p[0]=-1,然后从左向右递推,若a[i - 1] != a[i],则p[i] = i - 1,否则p[i] = p[i - 1]。
查询的时候首先检查a[r]是否等于x。若不等于则找到一个解r;否则检查p[r]即可。
代码:
#include <iostream>
#include <cstdio>
using namespace std; int a[];
int p[];
int n, t, l, r, x;
void init()
{
p[] = -;
for (int i = ; i < n; i++)
{
if (a[i] != a[i - ])
p[i] = i - ;
else
p[i] = p[i - ];
}
}
int main()
{
cin >> n >> t;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
init();
while (t--)
{
scanf("%d %d %d", &l, &r, &x);
l--;
r--;
if (a[r] != x)
{
printf("%d\n", r + );
}
else
{
if (p[r] != - && p[r] >= l)
{
printf("%d\n", p[r] + );
}
else
{
puts("-1");
}
}
}
return ;
}
CF622C 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 ...
- codeforces 622C C. Not Equal on a Segment
C. Not Equal on a Segment time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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
预处理p[i],p[i]表示:[p[i],i]这段闭区间上所有数字都是a[i] 询问的时候,如果xi==a[ri]并且p[ri]<=li,一定无解 剩下的情况都是有解的,如果xi!=a[ri], ...
- Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...
- Educational Codeforces Round 7
622A - Infinite Sequence 20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...
- postgresql 9源码安装
安装过程: 1.configuration --prefix=PREFIX install all files under the directory PREFIX instead of usr/lo ...
- PostgreSQL源码安装文档
This document describes the installation of PostgreSQL using the source code distribution. (If yo ...
- Circles and Pi
Circles and Pi Introduction id: intro-1 For as long as human beings exist, we have looked to the sky ...
随机推荐
- [RK3288][Android6.0] 调试笔记 --- pmu(rk818)寄存器读写【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/76919134 Platform: Rockchip OS: Android 6.0 Kern ...
- Bootloader与Kernel间参数传递机制 taglist【转】
本文转载自:http://blog.csdn.net/tommy_wxie/article/details/9187821 Tag list被用来在bootloader和Linux kernel 之间 ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- CentOS7 安装和配置Tomcat
1.官网下载Tomcat的压缩包 https://tomcat.apache.org/download-70.cgi 2.使用Xftp5把下载的压缩包上传到 /usr/soft (soft文件夹自己新 ...
- linux学习二(小随笔)
1apt-get 解包命令 tar zxvf ......... 打包命令 tar czvf ......... gz gunzip ........gz gzip ..........gz l ...
- 分享几篇文章(PDF版)
后天就要回老家了,除了团团年估计也没有其他好玩的,就琢磨下点经典文章带回去看看. Google一番找到了老赵很早以前的文章: http://www.cnblogs.com/JeffreyZhao/ar ...
- UVaLive 6581 && Gym 100299B What does the fox say? (模拟+STL)
题意:给定一些动物的叫声,然后再定某些动物的叫声,让你去除这些叫声后得到的叫声. 析:先存储所有的叫声,然后用map来记录其他的叫声,在输出时再判定一下就好. 代码如下: #pragma commen ...
- iOS UI控件
创建: 2018/04/21 完成: 2018/04/25 更新: 2018/09/24 补充UIActivityIndicatorView的显示和隐藏方法 UIButton 设定项目 项目名 ...
- 洛谷 - P5000 - Hillwer编码 - 高精度
https://www.luogu.org/problemnew/show/P5000 第一次写一个正经的高精度题. 很明显ASCII码的乘积绝对是溢出的. 那么直接上Java.正好学一手Java的字 ...
- Android6.0 危险权限和普通权限
Normal Permissions如下 ACCESS_LOCATION_EXTRA_COMMANDS ACCESS_NETWORK_STATE ACCESS_NOTIFICATION_POLICY ...