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 ...
随机推荐
- bzoj4169: Lmc的游戏
终于有道我会的了... int f[2][maxn],g[2][maxn],tot[maxn];//构造叶子编号时希望最大/小result 先手取子树最小/大的编号的排名 tot是子树中叶子个数 如果 ...
- HDU2476 String painter —— 区间DP
题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others) Me ...
- PHP——巧用PHP函数array_merge()合并数组
前言 返回联系人列表,包含所有的字母,之前返回的是存在这个联系人才会返回对应的大写字母,后面更改了要求要返回所有的字母从A-Z.PHP内置的一些函数活用起来,真的很省劲! 步骤 一般这时候大家可能直接 ...
- html5--6-11 CSS选择器7--伪类选择器
html5--6-11 CSS选择器7--伪类选择器 #E:target 选择当前活动的锚点元素. 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候, ...
- I.MX6 android 4.2 源码下载
/************************************************************************* * I.MX6 android 4.2 源码下载 ...
- "standard,singleTop,singleTask,singleInstance"-Android启动模式
安卓有4种启动模式,下面我们就进行详细的讲解 用栈的思维去理解,就能理解这些启动模式的本质了 先设置两个页面: A(为测试对象),B两个页面,两个页面都有跳至对方的按钮 一.标准模式(standard ...
- poj 3734 Blocks【指数型生成函数】
指数型生成函数,推一推可得: \[ (1+\frac{x^1}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+...)^2+(1+\frac{x^2}{2!}+\frac{x^4 ...
- hdu 3484 Interviewe RMQ+二分
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; + ...
- 跟我一起玩Win32开发(5):具有单选标记的菜单
帅哥们,美女们,下午好,我又来误人子弟,请做好准备. 今天,我们的目的是,想要实现下图中的这种菜单效果. 就是一种类似单选按钮的菜单,多个菜单项中,同时只有一个会被选中. 首先,我们在资源编辑器中,设 ...
- 自己写一个websocket
import socket, base64, hashlib sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsock ...