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

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 = 200011;
const int maxn = 30011;
int a[maxn],C[maxn],last[1000011];
int ans[maxq];
void init(){
    memset(C,0,sizeof(C));
    memset(last,-1,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 = 0;
    while(x > 0){
        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,1,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 = 1;
        REP(i,q){
            FOR(j,pre,Q[i].r){
                if(last[a[j]]==-1){
                    add(j,1);
                }else {
                    add(last[a[j]],-1);
                    add(j,1);
                }
                last[a[j]] = j;
            }
            ans[Q[i].idx] = sum(Q[i].r)-sum(Q[i].l-1);
            pre = Q[i].r+1;
        }
        REP(i,q)printf("%d\n",ans[i]);
    }
    return 0;
}
时间: 2024-10-26 02:20:48

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

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

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

http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的性质,在一个区间中,我们如果把所有数字都异或的话,可以发现最后偶数次的数字异或后都变成了0,只剩下了奇数次的数字异或. 举个例子,{1,2,3,2,3,5} 异或和是1^2^3^2^3^5=1^5 因为最后要计算偶数次数字的异或和,那么最后我们只需要再异或上该区间内所有不同数字即可. 那么我们可以先

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

题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了,23333,反正都是定义右区间的. 这题的关键难度就是如何定义id是连续的呢.我们每次往区间里面放一个数值以后都要add(pos, 1),就是把pos~n的所有的关系都+1.然后如果说在pos之前就出现id-1,就要add(pos[id-1], -1)(同理id+1也是这样),这样子表示从pos[

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

TOJ 4105 Lines Counting(离线树状数组)

4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Runs: 47 On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two interval

离线树状数组 hihocoder 1391 Countries

官方题解: 1 // 离线树状数组 hihocoder 1391 Countries 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 using namespace s

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

BZOJ 1227 [SDOI2009] 虔诚的墓主人 离线+树状数组+离散化

鸣谢:140142耐心讲解缕清了我的思路 题意:由于调这道题调的头昏脑涨,所以题意自己搜吧,懒得说. 方法:离线+树状数组+离散化 解析:首先深表本蒟蒻对出题人的敬(bi)意(shi).这道题简直丧心病狂,看完题后大脑一片空白,整个人都不好了,刚开始的思路是什么呢?暴力思想枚举每个墓碑,然后计算每个墓碑的虔诚度,然后再来统计.不过看看数据范围呢?10^9*10^9的矩阵,最多才10^5个树,光枚举就已经超时了,所以肯定不行.(不过要是考试真没思路我就那么搞了- -!) 然后也想到来枚举墓碑,不过

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 Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有出现偶数次的数异或的值. 思路:容易想到,把区间内的所有的数都异或得到的是出现奇数次的数的值,然后再异或该区间内的所有出现过的数(每个数只统计一次),得到的ans了. 第一个问题:得到询问区间的