DLUTOJ #1394 Magic Questions

传送门

Time Limit: 3 Sec  Memory Limit: 128 MB

Description

Alice likes playing games. So she will take part in the movements of M within N days, and each game is represented in an integer between 1 and M. Roommates have Q magic questions: How many different kinds of games does Alice participate between Lth day and Rth day(including Lth day and Rth day)?

Input

You will be given a number of cases; each case contains blocks of several lines. The first line contains 2 numbers of N and M. The second line contains N numbers implying the game numbers that Alice take part in within N days. The third line contains a number of Q. Then Q lines is entered. Each line contain two numbers of L and R.

1≤N,M,Q≤100000

Output

There should be Q output lines per test case containing Q answers required.

Sample Input

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

Sample Output

3 2 3

HINT



这是今年校赛的K题,一道经典题目,但现场没A。

在线可以用主席树,目前还不会。有一个巧妙的利用数状数组的离线解法,比较好写。

要点是:

1.将查询按右端点从小到大排序。

2.将每个数上一次出现的位置记录下来。当这个数再次出现时,将它上次出现位置上的计数消除。

Implementation:

主体是个双指针。

#include <bits/stdc++.h>
using namespace std;

const int N(1e5+5);
int n, m, q, a[N], pos[N], bit[N], ans[N];

void add(int x, int v){
    for(; x<=n; bit[x]+=v, x+=x&-x);
}

int sum(int x){
    int res=0;
    for(; x; res+=bit[x], x-=x&-x);
    return res;
}

struct P{
    int l, r, id;
    P(int l, int r, int id):l(l),r(r),id(id){}
    P(){};
    bool operator<(const P&b)const{return r<b.r;}
}p[N];

int main(){
    // ios::sync_with_stdio(false);
    for(; ~scanf("%d%d", &n, &m); ){
        for(int i=1; i<=n; i++) scanf("%d", a+i);
        scanf("%d", &q);
        for(int l, r, i=0; i<q; i++) scanf("%d%d", &l, &r), p[i]={l, r, i};

        sort(p, p+q);   //error-prone
        memset(bit, 0, sizeof(bit));
        memset(pos, 0, sizeof(pos));
        for(int i=1, j=0, k; j<q&&i<=n; ){  //error-prone
            for(; i<=p[j].r; i++){
                if(pos[a[i]]) add(pos[a[i]], -1);
                pos[a[i]]=i;
                add(i, 1);
            }
            for(k=j; k<q&&p[k].r==p[j].r; k++)  //error-prone
                ans[p[k].id]=sum(p[k].r)-sum(p[k].l-1);
            j=k;
        }
        for(int i=0; i<q; i++) printf("%d\n", ans[i]); //error-prone
    }
    return 0;
}
时间: 2024-12-30 11:10:47

DLUTOJ #1394 Magic Questions的相关文章

How To Ask Questions The Smart Way

How To Ask Questions The Smart Way Eric Steven Raymond Thyrsus Enterprises <[email protected]> Rick Moen <[email protected]> Copyright ? 2001,2006,2014 Eric S. Raymond, Rick Moen Revision History Revision 3.10 21 May 2014 esr New section on St

resize2fs: Bad magic number in super-block while trying to open

I am trying to resize a logical volume on CentOS7 but am running into the following error: resize2fs 1.42.9 (28-Dec-2013) resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root Couldn't find valid filesystem superbloc

WPF系列之三:实现类型安全的INotifyPropertyChanged接口,可以不用“Magic string” 么?

通常实现INotifyPropertyChanged接口很简单,为你的类只实现一个PropertyChanged 的Event就可以了. 例如实现一个简单的ViewModel1类: public class ViewModel1 : INotifyPropertyChanged { private string _data; public string Data { get { return _data; } set { if (_data == value) return; _data = v

HDU 4421 Bit Magic(2-sat)

HDU 4421 Bit Magic 题目链接 题意:就依据题目,给定b数组.看能不能构造出一个符合的a数组 思路:把每一个数字的每一个二进制位单独考虑.就变成一个2-sat题目了,依据题目中的式子建立2-sat的边.然后每一位跑2-sat.假设每位都符合.就是YES,假设有一位不符合就是NO 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #incl

工作笔记20170315-------关于FAQ(Frequently Asked Questions)列表的代码

源自于:http://www.17sucai.com/pins/3288.html (1)FAQ问答列表点击展开收缩文字列表 <ul>   <li class="clearfix">   <h5><b class="UI-ask"></b>什么是享赢棋牌联盟?</h5>   <div class="foldContent">   <p>享赢棋牌联盟是杭

php中的魔术方法(Magic methods)和魔术常亮

PHP中把以两个下划线__开头的方法称为魔术方法,这些方法在PHP中充当了举足轻重的作用. 魔术方法包括: __construct(),类的构造函数 __destruct(),类的析构函数 __call(),在对象中调用一个不可访问方法时调用 __callStatic(),用静态方式中调用一个不可访问方法时调用 __get(),获得一个类的成员变量时调用 __set(),设置一个类的成员变量时调用 __isset(),当对不可访问属性调用isset()或empty()时调用 __unset(),

magic number介绍

magic number:魔数,这是放在linux的目录中的文件信息块中的一个标识符,一般只有几位,用来标识该文件是什么类型的文件,可以被什么样的应用使用.这个魔数不是固定的,有时候一个文件信息中的魔数可能会不断变化.这个东西不重要的,对用户造不成多少影响.两个例子:1.ELF文件的头部,前4个字节是魔数.这个常用于识别文件类型等.linux上,二进制的可执行文件的前四个字节是7f45,而在AIX上,二进制可执行文件的前四个字节是0x01df.2.内核程序中,给一些 IO 操作进行编号时,也会用

CES聚焦:荣耀Magic凭什么成最闪耀的星

就在美国科技博客TechCrunch以<要是这款高颜值高智慧的荣耀Magic能来美国就好了>为题的评论发布不到半个月,荣耀就借刚刚于拉斯维加斯召开的CES国际消费电子展的契机,将这款很"Magic"的产品带出国门,推向了全球市场.这款曾在国内引发广泛争议的焦点产品,凭借着手机端人工智能的开拓性创新,在素有"科技风向标"和"潮流科技博览会"美誉的CES上备受关注,甚至于有好事者将之称为"2016~2017手机界最闪耀的一颗星&

Popular HashMap and ConcurrentHashMap Interview Questions

http://howtodoinjava.com/core-java/collections/popular-hashmap-and-concurrenthashmap-interview-questions/ Popular HashMap and ConcurrentHashMap Interview Questions June 14, 2013 by Lokesh Gupta In my previous post related to “How HashMap works in jav