COJ 0985 WZJ data structure (negative 15) (different number of restricted regions)

Source: Internet
Author: User

Portal: http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=955

Question Description:

Chx have a question to ask you. Give you a length of N of number A, please find two position l,r, so that a[l], a[l+1] 、......、 a[r] No duplicates, output the maximum value of r-l+1.

The above is the secondary school League plus test a question. Wzj think this problem is too water, changed the topic:

Wzj have a question to ask you. To give you a sequence of length n, you have to answer the M-th question. Each question gives you two positive integer QL,QR. Please find two positions L, R (QL<=L<=R<=QR) so that there are no duplicates in a[l], a[l+1] 、......、 A[r], and the maximum value of the output r-l+1.

In some people's Spit groove (Not Me (⊙▽⊙)), the subject is not mandatory online. Watch your range, good luck!

Input:

Enter the first behavior of two positive integer n,m.
Enter the second behavior of n integer ai.
Next m lines are two positive integers per line ql,qr.

Output:

For each issue, the maximum value of the output r-l+1.

Input Example:

5 3
1 2 1) 3 4
1 3
2 4
2 5

Output Example:

2
3
4

Other Notes:

1<=n<=200000
1<=m<=500000
1<=ql,qr<=n
-10^9<=ai<=10^9

The puzzle (fortunately there is no force online AH (⊙▽⊙)) divided into dp+rmq.

We first deal with the longest range of different numbers, each time to limit it will be cut off, for the right "this interval" we pre-processing cycle section count and then use RMQ to fix the maximum, for the left interval we find the number, and then directly using L-QL out of the interval (this interval must not repeat the number of spicy O (* ̄3 ̄) o)

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6 #defineREP (i, S, N) for (int i = s; i <= n; i + +)7 #defineRAP (i, N, s) for (int i = n; i >= s; i--)8 using namespacestd;9 Const intMAXN =200000+Ten;Ten Const intMaxhash =871199; One intN, Q, d[maxn][ -], LOG[MAXN], DP[MAXN], A[MAXN], LAST[MAXN]; A namespacehash{ -     intFch[maxhash], NEXT[MAXN], VAL[MAXN], MS =0; -     voidHash_init () {memset (FCH,-1,sizeof(FCH)); } the     intFind_insert (intv) { -         intid = v%Maxhash; -         if(ID <0) id + =Maxhash; -          for(inti = Fch[id]; I! =-1; i = Next[i])if(Val[i] = = v)returni; +NEXT[MS] = Fch[id]; Val[ms] = v;returnFch[id] = ms + +; -     } +}using namespaceHASH; A voidRMQ () { atlog[0] = -1; REP (I,1, N) d[i][0] = I-dp[i] +1, log[i] = Log[i >>1] +1;//This cycle section starts counting -      for(intj =1; (1<< j) <= N; J + +) -          for(inti =1; i + (1<< j)-1<= N; i + +) -D[I][J] = max (D[i][j-1], D[i + (1<< (J-1))][j-1]); -     return ; - } inInlinevoidReadint&x) { -x =0;intsig =1;CharCH =GetChar (); to      while(!isdigit (CH)) {if(ch = ='-') sig =-1; CH =GetChar ();} +      while(IsDigit (ch)) x =Ten* x + CH-'0', ch =GetChar (); -X *= sig;return ; the } *InlinevoidWriteintx) { $     intLen =0, buf[Ten];Panax Notoginseng      while(x) buf[++ len] = x%Ten, x/=Ten; -      for(inti = len; I I--) Putchar (Buf[i] +'0'); thePutchar ('\ n');return ; + } A voidinit () { the hash_init (); + read (n); read (Q); -REP (I,1, n) read (A[i]), a[i] = Find_insert (A[i]), dp[i] = max (Dp[i-1], Last[a[i]] +1), last[a[i]] =i; $     //update the tail of the cycle section, transfer very witty ah I want to curse. Finally, don't forget to update your location (⊙▽⊙) $ RMQ (); -     return ; - } the intQueryintLintR) { -     intK = log[r-l +1];Wuyi     returnMax (D[l][k], D[r-(1<< k) +1][k]); the } - voidWork () { Wu     intQL, QR; -      while(Q--){ About read (QL); Read (QR); $         intL = QL, R = QR +1, M; -          while(L +1< R) {//What the hell are these two points? Ε=ε=ε=┏ (゜ロ゜;) ┛ -M = L + R >>1; -             if(Dp[m] < QL) L = M;//two points to find a follow -up link A             ElseR = M;//two cents in the wrong book? TAT +         } thePrint (max (L-QL +1, Query (L +1, QR)));//split into two segments, the previous segment of this cycle section and the last section of the previous loop (ˉ"ˉ) -     } $     return ; the } the voidprint () { the  the     return ; - } in intMain () { the init (); the Work (); About print (); the     return 0; the}

COJ 0985 WZJ data structure (negative 15) (different number of restricted regions)

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.