Educational Codeforces Round 7 C. Not Equal on a Segment(思维)
来源:互联网 发布:网游rpg知乎 编辑:程序博客网 时间:2024/05/29 18:12
题意:
给定N,Q≤2×105,N为序列长度,Q次询问
li,ri,xi,查找[li,ri]中≠xi的任意一个下标,无解输出−1
分析:
经典想法了,R[i]:=与a[i]相等的最右的那个下标
R[i]=a[i]==a[i+1] ? R[i+1]:i
询问的时候判断a[l]=x与否,不然就是R[l]+1,看是否在r内
时间复杂度为O(n+q)
由于ai≤106,可以维护106个vector来保存下标
然后二分x这个vector里[li,ri]子集的[l′,r′],如果r′−l′==ri−li,就是−1
不然就再次二分找到那个缺口,就是答案
时间复杂度为O(n+qlog2n)
代码:
//// Created by TaoSama on 2016-02-10// Copyright (c) 2016 TaoSama. All rights reserved.//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 2e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n, q;int a[N], R[N];int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); scanf("%d%d", &n, &q); for(int i = 1; i <= n; ++i) scanf("%d", a + i); for(int i = n; i; --i) if(a[i] == a[i + 1]) R[i] = R[i + 1]; else R[i] = i; while(q--) { int l, r, x; scanf("%d%d%d", &l, &r, &x); if(a[l] != x) printf("%d\n", l); else { if(R[l] + 1 <= r) printf("%d\n", R[l] + 1); else puts("-1"); } } return 0;}
0 0
- Educational Codeforces Round 7 C. Not Equal on a Segment(思维)
- Educational Codeforces Round 7--C. Not Equal on a Segment
- Educational Codeforces Round 7 C. Not Equal on a Segment
- Educational Codeforces Round 7-C. Not Equal on a Segment
- Educational Codeforces Round 7-C. Not Equal on a Segment(模拟)
- Educational Codeforces Round 7 CF622C Not Equal on a Segment
- Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
- CodeForces 622 C. Not Equal on a Segment(水~)
- Codeforces 622C Not Equal on a Segment【思维预处理+二分】
- Codeforces 622 C. Not Equal on a Segment
- CodeForces 622C Not Equal on a Segment
- CodeForces 622C Not Equal on a Segment
- Codeforces 622C Not Equal on a Segment
- CodeForces 622C F - Not Equal on a Segment
- Not Equal on a Segment CodeForces
- Not Equal on a Segment CodeForces
- Not Equal on a Segment CodeForces
- Not Equal on a Segment CodeForces
- centos 初始配置 网卡自启动、SSH、开机启动命令行
- Android应用安全开发之防范无意识的数据泄露
- 跟着猫哥学Golang 14 - error
- JSP的内置对象(9个)直接使用
- POJ 1129 Channel Allocation 图的染色 最大团
- Educational Codeforces Round 7 C. Not Equal on a Segment(思维)
- HTTP与HTTPS握手的那些事
- test1
- Oracle 11g 关闭内存自动管理
- linux下面tomcat挂掉后重新启动的shell脚本
- java学习心得——自动回复功能
- Android之WebView优化之路
- HDU 2846 Repository (字典树)
- 排序笔记_1