Not Equal on a Segment_优化_提问_打表

Not Equal on a Segment

TimeLimit:1000MS  MemoryLimit:256MB
64-bit integer IO format: %I64d
Problem Description

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.

SampleInput
6 4
1 2 1 1 3 5
1 4 1
2 6 2
3 4 1
3 4 2
SampleOutput
2
6
-1
4

题意大意为:给的序列中, 提问给一个左右范围, 给出一个数字, 给出不是这个数 的范围中的任意下标。

解决的办法为, 先判断边界的数字是否相同, 若不同, 则返回这个边界。否则, 返回预处理的数。( 打表, 进行判断。 )

代码:
#include <cstdio>
using namespace std;
const int all = (2e6)+5;
int numa[ all ], numb[ all ];
int n, m, l, r, x;
int main(void)
{
    scanf( "%d%d", &n, &m );
    for( int i=1; i <= n; ++ i ){
        scanf( "%d", numa+i );
        // 与前一个数进行判断, 若不同, 加前一个数的下标即可。 注意的是, 下标为 0 的值 为 0, 输入数据范围为 大于 0
        if( numa[i] != numa[i-1] ){
            numb[i] = i-1;
        }
        else{
        // 若相同, 则存 前一个数所计算的数
            numb[i] = numb[i-1];
        }
    }

    while( m -- ){
        scanf( "%d%d%d", &l, &r, &x );
        if( x != numa[r] ){
            printf( "%d\n", r );
        }
        // 边界相同, 查表
        else if( numb[r] >= l ){
            printf( "%d\n", numb[r] );
        }
        else{
            puts( "-1" );
        }
    }
    return 0;
}
View Code

 



转载于:https://www.cnblogs.com/seana/p/5238752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值