The 5000ms time limit is quite long. The algorithm is DP.
The idea is to find the results of ANS[1..N], and then query is easy to do. The question is how DP?
Consider:
1 1 2 3 4 4 5w=1:7, 7 = 1 * 7
w=2:10,10 = | {1,1}|+| {1,2}|+| {2,3}|+| {3,4}|+| {4,4}|+| {4,5}|=1+2+2+2+1+2=10
W=3:12, 12 = | {1, 1, 2}|+| {1, 2, 3}|+| {2, 3, 4}|+| {3, 4, 5}|+| {4, 4, 5}|
...
To observe how w=3 is calculated on the basis of w=2, the first need to subtract | {4,5}|, and then consider whether 2,3,4,5,5 (the last number of each collection) is distinct.
Therefore, O (n^2) is solvable. However, the range of n is 1e6 and must be timed out.
So, simplify the idea and divide it into three parts.
1. ans[n-1];
2. In the number of post-K ([1..N]), the number of distinct can be obtained within O (n).
3. Consider a[w. N] If the number is increased by 1, you can think of a different idea.
Consider 1 1 2 3 4 4 5 in 3, because 3 is not previously present, so w=[1..4], this 3 will provide an increment of 1.
Consider 1 3 2, 4 4 5, because there is a 3, two spacing of 2, so w=[1..2], this 3 will provide an increment of 1.
A delta array is visible, and two O (n) loops can be determined for W=[1..N], what is the Delta Delta?
At the end, you can synthesize it.
1 /*4455*/2#include <iostream>3#include <string>4#include <map>5#include <queue>6#include <Set>7#include <stack>8#include <vector>9#include <deque>Ten#include <algorithm> One#include <cstdio> A#include <cmath> -#include <ctime> -#include <cstring> the#include <climits> -#include <cctype> -#include <cassert> -#include <functional> +#include <iterator> -#include <iomanip> + using namespacestd; A //#pragma COMMENT (linker, "/stack:102400000,1024000") at - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> - #definePII pair<int,int> in #defineVpii vector<pair<int,int> > - #defineRep (I, A, n) for (int i=a;i<n;++i) to #definePer (i, a, n) for (int i=n-1;i>=a;--i) + #defineCLR Clear - #definePB Push_back the #defineMP Make_pair * #defineFIR First $ #defineSEC SecondPanax Notoginseng #defineAll (x) (x). Begin (), (x). End () - #defineSZ (x) ((int) (x). Size ()) the #defineLson L, Mid, rt<<1 + #defineRson mid+1, R, rt<<1|1 A the Const intMAXN = 1e6+5; + intA[MAXN], B[MAXN]; - intPRE[MAXN]; $ __int64 DELTA[MAXN], ANS[MAXN]; $ BOOLMARK[MAXN]; - - intMain () { theIos::sync_with_stdio (false); - #ifndef Online_judgeWuyiFreopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout); - #endif Wu - intN, q, x; About intl, TMP; $ intI, J, K; - - while(SCANF ("%d", &n)!=eof &&N) { - for(i=1; i<=n; ++i) { Ascanf"%d", &a[i]); +++A[i]; the } -memset (Delta,0,sizeof(delta)); $memset (PRE,0,sizeof(pre)); thememset (Mark,0,sizeof(Mark)); the the //handle last I number the for(i=n,j=1; I>0; --i,++j) { - if(Mark[a[i]]) { inB[J] = b[j-1]; the}Else { theB[J] = b[j-1]+1; AboutMark[a[i]] =true; the } the } the //Handle Pre + for(i=1; i<=n; ++i) { -L = i-Pre[a[i]]; the //[1, l]++Bayidelta[1]++; thedelta[l+1]--; thePre[a[i]] =i; - } -ans[1] =N; the for(i=2; i<=n; ++i) { theDelta[i] + = delta[i-1]; theAns[i] = ans[i-1] + delta[i]-b[i-1]; the } - thescanf"%d", &q); the while(q--) { thescanf"%d", &x);94printf"%i64d\n", ans[x]); the } the } the 98 #ifndef Online_judge Aboutprintf"Time =%d.\n", (int) clock ()); - #endif101 102 return 0;103}
"Hdoj" 4455 substrings