题目链接:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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], ...

  5. Codeforces 622C Not Equal on a Segment 【线段树 Or DP】

    题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...

  6. Educational Codeforces Round 7

    622A - Infinite Sequence    20171123 暴力枚举\(n\)在哪个区间即可,时间复杂度为\(O(\sqrt{n})\) #include<stdlib.h> ...

  7. postgresql 9源码安装

    安装过程: 1.configuration --prefix=PREFIX install all files under the directory PREFIX instead of usr/lo ...

  8. PostgreSQL源码安装文档

    This document describes the installation of PostgreSQL using the source    code distribution. (If yo ...

  9. Circles and Pi

    Circles and Pi Introduction id: intro-1 For as long as human beings exist, we have looked to the sky ...

随机推荐

  1. Java面试手写代码题

    1.栈实现 2.Iterator实现 3.单例 4.多线和控制(暂停,恢复,停止) 5.生产者消费者

  2. HDU3294 Girls' research —— Manacher算法 输出解

    题目链接:https://vjudge.net/problem/HDU-3294 Girls' research Time Limit: 3000/1000 MS (Java/Others)    M ...

  3. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

  4. Centos6.8更好yum源

    第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back ...

  5. html5--6-41 CSS背景

    html5--6-41 CSS背景 实例 学习要点 掌握CSS背景属性的使用 元素的背景属性: background 简写属性,作用是将背景属性设置在一个声明中. background-attachm ...

  6. codeforces 667D D. World Tour(最短路)

    题目链接: D. World Tour time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  7. 机器学习 Hidden Markov Models 1

    Introduction 通常,我们对发生在时间域上的事件希望可以找到合适的模式来描述.考虑下面一个简单的例子,比如有人利用海草来预测天气,民谣告诉我们说,湿漉漉的海草意味着会下雨,而干燥的海草意味着 ...

  8. RxJava入门之路(一)

    RxJava接触过蛮长时间了,但是让我说个所以然来还是说不出来,归根结底还是还是理解不够深刻,趁着年底这个时候争取写个系列出来给自己的学习做个记录 注意区分RxJava1.0和2.0的区别,以下默认是 ...

  9. (水题)洛谷 - P1093 - 奖学金

    https://www.luogu.org/problemnew/show/P1093 #include<bits/stdc++.h> using namespace std; #defi ...

  10. XTU1266:Parentheses(贪心+优先队列)

    传送门 题意 从左到右有n个连续的组,每一组有Li个括号,要么全是左括号,要么全是右括号,以及该组的每一个左括号翻成右括号, 或者右括号翻成左括号的花费Di.可以对这n个组的括号进行翻转,每一个括号都 ...