Codechef DISTNUM2 Easy Queries node Array segment tree

Source: Internet
Author: User

Description

You is given an array A consisting of N positive integers. Answer Q queries on it of following type:

    • l R k : let S denote the sorted (in increasing order) set of elements of array A with Its indices between L and R. Note that set Scontains distinct elements (i.e. no duplicates).
      You need to find kth number in it. If such a number does not exist, i.e. the S have less than K elements, output-1.

All the indices in the queries is 1-based.

Input

The first line of input contains, separated integers N and Q denoting the number of elements in A, and the number of queries, respectively.

The second line of input contains N space separated integers denoting the array A.

Each of the next Q lines contains five integers ai, bi, ci, di, ki.
We'll generate li, ri indices for this query as follows:

i-1th ansi-1 0th ANS0 li = (aimax (ansi-1, 0)biNri = (cimax (ansi-1, 0 )diNliriliri.
Output

For each query, output the answer to the "query in a". If such a number doesn ' t exist, output -1.

Constraints
    • 1N, Q
    • 1Ai109
    • 0ai, bi, ci, diN
    • 1liriN
    • 1kiN
Example
Input:4 2 1 1 0 3 0 0 3 2 1 3 0 0 3 3Output:2-123Input:10 109 10 6 3 8 4 9 6 4 2 0 9 to 9 1 3 to 8 1 0 to 2 1 7 6 1 2 4 1 3 6 1 6 each 4 1 8 9 1 3 9Output1 10464
Subtasks
    • Subtask #1 (points): Q x N ≤107
    • Subtask #2 (points): ki = 1
    • Subtask #3 (points): ai = 0, ci = 0
    • Subtask #4 (points): Original constraints
Explanation

Example #1:

Query 1. Sorted set of elements: {1, 2}. Second number in this is 2.

Query 2. Sorted set of elements: {1, 2, 3}. Fourth number doesn ' t exist, hence answer is-1.

Query 3. Sorted set of elements: {1, 2}. Second number in this set is 2.

Query 4. Sorted set of elements: {1, 2, 3}. Third number in this set is 3.

Test instructions

A sequence of n for a given length, where each element has a positive integer.

You need to answer Q for a question:

L,r,k: A sequence in which the elements in the sequence a (s) are labeled in ascending order from L to R (only one of the repeating elements is left).

You need to ask for the value of its K-element, and output-1 if it contains less than k elements.

Subscript numbered starting from 1

Exercises

Segment tree, where each node holds a dynamic array with no repeating elements

Query when the two points OK Complexity O (Q*LOGN*LOGN)

#include <iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>using namespacestd;Const intN = 1e5+Ten, M = 2e2+ One, inf = 2e9, mod = 1e9+7; typedefLong Longll;intN, q;ll ar[n],num[n];vector< ll > da[4*N];voidMerges (vector<ll> &a, vector<ll> &b, Vector<ll> &c) {    intLenB =0, Lenc =0;  while(LenB < B.size () && Lenc <c.size ()) {        if(B[lenb] = =C[lenc])            {A.push_back (B[lenb]); LenB+ +, lenc++; }Else {        if(B[lenb] <C[lenc]) {A.push_back (B[lenb++]); } ElseA.push_back (c[lenc++]); }    }     while(LenB <b.size ()) {A.push_back (B[lenb++]); }     while(Lenc <c.size ()) {A.push_back (C[lenc++]); }}voidBuildintKintLintr) {if(r = =l) {da[k].push_back (ar[l]); return ; } build (k<<1, L, (l+r)/2); Build (k<<1|1, (R+L)/2+1, R); Merges (Da[k],da[k<<1],da[k<<1|1]);} ll query (intIintJ,ll x,intKintLintr) {if(I==L&AMP;&AMP;J==R)returnUpper_bound (Da[k].begin (), Da[k].end (), X)-Da[k].begin (); Else {        intMid = (l+r) >>1; if(J<=mid)returnQuery (i,j,x,k<<1, L,mid); Else if(I>mid)returnQuery (i,j,x,k<<1|1, mid+1, R); Else returnQuery (i,mid,x,k<<1, L,mid) +query (mid+1,j,x,k<<1|1, mid+1, R); }}ll Solve (intLintRintk) {intLB =1, RB = n, ans =1;  while(lb<=RB) {        intMid = (LB+RB) >>1; if(Query (L,r,num[mid),1,1, N) >=k) RB = mid-1, ans =mid; ElseLB = mid +1; //cout<<1<<endl;    }    if(Query (L,r,num[ans),1,1, N) <k) {return-1; }    Else returnNum[ans];}intMain () {scanf ("%d%d",&n,&q);  for(intI=1; i<=n;i++) scanf ("%lld", &ar[i]), num[i] =Ar[i]; Sort (num+1, num+n+1); Build (1,1, N); ll pre=0;  for(intI=1; i<=q;i++) {ll a,b,c,d,k; scanf ("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k); intL = (A*max (PRE,0LL) +b)% n +1; intR = (C*max (PRE,0LL) +d)% n +1; printf ("%d\n", pre =solve (l,r,k)); }}

Codechef DISTNUM2 Easy Queries node Array segment tree

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.