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 ...
随机推荐
- mysql优化----explain的列分析
sql语句优化: : sql语句的时间花在哪儿? 答: 等待时间 , 执行时间. 等待时间:看是不是被锁住了,那就不是语句层面了是服务端层面了,看连接数内存. 执行时间:到底取出多少行,一次性取出1万 ...
- vue中导出Excel表格
项目中我们可能会碰到导出Excel文件的需求,一般后台管理系统中居多,将table中展示的数据导出保存到本地.当然我们也可以通过一些处理来修改要导出的数据格式,具体需求具体对待. 1.首先我们需要安装 ...
- 非常精彩的Silverlight 2控件样式
概述 大家是否觉的现在Silverlight 2提供的默认的控件不能满足自己的要求?好在Silverlight的控件可以运用皮肤,微软Silverlight控件的设计者的主管Corrina开发了几套非 ...
- [Selenium] Android 中旋转屏幕,触摸,滚动
package com.learingselenium.android; import junit.framework.TestCase import org.openqa.selenium.Rota ...
- web自动化测试的自身特点
1.web页面是出现的元素可能具有不确定性 2.不同操作系统上不同web浏览器之间的兼容性 3.web应用的高并发性和容错性 4.移动设备上web客户端兼容性,旋转下和各种触摸特性
- [SHOI 2013] 发微博
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4419 [算法] 用std :: set维护每个人的好友集合 当两人成为好友时将每人接 ...
- 【扬中集训DAY1T1】 微信群
[题目链接] 点击打开链接 [算法] 对问题稍加分析后,发现其实要求的就是 : C(N,K) + C(N,K+1) + C(N,K+2) + ... + C(N,N) 因为N最大10^9,K最大10^ ...
- http监听工具Fildder
空间管理您的位置: 51Testing软件测试网 » 雨洒泪 » 日志 中国龙,中国风,中国会变得更加强大! 力量越大,责任越大! 人的一生,会面临很多选择,但决定一个人一生的,往往就是最关键那么的一 ...
- 实现列表两端完全对其text-aligh:justify 和display:inline-block
dispaly:inline-block 对于块级元素,在IE8-下是不兼容的,所以一般不要用在这些元素上,可以用在inline元素上:span text-align:justify实现子元素完全对齐 ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 14. ASP.NET Core Identity 入门
默认的身份认证好授权系统 UserManager用来操作用户的类, Singi用来身份认证的 添加AccountController 先声明SignInManager和UserManager这两个服务 ...