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 ...
随机推荐
- jquery.validate运用和扩展
一.运用 默认校验规则 ().required:true 必输字段 ().remote:"remote-valid.jsp" 使用ajax方法调用remote-valid.jsp验 ...
- Android动画原理分析
最近在Android上做了一些动画效果,网上查了一些资料,有各种各样的使用方式,于是乘热打铁,想具体分析一下动画是如何实现的,Animation, Animator都有哪些区别等等. 首先说Anima ...
- 利用ViewHolder优化自定义Adapter的典型写法
1 public class MarkerItemAdapter extends BaseAdapter { private Context mContext = null; private List ...
- Android开发探秘之一:创建可以点击的Button
感觉到自己有必要学习下手机开发方面的知识,不论是为了以后的工作需求还是目前的公司项目. 当然,任何新东西的开始,必然伴随着第一个HelloWorld,Android学习也不例外.既然才开始,我就不做过 ...
- JS案例之4——Ajax多图上传
近期项目中有好几次用到多图上传,第一次在项目中真正用到Ajax技术,稍微整理了下,贴个案例出来. 我们传统的做法是当用户提交一个表单时,就向web服务器端发送一个请求.服务器接受并处理传来的表单信息, ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- Bootstrap 排版
Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans-serif 作为其默认的字体栈. 使用 Bootstrap 的排版特性,您可以创建标题.段落. ...
- xcode 出现the file couldn't be opened 怎么解决
右键——show In finder——显示xcode包内容——将有数字的删除——把有用的xcode双击
- FPGA学习之基本结构
如何学习FPGA中提到第一步:学习.了解FPGA结构,FPGA到底是什么东西,芯片里面有什么,不要开始就拿个开发板照着别人的东西去编程.既然要开始学习FPGA,那么就应该从其基本结构开始.以下内容是我 ...
- 九幽史程博:助力国内开发者借Win10东风出海
微软Biuld2016大会刚刚结束,会议上微软CEO纳德拉Show出的一大波黑科技,又一次让软粉们心情为之振奋,信仰充值爆棚! 尽管过去一年微软的Win10 Mobile表现不尽如人意,可是凭借PC端 ...