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 ...
随机推荐
- css3爆炸效果更换图片轮播图
思路:给一个div设置一个背景图片1.jpg,然后在这个div上面用两个for循环动态的创建一个列数为C行数为R数量的span,并给这些span设置宽高.定位并设置背景图片0.jpg,然后设置每个sp ...
- 解决ios下的微信打开的页面背景音乐无法自动播放
后面的项目发现,还有两个坑,需要注意下: ·本文的解决方案的核心是利用了 微信/易信 在ready的时候会有个 WeixinJSBridgeReady/YixinJSBridgeReady事件,通过监 ...
- 第二章 时间控件(DateTime Picker)
这家伙太懒了,碰到问题才写博文,嘿嘿. 好了进入正题,二话不说,先放地址: 中文:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm ...
- Redis做消息队列文章两篇
介绍:http://www.cnblogs.com/lhfcws/p/3732535.html 具体做法:http://shift-alt-ctrl.iteye.com/blog/1867454 另外 ...
- 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条
不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...
- python中class 的一行式构造器
好处:避免类初始化时大量重复的赋值语句 用到了魔法__dict__ # 一行式构造器 class Test(): # 初始化 def __init__(self, a, b, c=2, d=3, e= ...
- Linux基础入门(20135207 王国伊)
实验一 Linux系统简介 一.实验心得 首个实验是简单介绍了Linux系统的简介,了解Linux系统的历史和发展.使我受益匪浅 实验二 基本概念及操作 一.学习目标 1.实验楼环境介绍 2.常用 ...
- Linux第二次学习笔记
#Linux第二次实验(第三周) 学习目标 熟悉Linux系统下的开发环境 熟悉vi的基本操作 熟悉gcc编译器的基本原理 熟练使用gcc编译器的常用选项 熟练使用gdb调试技术 熟悉makefile ...
- MPMoviePlayerViewController的使用 (不直接将播放器放到主视图控制器,而是放到一个内部模态视图控制器中)
其实MPMoviePlayerController如果不作为嵌入视频来播放(例如在新闻中嵌入一个视频),通常在播放时都是占满一个屏幕的,特别是在 iPhone.iTouch上.因此从iOS3.2以后苹 ...
- 20145215实验五 Java网络编程及安全
20145215实验五 Java网络编程及安全 实验内容 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验步骤 本次实验我的结对编程对象是20145208蔡野,我负责编写客 ...