Cdoj 1256 Hao Hao Love sports pretreatment/prefix and

Source: Internet
Author: User

Hao Hao Love sports

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

http://acm.uestc.edu.cn/#/problem/show/1256

Description

Hao Hao likes sports

He N days will participate in < Span id= "mathjax-span-188" class= "math" > m A [1, M integer representation)

Roommate has a Q question

Asked Hao Hao Day to the R Day to participate in how many different kinds of sports

Input

Enter two numbersN,M(1≤n≤< Span id= "mathjax-span-219" class= "mn" >2000 ,   1 ≤m≤ 100 );

Enter N number < Span id= "mathjax-span-230" class= "math" >ai indicates that the first day Hao Hao made the ai type of motion;

Enter a number q(1≤q≤6);

InputQLine two numbers per linel ,   r (1≤l≤< Span id= "mathjax-span-267" class= "Mi" >r≤ n );

Output

Altogether Q Line

Each line output a number indicating Hao Hao in the first Day to the R days altogether how many kinds of activities

Sample Input

5 31 2 3 2 231 42 41 5

Sample Output

323
HINT

Test instructions

Exercises

There are two kinds of positive solutions

1.n^2 pretreatment

Since n is only 2000, we can n^2 to preprocess the answer, and then for each inquiry we are O (1) answer
Core code:
for (i=0;i<n;i++) {
memset (F,0,sizeof (f));
x=0;
for (j=i;j<n;j++) {
if (F[a[j]]==0) {f[a[j]]=1; x + +;}
Ans[i][j]=x;
}
}

2. Prefixes and ideas
We are going to maintain sum[i][j], which shows how many times J has appeared in [1,i].
Then for each inquiry, we just sweep the M, Judge Sum[r][i]-sum[l-1][i] is more than 0 is good
The complexity of this preprocessing is O (n), and the complexity of each query is O (m)
So the overall complexity is O (N+QM)

Code:

#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespacestd;intn,m,i,tmp,j,q,l,r,ans,a[2005][ the];intMain () {scanf ("%d%d",&n,&m);  for(i=1; i<=n;++i) {scanf ("%d",&tmp); A[I][TMP]=1;  for(j=1; j<=m;++j) a[i][j]+=a[i-1][j]; } scanf ("%d",&p);  for(i=1; i<=q;++i) {scanf ("%d%d",&l,&R); Ans=0;  for(j=1; j<=m;++j) ans+= (a[r][j]>a[l-1][j]); printf ("%d\n", ans); }    return 0;}

Cdoj 1256 Hao Hao Love sports pretreatment/prefix and

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.