Hdu4455Substrings dp,

Source: Internet
Author: User

Hdu4455Substrings dp,
// Dp [I + 1] = dp [I] + dis [I]-c [I];
// Dp [I] indicates the sum of different numbers of substrings whose lengths are I.
// Dis [I] indicates that the shortest distance of all vertices prior to them is greater than or equal to the sum of I.
// C [I] indicates the sum of different numbers of the last subsequence with the length of I
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Const int maxn = 1000010;
_ Int64 dp [maxn];
Int a [maxn];
Int dis [maxn];
Int last [maxn];
Int Hash [maxn];
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int N; int Q;
While (scanf ("% d", & N)
{
Memset (Hash, 0, sizeof (Hash ));
Memset (dis, 0, sizeof (dis ));
For (int I = 1; I <= N; I ++)
{
Scanf ("% d", & a [I]);
If (Hash [a [I])
Dis [I-Hash [a [I] ++;
Else
Dis [I] ++; // For a number that is not the same as before, it is assumed that the number of vertices with the same number is 0.
Hash [a [I] = I;
}
Memset (Hash, 0, sizeof (Hash ));
For (int I = N; I> 0; I --)
{
Dis [I] + = dis [I + 1];
Last [I] = last [I + 1];
If (! Hash [a [I])
Last [I] ++;
Hash [a [I] = 1;
}
Dp [1] = N;
For (int I = 2; I <= N; I ++)
Dp [I] = dp [I-1] + (_ int64) dis [I]-(_ int64) last [N-I + 2];
Scanf ("% d", & Q );
While (Q --)
{
Int query;
Scanf ("% d", & query );
Printf ("% I64d \ n", dp [query]);
}
}
Return 0;
}





















































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.