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 ...
随机推荐
- (linux)platform_driver_probe与platform_driver_register的区别
[驱动注册]platform_driver_register()与platform_device_register() 设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册 ...
- 微信公众号菜单与应用交互session
http://www.cnblogs.com/yank/p/3476874.html http://blog.csdn.net/zmhawk/article/details/43671195 http ...
- HDU1532 Drainage Ditches —— 最大流(sap算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...
- 【原】WPF客户端只能启动一次
public partial class App : Application { System.Threading.Mutex mutex; public App() { this.Startup + ...
- codeforces 669D D. Little Artem and Dance(乱搞题)
题目链接: D. Little Artem and Dance time limit per test 2 seconds memory limit per test 256 megabytes in ...
- 百度地图API应用之获取用户的具体位置
功能的大概:用户通过点击地图上面的位置,在地图上面进行描点,然后再把获取的到的地理位置保存到地图上面的地址栏目中. 主要是百度地图API的使用 .代码如下: var map = new BMap.Ma ...
- 【TJOI2013】 单词
[题目链接] 点击打开链接 [算法] AC自动机+递推 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 200 ...
- 【197】PowerShell 通过 FTP 下载文件
参考:使用 WGET 从FTP上下载文件 在 PowerShell 使用 wget2 工具,代码如下: wget2 ftp://user:password@192.168.14.31/1.jpg 其中 ...
- 基础总结篇之二:Activity的四种launchMode (转载)
转自:http://blog.csdn.net/liuhe688/article/details/6754323 合抱之木,生於毫末:九層之台,起於累土:千里之行,始於足下.<老子> 今天 ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...