CF#215 DIV2: B. Sereja and Suffixes

Sereja has an array a, consisting of
n integers a1,
a2,
..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote
out m integers l1,?l2,?...,?lm
(1?≤?li?≤?n). For each number
li he wants to know how many distinct numbers are staying on the positions
li,
li?+?1, ...,
n. Formally, he want to find the number of distinct numbers among
ali,?ali?+?1,?...,?an.?

Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each
li.

Input

The first line contains two integers n and
m (1?≤?n,?m?≤?105). The second line contains
n integers a1,
a2,
..., an
(1?≤?ai?≤?105) — the array elements.

Next m lines contain integers
l1,?l2,?...,?lm.
The i-th line contains integer
li
(1?≤?li?≤?n).

Output

Print m lines — on the
i-th line print the answer to the number li.

Sample test(s)

Input

10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10

Output

6
6
6
6
6
5
4
3
2
1

hash解决,因为要查询每个位置之后的不同的数,那么让每个数的位置越靠后越好,寻找每个数的最后一次出现的位置,hash记录,并递推个数。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#include <iostream>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef __int64 LL;
const int MAX = 0x3f3f3f3f;
const int maxn = 100010;
int n, m;
int vis[maxn], a[maxn], b[maxn];
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = n; i >= 1; i--) {
        b[i] = b[i+1];
        if(!vis[ a[i] ]) {
            b[i]++;
            vis[ a[i] ] = 1;
        }
    }
    while(m--) {
        int tmp;
        scanf("%d", &tmp);
        printf("%d\n", b[tmp]);
    }
    return 0;
}



CF#215 DIV2: B. Sereja and Suffixes

时间: 2024-08-26 07:53:45

CF#215 DIV2: B. Sereja and Suffixes的相关文章

CodeForces 368B Sereja and Suffixes

Sereja and Suffixes Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 368B64-bit integer IO format: %I64d      Java class name: (Any) Sereja has an array a, consisting of n integers a1, a2, ..., an. The

CF #261 div2 D. Pashmak and Parmida&amp;#39;s problem (树状数组版)

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence a that consists of n

CF243 DIV2 C Sereja and Swaps(暴力)

题意:给你一个数组,问你交换最多K个数以后,最大子串和为多少; 解题思路:枚举这个数组最大字串和的起点和终点,然后优先队列交换这段里面的小数去换外面的大数,即可求出答案! 解题代码: 1 // File Name: c.cpp 2 // Author: darkdream 3 // Created Time: 2014年04月28日 星期一 19时04分36秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8

CF #261 DIV2 A,B,C,D

A 先判断邻边长度是否相等,不是就无法构成正方形,否则的话位置坐标搞一下 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <bitset> #include <vector> #include <set>

cf#261 div2 解题报告

.....代码没什么可说的,主要是学习各路大神姿势 A题 化简化简水题,都告诉平行坐标轴了,数据还出了对角线,后面两个点坐标给的范围也不错 ........和最优代码相比姿势有点混乱 #include <cstdio> int x[4],y[4]; int abs(int n){ return n<0?-n:n; } int main(){ scanf("%d%d%d%d",x,y,x+1,y+1); int dx=abs(x[0]-x[1]); int dy=abs

CF 368B Sereja and Suffixes(DP?)

题目链接:http://codeforces.com/problemset/problem/368/B 题目: Sereja has an array a, consisting of n integers a1, a2, ..., an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..

cf 215 C. Crosses yy题

链接:http://codeforces.com/problemset/problem/215/C C. Crosses time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There is a board with a grid consisting of n rows and m columns, the rows are n

cf 301 div2

A - Combination Lock 题目大意:给有n个(0-9)环圈密码锁,数串 s1->s2最少移动次数: 题目分析: 简单模拟: 代码: const int N=100007; char s1[N],s2[N]; int main() { int n; while(scanf("%d",&n)==1) { int ans=0; scanf("%s%s",s1+1,s2+1); for(int i=1;i<=n;i++) { int a=

CF R631 div2 1330 E Drazil Likes Heap

LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了一下就没了. 但是到E就只剩下30min(都怪A B浪费我过多时间. 观察题目中给的一个程序 其维护了一个大根堆且这个程序意思是一个函数 这个函数是指对于这个大根堆上的一个非空节点来说每次会将这个值给删掉继承最大的儿子值 然后递归值最大的儿子值得某个节点没有一个非空儿子(那么这个点的值被删掉为0.