H. Milestones

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100114

Description

The longest road of the Fairy Kingdom has n milestones. A long-established tradition defines a specific color for milestones in each region, with a total of m colors in the kingdom. There is a map describing all milestones and their colors. A number of painter teams are responsible for milestone maintenance and painting. Typically, each team is assigned a road section spanning from milestone #l to milestone #r. When optimizing the assignments, the supervisor often has to determine how many different colors it will take to paint all milestones in the section l…r. Example. Suppose there are five milestones #1, #2, #3, #4, #5 to be painted with colors 1, 2, 3, 2, 1, respectively. In this case, only two different paints are necessary for milestones 2…4: color 2 for milestones #2 and #4, and color 3 for milestone #3. Write a program that, given a map, will be able to handle multiple requests of the kind described above.

Input

The first line contains two integers, n and k – the number of milestones and the number of requests, respectively. The second line consists of n integers separated by spaces and defines the sequence of colors for milestones from #1 to #n. The following k lines contain pairs of integers, one pair per line. Each pair consists of two numbers – li and ri – and defines a range of milestones for request i.

Output

The output file should contain k integers separated by line breaks. Result number i should present the result of the i-th request, i. e. the number of colors required to paint all milestones in the road section li…ri.

Sample Input

5 3 1 2 3 2 1 1 5 1 3 2 4

Sample Output

3 3 2

HINT

1 ≤ n ≤ 10 000; 1≤ m ≤ 255; 1 ≤ li ≤ ri ≤ n; 1 ≤ k ≤ 100 000.

题意

求区间内有多少个不同的数

没有修改

题解:

离线维护树状数组就好了

代码:

#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std; typedef long long ll;
typedef pair<int,int> PII;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define FORD(i,s,t) for(int i = (s);i >= (t);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define REPD(i,n) for(int i=(n-1);i>=0;i--)
#define PII pair<int,int>
#define PB push_back
#define ft first
#define sd second
#define lowbit(x) (x&(-x))
#define INF (1<<30)
#define eps (1e-8) const int maxq = ;
const int maxn = ;
int a[maxn],C[maxn],last[];
int ans[maxq];
void init(){
memset(C,,sizeof(C));
memset(last,-,sizeof(last));
}
struct Query{
int l,r;
int idx;
bool operator < (const Query & rhs)const{
return r < rhs.r;
}
}Q[maxq]; void add(int x,int val){
while(x<maxn){
C[x] += val;
x += lowbit(x);
}
}
int sum(int x){
int res = ;
while(x > ){
res += C[x];
x -= lowbit(x);
}
return res;
}
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
while(~scanf("%d",&n)){
init();
int q;
scanf("%d",&q);
FOR(i,,n)scanf("%d",&a[i]);
REP(i,q){
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].idx = i;
}
sort(Q,Q+q);
int pre = ;
REP(i,q){
FOR(j,pre,Q[i].r){
if(last[a[j]]==-){
add(j,);
}else {
add(last[a[j]],-);
add(j,);
}
last[a[j]] = j;
}
ans[Q[i].idx] = sum(Q[i].r)-sum(Q[i].l-);
pre = Q[i].r+;
}
REP(i,q)printf("%d\n",ans[i]);
}
return ;
}

Codeforces Gym 100114 H. Milestones 离线树状数组的更多相关文章

  1. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

  2. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  3. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  4. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

  5. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  6. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

  7. 离线树状数组 hihocoder 1391 Countries

    官方题解: // 离线树状数组 hihocoder 1391 Countries #include <iostream> #include <cstdio> #include ...

  8. 区间的关系的计数 HDU 4638 离线+树状数组

    题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...

  9. BZOJ_2743_[HEOI2012]采花_离线+树状数组

    BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...

随机推荐

  1. getDeclaredMethods()和getMethods()区别

    getDeclaredMethods()          返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共.保护.默认(包)访问和私有方法, ...

  2. 查询MySQL锁等待的语句

    select 'Blocker' role,    p.id,    p.user,    left(p.host, locate(':', p.host) - 1) host,    tx.trx_ ...

  3. C# 如何以参数的形式调用.exe程序

    System.Diagnostics.Process.Start("程序的路径", "参数1 参数2");第一个参数是aaa.exe 的路径,第二个参数是用空格 ...

  4. [原创]C语言利用pcre正则表达式库

    C语言使用正则表达式,可以利用pcre库,这个比较不错的哦. 在使用过程中,利用python进行测试正则表达式是否OK,后发现出现了问题.如下所示: regex.c:11:18: warning: u ...

  5. HDU5790 Prefix 字典树+主席树

    分析:这个题和spoj的d_query是一个题,那个是求一段区间里有多少个不同的数字,这里是统计有多少个不同的前缀 用字典树进行判重,(和查询不同的数字一样)对于每个不同的前缀,只保留它最后一次出现的 ...

  6. codeforces 691E Xor-sequences 矩阵快速幂

    思路:刚开始 n个元素,a[i][j]代表以i开头,j结尾的二元组符合条件的有多少 这是等于长度为2的数量 长度为3的数量为a*a,所以长度为n的数量是a^(k-1) 然后就是矩阵快速幂,然而我并不能 ...

  7. 【Windows核心编程】重载类成员函数new / new[] / delete / delete[]

    // Heap.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...

  8. WebStorm2016.1 破解 激活

    WebStorm2016.1 破解 激活   方法来自 Rover12421 大神. 1.从官网下载WebStorm2016.1安装. 2.下载 破解补丁 并解压,记住路径 3.编辑WebStorm安 ...

  9. Red Hat Enterprise Linux 6.4常用命令

    常用命令: profile文件立即生效:source /etc/profile source /home/kituser/.bash_profile 修改文件夹子文件权限:chmod -R 777 / ...

  10. Android实例-全屏显示程序(XE10+小米2)(无图)

    方法一:选中窗体->BorderStyle改为“None” 方法二: 1.下载第三方控件QAndroid.Shell 2.执行FShell.Execute('su -c mount -o rem ...