Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3599 Accepted Submission(s): 1105
Problem Description
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct elements’ number in all substrings of length w. For example, the array is { 1 1 2 3 4 4 5 } When w = 3, there are five substrings of length 3. They are (1,1,2),(1,2,3),(2,3,4),(3,4,4),(4,4,5)
The distinct elements’ number of those five substrings are 2,3,3,2,2.
So the sum of the distinct elements’ number should be 2+3+3+2+2 = 12
Input
There are several test cases.
Each test case starts with a positive integer n, the array length. The next line consists of n integers a1,a2…an, representing the elements of the array.
Then there is a line with an integer Q, the number of queries. At last Q lines follow, each contains one integer w, the substring length of query. The input data ends with n = 0 For all cases, 0<w<=n<=106, 0<=Q<=104, 0<= a1,a2…an <=106
Output
For each test case, your program should output exactly Q lines, the sum of the distinct number in all substrings of length w for each query.
Sample Input
7 1 1 2 3 4 4 5 3 1 2 3 0
Sample Output
7 10 12
Source
2012 Asia Hangzhou Regional Contest
Recommend
We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
const int maxn =1e6;
const int ub=maxn+5;
const int mod=1e9+7;
/*
题目大意:给定一个序列,
和数个询问,要求计算,
所有连续长度为n的序列的不同数字的个数的和。
这道题思维很好,
首先思考DP,dp[i]表示
长度为i时全部的和,明显dp[1]=n;
然后思考递推,dp[i+1]和dp[i]的关系,
不难感觉到肯定有个递增量,这个递增量是从第i位开始的,
所以从i到n每个位置都可能有个递增量,
这个递增量取决于上一次出现的数和其位置的位移差与i的大小关系,
如果上一次出现 的位置差大于i,那么这个数在序列递增时是会整体贡献的,
小于i则无贡献。
但这样会有部分的重复,我们详解下这个过程,
比如序列:
1,1,2,3,4,5,6;
按照上面的思路,如果长度为3,
那么2,3,。。。6都会做出贡献,
原本长度为2时会有五段序列,三的时候只会有四段序列,
所以减去的就是上次计算多出的那段序列,那段序列在末尾部分。
就是说最后两个数,5,6,在上一次时是被计算在内的,而这次应该要减去。
减去的值就是末尾部分长度为i-1的不同数的个数。
详见代码,思路出来了代码大部分都是套路。
*/
int dat[ub];
///树状数组结构
int tree[ub];
int lowbit(int x){return x&(-x);}
void add(int x,int d){for(;x<=maxn;tree[x]+=d,x+=lowbit(x));}
int sum(int x){int ret=0;for(;x>0;ret+=tree[x],x-=lowbit(x));return ret;}
///下一个数出现的位置,预处理
int nxt[ub],show[ub];
ll dp[ub];
///数据域
int n,q,x,c;
int main()
{
while(scanf("%d",&n)==1&&n)
{
for(int i=1;i<=n;i++) scanf("%d",&dat[i]);
memset(show,0,sizeof(show));
memset(tree,0,sizeof(tree));///本人傻逼的地方,,,树状数组忘了初始化。。。。
for(int i=1;i<=n;i++)
{
nxt[i]=i-show[dat[i]];
show[dat[i]]=i;
add(nxt[i],1);
}
memset(dp,0,sizeof(dp));
memset(show,0,sizeof(show));
dp[1]=n;
c=1;
show[dat[n]]=1;
for(int i=2;i<=n;i++)
{
int v=sum(maxn)-sum(i-1);
dp[i]=dp[i-1]+v-c;
if(show[dat[n-i+1]]==0)
{
show[dat[n-i+1]]=1;
c++;
}
}
scanf("%d",&q);
for(int i=0;i<q;i++)
{
scanf("%d",&x);
printf("%lld\n",dp[x]);
}
}
return 0;
}