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 ...
随机推荐
- 转:程序员必须知道的几个Git代码托管平台
http://www.open-open.com/lib/view/open1420704561390.html
- 通过imeMode禁用键盘只能输入数字
var obj = document.getElementById('y'); var arr = [48,49,50,51,52,53,54,55,56,57];//数字对应的键码 obj.onke ...
- 观察器observes与对象初始化
Demo.Person2 = Ember.Object.extend({ init: function() { alert('lljsd'); this.set('salutation', " ...
- Gradle的安装
1.Gradle依赖JDK,所以先在机器上安装好JDK,设置好JAVA_HOME与PATH环境变量 2.从官网下载合适的Gradle发行包 3.发行包是一个zip文件,解压后,将 GRADLE_HOM ...
- U5398 改数(num)
U5398 改数(num) 5通过 28提交 题目提供者52zyz 标签 难度尚无评定 提交 最新讨论 暂时没有讨论 题目背景 又是一年NOIP,科学馆的五楼:“我们看下这道题,我们来模拟一下…2,3 ...
- [py]os.walk爬目录&sys.argv灵活获取参数
1, 遍历目录 os.walk('/tmp') os.next() 2,sys.argv ######################################## py@lanny:~/t ...
- c语言 动态数组
C语言中,在声明数组时,必须明确告诉编译器数组的大小,之后编译器就会在内存中为该数组开辟固定大小的内存.有些时候,用户并不确定需要多大的内存,使用多大的数组,为了保险起见,有的用户采用定义一个大数组的 ...
- Got a packet bigger than 'max_allowed_packet' bytes
昨天用导入数据的时候发现有的地方有这个错误.后来才发现我用RPM包装的MYSQL配置文件里面有old_passwords=1去掉就可以了. Got a packet bigger than ‘max_ ...
- Linq To Entities 及其相关
说到Linq,很多人都非常熟悉,我们可以很方便的用它来操纵对象的集合.今天要说的是Linq To Entities及其相关的操作.下面一步一步的来进行.这里我着重强调的是语法上面的一些注意点.所以怎么 ...
- Opencv实现运动检测
运动检测多种多样,这里的需求只是检测到有运动物体就行了,而且 要尽量减少误报的情况.另外尽量降低CPU的消耗,因为最终需要在树莓派上面运行. 看了一些中文的文章,发现无法很好地理解别人说的内容,反而是 ...