Topic links
n the number of M queries, each query outputs the nearest distance of any two of the same number in a given interval.
First read the inquiry in, and then press R from small to large sorting, n number in order to insert, and map statistics before the occurrence, if so, update the line segment tree.
If the current I equals the R of an inquiry, then query, see the code specifically.
1#include <iostream>2#include <vector>3#include <cstdio>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <map>8#include <Set>9#include <string>Ten#include <queue> One#include <stack> A#include <bitset> - using namespacestd; - #definePB (x) push_back (x) the #definell Long Long - #defineMK (x, y) make_pair (x, y) - #defineLson L, M, rt<<1 - #defineMem (a) memset (a, 0, sizeof (a)) + #defineRson m+1, R, rt<<1|1 - #defineMem1 (a) memset (a,-1, sizeof (a)) + #defineMEM2 (a) memset (a, 0x3f, sizeof (a)) A #defineRep (i, N, a) for (int i = A; i<n; i++) at #defineFi first - #defineSe Second -typedef pair<int,int>PLL; - Const DoublePI = ACOs (-1.0); - Const DoubleEPS = 1e-8; - Const intMoD = 1e9+7; in Const intINF =1061109567; - Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} }; to Const intMAXN = 5e5+5; + intminn[maxn<<2], A[MAXN], Ans1[maxn], ans; - structnode the { * intL, R, id; $ BOOL operator< (Node A)ConstPanax Notoginseng { - if(r = =A.R) theid<a.id; + returnr<A.R; A } the }Q[MAXN]; +Map <int,int>MP; - voidPushup (intRT) { $MINN[RT] = min (minn[rt<<1], minn[rt<<1|1]); $ } - voidUpdateintPintValintLintRintRT) { - if(L = =r) { theMINN[RT] =Val; - return ;Wuyi } the intm = l+r>>1; - if(p<=m) Wu Update (P, Val, Lson); - Else About Update (P, Val, Rson); $ pushup (RT); - } - voidQueryintLintRintLintRintRT) { - if(l<=l&&r>=r) { AAns =min (ans, minn[rt]); + return ; the } - intm = l+r>>1; $ if(l<=m) the query (L, R, Lson); the if(r>m) the query (L, R, Rson); the } - intMain () in { the intN, M; theCin>>n>>m; About for(inti =1; i<=n; i++) thescanf"%d", &a[i]); the for(inti =0; i<m; i++) { thescanf"%d%d", &Q[I].L, &Q[I].R); +Q[i].id =i; - } theSort (q, q+m);Bayi intpos =0; the MEM2 (Minn); the for(inti =1; i<=n; i++) { - if(Mp[a[i]]) { -Update (Mp[a[i]], I-mp[a[i]],1N1); the } theMp[a[i]] =i; the while(i = =Q[POS].R) { theAns =inf; -Query (Q[POS].L, I,1N1); the if(ans = =inf) theAns =-1; theAns1[q[pos].id] =ans;94pos++; the } the } the for(inti =0; i<m; i++)98cout<<ans1[i]<<" "; About return 0; -}
Codeforces 522D. Closest Equals line tree + offline