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. 【转】ios app 应用内购买配置完全指南

    转自:http://blog.sina.com.cn/s/blog_4b55f6860100sbfb.html 第一印象觉得In-App Purchase(简称IAP)非常简单.Apple提供的大量文 ...

  2. request的用法

    Request从几个集合取数据是有顺序的,从前到后的顺序依次是 QueryString,Form,最后是ServerVariables.Request对象按照这样的顺序依次搜索这几个集合中的变量,如果 ...

  3. Windows环境自动获取AWR报告

    1.双击awr.cmd,通过cmd窗口运行awr.sql cmd.exe /c sqlplus lcam_1230/zcpzg1z_1230@54_orcl @awr.sql awr.cmd 2.aw ...

  4. (原创)LAMP教程4-用VirtualBox安装64位的centos6.4

    (原创)LAMP教程4-用VirtualBox安装64位的centos6.4 好的,今天就要开始正式的讲一些有营养的东西了,是的,没有错就是讲如何用VirtualBox安装64位的centos6.4 ...

  5. Codeforces 167B Wizards and Huge Prize(概率dp)

    题意: n个人,开始有一个容量为k得背包,击败一个人背包可以获得一定容量或得到一个财富(放入背包内),给出击败每个人的概率,求至少击败l个人,且背包容量大于获得的总财富值的概率 分析: 状态好确定,d ...

  6. "_ITERATOR_DEBUG_LEVEL"的不匹配项: 值"0"不匹配值"2"

    error: 1>vtkCommon.lib(vtkDebugLeaksManager.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项 ...

  7. 对Spring的理解

    1.Spring实现了工厂模式的工厂类,这个类名为BeanFactory实际上是一个接口,在程序中通常BeanFactory的子类ApplicationContext.Spring相当于一个大的工厂类 ...

  8. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  9. 【Hadoop学习】Super用户以其他用户的名义执行操作

    Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146410.html 简介 ...

  10. 从根源上解析 Java volatile 关键字的实现

    1.解析概览 内存模型的相关概念 并发编程中的三个概念 Java内存模型 深入剖析Volatile关键字 使用volatile关键字的场景 2.内存模型的相关概念 缓存一致性问题.通常称这种被多个线程 ...