博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DLUTOJ #1394 Magic Questions
阅读量:4326 次
发布时间:2019-06-06

本文共 1429 字,大约阅读时间需要 4 分钟。

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 
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

 

转载于:https://www.cnblogs.com/Patt/p/5410711.html

你可能感兴趣的文章
软件自动化测试学习步骤
查看>>
vector 简单使用
查看>>
20139216网络攻防技术第七次作业
查看>>
Sublime Text 配置
查看>>
【杂谈】需要mark的一些东西
查看>>
P2731 骑马修栅栏 欧拉函数
查看>>
sort函数
查看>>
CentOS-6.3安装配置Nginx
查看>>
女陔说"你不懂我", 到底什么意思
查看>>
uva11149
查看>>
S/4HANA中的销售计划管理
查看>>
【图灵学院09】RPC底层通讯原理之Netty线程模型源码分析
查看>>
非常的好的协同过滤入门文章(ZZ)
查看>>
数据结构:哈希表
查看>>
markdown 基本语法
查看>>
tensorflow之tf.slice()
查看>>
Python高阶函数-闭包
查看>>
Windows下安装Redis
查看>>
Ubuntu 12.04 部署 PostGIS 2.1
查看>>
手机web——自适应网页设计(html/css控制)
查看>>