cf622c not Equal on a Segment

Source: Internet
Author: User

Tag: RET not str color PRE position return exists turn

Topic Links:

Http://codeforces.com/problemset/problem/622/C

Main topic:

Given a sequence of n (n not more than 200000), there is m (m not more than 200000) asking, and I ask if there is a number in an interval [Li,ri] that is not equal to a given value x. If present, the position of the number is output, otherwise output-1.

Problem Solving Ideas:

Preprocess an array p[n]. P[i] represents the position of the number from position I to the left until position 0, the first one that is not equal to a[i]. Recursion can be implemented with the complexity of O (n). Specifically, first make p[0]=-1, then recursion from left to right, if a[i-1]! = a[i], then p[i] = i-1, otherwise p[i] = p[i-1].

First check a[r] is equal to X when querying. If not equal then find a solution r; otherwise check p[r].

Code:

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 inta[1000005];6 intp[1000005];7 intN, T, L, R, X;8 voidInit ()9 {Tenp[0] = -1; One      for(inti =1; I < n; i++) A     { -         if(A[i]! = a[i-1]) -P[i] = i-1; the         Else -P[i] = p[i-1]; -     } - } + intMain () - { +CIN >> N >>T; A      for(inti =0; I < n; i++) at     { -scanf"%d", &a[i]); -     } - init (); -      while(t--) -     { inscanf" %d%d%d", &l, &r, &x); -l--; tor--; +         if(A[r]! =x) -         { theprintf"%d\n", R +1); *         } $         ElsePanax Notoginseng         { -             if(P[r]! =-1&& P[r] >=l) the             { +printf"%d\n", P[r] +1); A             } the             Else +             { -Puts"-1"); $             } $         } -     } -     return 0; the}

cf622c not Equal on a Segment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.