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]即可。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 int a[1000005];
 6 int p[1000005];
 7 int n, t, l, r, x;
 8 void init()
 9 {
10     p[0] = -1;
11     for (int i = 1; i < n; i++)
12     {
13         if (a[i] != a[i - 1])
14             p[i] = i - 1;
15         else
16             p[i] = p[i - 1];
17     }
18 }
19 int main()
20 {
21     cin >> n >> t;
22     for (int i = 0; i < n; i++)
23     {
24         scanf("%d", &a[i]);
25     }
26     init();
27     while (t--)
28     {
29         scanf("%d %d %d", &l, &r, &x);
30         l--;
31         r--;
32         if (a[r] != x)
33         {
34             printf("%d\n", r + 1);
35         }
36         else
37         {
38             if (p[r] != -1 && p[r] >= l)
39             {
40                 printf("%d\n", p[r] + 1);
41             }
42             else
43             {
44                 puts("-1");
45             }
46         }
47     }
48     return 0;
49 }
时间: 2024-12-12 15:05:08

CF622C Not Equal on a Segment的相关文章

postgresql 9源码安装

安装过程: 1.configuration --prefix=PREFIX install all files under the directory PREFIX instead of usr/local/psql --with-pgport=NUMBER set NUMBER as the default port number for server and clients,the default is 5432. --with-segsize=SEGSIZE(控制表的大小) set the

PostgreSQL源码安装文档

This document describes the installation of PostgreSQL using the source    code distribution. (If you are installing a pre-packaged distribution,    such as an RPM or Debian package, ignore this document and read the    packager's instructions instea

HZNU 2019 Summer training 6 -CodeForces - 622

A - Infinite Sequence  CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++).最后问你第n个位置是什么数字. 思路:如果你花点时间列数列的话会发现,1~n的最后一位对应的位置是1~n的和,那我们就大胆使用二分进行搜索这位数. #include<iostream> #include<algorithm> #include<cstdio> #incl

HZNU 2019 Summer training 6

A - Infinite Sequence CodeForces - 622A 题意:第一个数是1,接下来是1和2,接下来是1,2, 3,接下来是1,2,3, 4,问第n个数是什么 题解:找出第几轮在找出第几个 #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector>

How to determine which grid cells a line segment passes through?

https://cn.mathworks.com/matlabcentral/answers/230155-how-to-determine-which-grid-cells-a-line-segment-passes-through How to determine which grid cells a line segment passes through? Hi, I apologize if this question has been asked before but I have l

Rollback Segment Configuration &amp; Tips (Doc ID 69464.1)

Rollback Segment Configuration & Tips (Doc ID 69464.1) To Bottom ROLLBACK SEGMENT CONFIGURATION & TIPS ====================================== Good rollback segment configuration is crucial to a well tuned Oracle database. The following should help

2017浙江省赛 E - Seven Segment Display ZOJ - 3962

地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3962 题目: A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix

UNDO segment深入解析

Undo Segment深入解析   在undo自动管理时,设置了undo_retention以后,undo块就存在四种状态. Active:表示正在使用该undo的事务还没有提交或回滚.Inactive:表示该undo上没有活动的事务,该状态的undo可以被其他事务覆盖.Expired:表示该undo持续inactive的时间超过undo_retention所指定的时间.Freed:表示该undo块内容是空的,从来没有被使用过. Undo Retention      After a tran

[LeetCode] Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem