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
Sample Output
HINT
这是今年校赛的K题,一道经典题目,但现场没A。
在线可以用主席树,目前还不会。有一个巧妙的利用数状数组的离线解法,比较好写。
要点是:
1.将查询按右端点从小到大排序。
2.将每个数上一次出现的位置记录下来。当这个数再次出现时,将它上次出现位置上的计数消除。
Implementation:
主体是个双指针。
#include <bits/stdc++.h>
using namespace std; const int N(1e5+);
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=;
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=; i<=n; i++) scanf("%d", a+i);
scanf("%d", &q);
for(int l, r, i=; i<q; i++) scanf("%d%d", &l, &r), p[i]={l, r, i}; sort(p, p+q); //error-prone
memset(bit, , sizeof(bit));
memset(pos, , sizeof(pos));
for(int i=, j=, k; j<q&&i<=n; ){ //error-prone
for(; i<=p[j].r; i++){
if(pos[a[i]]) add(pos[a[i]], -);
pos[a[i]]=i;
add(i, );
}
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-);
j=k;
}
for(int i=; i<q; i++) printf("%d\n", ans[i]); //error-prone
}
return ;
}
DLUTOJ #1394 Magic Questions的更多相关文章
- How To Ask Questions The Smart Way
How To Ask Questions The Smart Way Eric Steven Raymond Thyrsus Enterprises <esr@thyrsus.com> R ...
- [Google Code Jam (Qualification Round 2014) ] A. Magic Trick
Problem A. Magic Trick Small input6 points You have solved this input set. Note: To advance to the ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- 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 ...
- WPF系列之三:实现类型安全的INotifyPropertyChanged接口,可以不用“Magic string” 么?
通常实现INotifyPropertyChanged接口很简单,为你的类只实现一个PropertyChanged 的Event就可以了. 例如实现一个简单的ViewModel1类: public cl ...
- 40 Questions to test your skill in Python for Data Science
Comes from: https://www.analyticsvidhya.com/blog/2017/05/questions-python-for-data-science/ Python i ...
- Google Code Jam 资格赛: Problem A. Magic Trick
Note: To advance to the next rounds, you will need to score 25 points. Solving just this problem wil ...
- Google Deepmind AI tries it hand at creating Hearthstone and Magic: The Gathering cards
http://www.techrepublic.com/article/google-deepmind-ai-tries-it-hand-at-creating-hearthstone-magic-t ...
- Expect Command And How To Automate Shell Scripts Like Magic
In the previous post, we talked about writing practical shell scripts and we saw how it is easy to w ...
随机推荐
- vue中如何不通过路由直接获取url中的参数
前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...
- 使用PDO进行sql的预处理和操作结果集
- parse_url等函数
$_SERVER["REQUEST_URI"]://这个可以获取域名后的url,比如/test1/parse_url.php?id=7&name=wuhan 常见用法$ur ...
- php安全配置记录
Php环境部署完成后,通常我们会进行一些安全设置.除了熟悉各种PHP漏洞外,还可以通过配置php.ini来加固PHP的运行环境.PHP官方也曾经多次修改php.ini的默认设置. 接下来,推荐php. ...
- 3D数学基础:四元数与欧拉角之间的转换
在3D图形学中,最常用的旋转表示方法便是四元数和欧拉角,比起矩阵来具有节省存储空间和方便插值的优点.本文主要归纳了两种表达方式的转换,计算公式采用3D笛卡尔坐标系: 单位四元数可视化为三维矢量加上第四 ...
- vi 命令 用法
一.Unix编辑器概述 编辑器是使用计算机的重要工具之一,在各种操作系统中,编辑器都是必不可少的部件.Unix及其相似的ix 操作系统系列中,为方便各种用户在各个不同的环境中使用,提供了一系列的ex编 ...
- 20135231 —— Linux 基础入门学习
20135231 何佳 学习计时:共12小时 读书:5 代码:2 作业:2 博客:3 一.学习目标 1. 能够独立安装Linux操作系统 2. 能够熟练使用Linux系统的基本命令 3. 熟练使用Li ...
- beta阶段140字评论
第一组 奋斗吧兄弟 有三种生态环境可以选择,动物还可以放大缩小,增加了许多趣味性.寓教于乐 第二组金州勇士 考试系统很贴近学生生活,不过这种系统现有也挺成熟了,其实我觉得做一个报考系统可能更讨喜 ...
- 启动页面设置,icon图标设置
更多尺寸像素如何放置请看:http://chicun.jammy.cc/ 如何设置App的启动图,也就是Launch Image? 新建一个iosLaunchImage文件夹
- Object C学习笔记13-Dictionary字典
通过Array数组和Set集合的学习和理解,可以想象得到Dictionary也分为两种情况了,那就是可变和不可变两种类型的.的确如此,在Object C中提供了两个字典类,分别为NSDictionar ...