Codeforces Gym 100114 H. Milestones 离线树状数组
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
Output
Sample Input
5 3 1 2 3 2 1 1 5 1 3 2 4
Sample Output
HINT
题意
求区间内有多少个不同的数
没有修改
题解:
离线维护树状数组就好了
代码:
#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 离线树状数组的更多相关文章
- Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp
Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- 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) ,问在每个区间里所有 ...
- 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 ...
- 离线树状数组 hihocoder 1391 Countries
官方题解: // 离线树状数组 hihocoder 1391 Countries #include <iostream> #include <cstdio> #include ...
- 区间的关系的计数 HDU 4638 离线+树状数组
题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...
- BZOJ_2743_[HEOI2012]采花_离线+树状数组
BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...
随机推荐
- JOB的创建,定时,执行
--建表 create table test_job(para_date date); commit; insert into test_job values(sysdate); commit; ...
- 【转】STL空间配置器
STL空间配置器(allocator)在所有容器内部默默工作,负责空间的配置和回收.STL标准为空间配置器定义了标准接口(可见<STL源码剖析>P43).而具体实现细节则由各编译器实现版本 ...
- ASP.NET缓存策略经验谈
要提升ASP.NET应用程序的性能,最简单.最有效的方式就是使用内建的缓存引擎.虽然也能构建自己的缓存,但由于缓存引擎已提供了如此多的功能,所以完全不必如此麻烦.在很大程度上,ASP.NET开发者在W ...
- C# DataGridView的列对象属性探讨 (未完待续)
比较难的几个属性的释义[1]:
- Android 获取本机WIFI及3G网络IP
获取本机WIFIprivate String getLocalIpAddress() { WifiManager wifiManager = (WifiManager) getSystemServic ...
- Android性能调优
本文主要分享自己在appstore项目中的性能调优点,包括同步改异步.缓存.Layout优化.数据库优化.算法优化.延迟执行等.一.性能瓶颈点整个页面主要由6个Page的ViewPager,每个Pag ...
- 【原】Mongodb相关资料
Mongodb与关系型数据库对比 Mongodb与关系型数据库对比 由于之前主要接触的是关系型数据库,所以主要将Mongodb与关系型数据库进行对比:主要从术语.Server与Client.数据定义语 ...
- 利用Spring.Net技术打造可切换的分布式缓存读写类
利用Spring.Net技术打造可切换的Memcached分布式缓存读写类 Memcached是一个高性能的分布式内存对象缓存系统,因为工作在内存,读写速率比数据库高的不是一般的多,和Radis一样具 ...
- Orchard源码分析(1):插件式的支持——模块和主题
在Orchard,模块和主题都是可以插拔式的,在源码处理时,用类型(参考:DefaultExtensionTypes)区分,都没太大的本质区别,以下都称做模块. 插件的支持,实现分以下几步: 搜集模块 ...
- 小C的填数游戏
题意: 给出一张n个点的无向图 i连向i-1和i-2 边权为wij 有两个点权ai和bi ai为0或1 在给m个操作 1.将ai异或1 2.将区间x到y的点都填上一个数ci 使得Σ(bi*(ai^ci ...