Educational Codeforces Round 7-C. Not Equal on a Segment(模拟)
2016-02-11 22:19
465 查看
C. Not Equal on a Segment
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given array a with n integers
and m queries. The i-th
query is given with three integers li, ri, xi.
For the i-th query find any position pi (li ≤ pi ≤ ri)
so that api ≠ xi.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105)
— the number of elements in a and the number of queries.
The second line contains n integers ai (1 ≤ ai ≤ 106)
— the elements of the array a.
Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106)
— the parameters of the i-th query.
Output
Print m lines. On the i-th
line print integer pi —
the position of any number not equal to xi in
segment [li, ri] or
the value - 1 if there is no such number.
Sample test(s)
input
output
思路:
一开始这题想到了线段树但是却无从下手,想了好久也不会做,之后看了看别人的思路,说直接将相同的数统计一下下标就行了,最后发现原来是有一个规律在里面,因为这题是找不同的位置所以只要将相同的位置预处理一下,有相同的直接跳过,这样之后基本就是O(1)的查询时间了,因为相同的我都已经跳过了,思维太差了,想不到这个地方可以将时间复杂度降下来。
AC代码:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given array a with n integers
and m queries. The i-th
query is given with three integers li, ri, xi.
For the i-th query find any position pi (li ≤ pi ≤ ri)
so that api ≠ xi.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 2·105)
— the number of elements in a and the number of queries.
The second line contains n integers ai (1 ≤ ai ≤ 106)
— the elements of the array a.
Each of the next m lines contains three integers li, ri, xi (1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 106)
— the parameters of the i-th query.
Output
Print m lines. On the i-th
line print integer pi —
the position of any number not equal to xi in
segment [li, ri] or
the value - 1 if there is no such number.
Sample test(s)
input
6 4 1 2 1 1 3 5 1 4 1 2 6 2 3 4 1 3 4 2
output
2 6 -1 4
思路:
一开始这题想到了线段树但是却无从下手,想了好久也不会做,之后看了看别人的思路,说直接将相同的数统计一下下标就行了,最后发现原来是有一个规律在里面,因为这题是找不同的位置所以只要将相同的位置预处理一下,有相同的直接跳过,这样之后基本就是O(1)的查询时间了,因为相同的我都已经跳过了,思维太差了,想不到这个地方可以将时间复杂度降下来。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cstdio> #include<cmath> #include<ctime> #include<cstdlib> #include<queue> #include<vector> #include<set> using namespace std; const int T=200100; #define inf 0x3f3f3f3fL #define mod 1000000000 typedef long long ll; typedef unsigned long long ULL; int v[T],num[T]; int main() { #ifdef zsc freopen("input.txt","r",stdin); #endif int n,m,i,j,k; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;++i){ scanf("%d",&v[i]); if(v[i]==v[i-1]){ num[i] = num[i-1]; } else { num[i] = i; } } int x,y,w; while(m--) { bool flag = false; scanf("%d%d%d",&x,&y,&w); for(i=y;i>=x;--i){ if(v[i]==w){//相同直接跳到最后面 i = num[i]; } else {//找到不相同,结束 flag = true; break; } } if(!flag)i=-1; printf("%d\n",i); } } return 0; }
相关文章推荐
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- PHP模拟asp.net的StringBuilder类实现方法
- C#使用ImitateLogin模拟登录百度
- javascript用层模拟可移动的小窗口
- 自编jQuery插件实现模拟alert和confirm
- JS模拟按钮点击功能的方法
- PHP模拟asp中response类实现方法
- 模拟Spring的简单实现
- javascript 模拟点击广告
- JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
- php实现模拟post请求用法实例
- JavaScript实现MIPS乘法模拟的方法
- 模拟xcopy的函数
- JS模拟实现Select效果代码
- php模拟服务器实现autoindex效果的方法
- C# SendInput 模拟鼠标操作的实现方法
- PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法
- js实现模拟银行卡账号输入显示效果
- JS模拟并美化的表单控件完整实例