Test instructions: Give a string of numbers, q query, each query length is W of all substrings of the number of different numbers of how much.
Solution: First preprocessing d[i] is: the left of each value and its position of equal value and the distance of its position, if the left is not the same as he, set as n+8 (as Infinity).
Consider the answer of the known w=k, push w = k+1, then each interval will be increased by one number, that is, after the n-k number will be increased to these intervals, it is easy to know, if the interval has that number, then the number will not add 1, that is d[i] > K, the results + +, that is, after the query n-k number d[i The number of > K is the number to be added, and then the last side of the loss of an interval, the loss is not to increase the number of the interval, with the increase of W, the interval will stretch to the left, so record how many different numbers of the interval can be. Find out how many d[i]>k in the interval can use a tree array to first ask how many D[i]<=k (Getsum (k)), and then the interval length is reduced.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineLLL __int64using namespacestd;#defineN 1000107intc[n],n,d[n],last,vis[n],a[n],pos[n],l[n];lll Sum[n];intLowbit (intx) {returnx&-x;}voidModifyintXintval) { while(x <= n+Ten) {C[x]+=Val; X+=lowbit (x); }}intGetsum (intx) { intres =0; while(X >0) {res+=C[x]; X-=lowbit (x); } returnRes;}intMain () {intq,i,j,w; while(SCANF ("%d", &n)!=eof &&N) { for(i=1; i<=n;i++) scanf ("%d",&A[i]); memset (POS,0,sizeof(POS)); for(i=1; i<=n;i++) { intx =A[i]; D[i]= IPos[x]; if(D[i] = = i) D[i] = n+8; POS[X]=i; } memset (c,0,sizeof(c)); memset (Vis,0,sizeof(VIS)); for(i=2; i<=n;i++) Modify (D[i],1); sum[1] =N; Last=1; Vis[a[n]]=1; for(i=2; i<=n;i++) {Sum[i]= sum[i-1]+ (n-i+1-getsum (I-1))-Last ; Modify (D[i],-1); if(!vis[a[n-i+1]]) {vis[a[n-i+1]] =1; Last++; }} scanf ("%d",&q); while(q--) {scanf ("%d",&W); printf ("%i64d\n", Sum[w]); } } return 0;}View Code
HDU 4455 Substrings--recursive + Tree array optimization