Spoj 3267 DQUERY - D-query
题目描述
English VietnameseGiven a sequence of n numbers a _{1}1 , a _{2}2 , ..., a _{n}n and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence a _{i}i , a _{i+1}i+1 , ..., a _{j}j.
输入输出格式
输入格式:
- Line 1: n (1 ≤ n ≤ 30000).
- Line 2: n numbers a _{1}1 , a _{2}2 , ..., a _{n}n (1 ≤ a _{i}i ≤ 10 ^{6}6 ).
- Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
- In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).
输出格式:
- For each d-query (i, j), print the number of distinct elements in the subsequence a _{i}i , a _{i+1}i+1 , ..., a _{j}j in a single line.
输入输出样例
5
1 1 2 1 3
3
1 5
2 4
3 5
3
2
3 离线扫描经典题,但是我复习一下主席树233
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200005;
struct node{
int tag;
node *lc,*rc;
}nil[maxn*53],*rot[30005],*cnt;
int n,Q,a[30005],le,ri,pre[maxn*5]; node *update(node *u,int l,int r){
node *ret=++cnt;
*ret=*u;
if(l>=le&&r<=ri){
ret->tag++;
return ret;
} int mid=l+r>>1;
if(le<=mid) ret->lc=update(ret->lc,l,mid);
if(ri>mid) ret->rc=update(ret->rc,mid+1,r); return ret;
} int query(node *u,int l,int r){
if(l==r) return u->tag;
int mid=l+r>>1;
if(le<=mid) return u->tag+query(u->lc,l,mid);
else return u->tag+query(u->rc,mid+1,r);
} inline void init(){
rot[0]=nil->lc=nil->rc=cnt=nil;
nil->tag=0;
for(int i=1;i<=n;i++){
le=pre[a[i]]+1,ri=i,rot[i]=update(rot[i-1],1,1000000);
pre[a[i]]=i;
}
} inline void solve(){
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&le,&ri);
printf("%d\n",query(rot[ri],1,1000000));
}
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",a+i);
init();
solve();
return 0;
}
Spoj 3267 DQUERY - D-query的更多相关文章
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)
3267. D-query Problem code: DQUERY English Vietnamese Given a sequence of n numbers a1, a2, ..., an ...
- SPOJ - 3267. D-query 主席树求区间个数
SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...
- SPOJ 3267 DQUERY - D-query (主席树)(区间数的种数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ 3267 D-query (可持久化线段树,区间重复元素个数)
D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair ...
- SPOJ 3267: DQUERY 树状数组,离线算法
给出q个询问,询问一段区间里面的不同元素的个数有多少个. 离线做,用树状数组. 设树状数组的意义是:1--pos这个段区间的不用元素的种类数.怎么做?就是add(pos,1);在这个位置中+1,就是说 ...
- spoj 3267 D-query
题目链接:http://vjudge.net/problem/SPOJ-DQUERY --------------------------------------------------------- ...
- SPOJ 3267 DQUERY(离线+树状数组)
传送门 话说这好像HH的项链啊…… 然后就说一说上次看到的一位大佬很厉害的办法吧 对于所有$r$相等的询问,需要统计有多少个不同的数,那么对于同一个数字,我们只需要关心它最右边的那一个 比如$1,2, ...
随机推荐
- 洛谷 P1163 银行贷款
题目描述 当一个人从银行贷款后,在一段时间内他(她)将不得不每月偿还固定的分期付款.这个问题要求计算出贷款者向银行支付的利率.假设利率按月累计. 输入输出格式 输入格式: 输入文件仅一行包含三个用空格 ...
- windows下安装python
1. 进入python官网 https://www.python.org/downloads/windows/ 2.根据我们的电脑位数选择版本,尽量选择 Download Windows x86 ...
- Ubuntu 16.04 LTS: apt-get update 失败处理 Aborted (core dumped)
在Ubuntu 16.04运行sudo apt-get update出现如下错误: rogn@ubuntu:~$ sudo apt-get update Get:1 http://us.archive ...
- 样式化复选框(Styling Checkbox)
原理:https://www.tuicool.com/articles/y67jee 表现:http://www.freejs.net/demo/381/index.html https://www. ...
- typescript知识教程
https://ts.xcatliu.com/basics/type-of-function.html
- 第1节 flume:10、flume的更多组件介绍
作业:flume如何实现收集mysql的数据,没隔几秒钟,查看mysql中的数据是否有变化,一旦有变化,把数据拿过来,存到hdfs上. 需要使用custom source.可网上搜索,github上.
- upupoo(网页壁纸)自主修改一:农历
最近在使用一款upupoo的壁纸软件,感觉还可以,主要是对其中html可设置为壁纸方面情有独钟 前几天在它的网页区发现了一个壁纸,感觉挺好: 感觉内容有点少,今天在工作空余时间就在其中加上了农历,同时 ...
- viewport移动端适配,读文笔记
文章地址: viewport移动端适配 笔记: 移动端适配目的: 希望在屏幕尺寸大小不同的手机上进行访问页面时,页面显示的效果能合理的展示,我们期望的是在手机屏幕较大时显示的内容比较大一些,手机屏幕小 ...
- IDEA ctrl+alt+L 格式化快捷键无效时解决
这几天发现自己Intellij IDEA ctrl+alt+L格式化代码无效 设置里面按照快捷键搜索 按了 ctrl+alt+L 也没反应 但是我设置的确实是默认的 ctrl+alt+L 最后终于找到 ...
- CSS--浮动与定位
*浮动布局能够实现横向多列布局. 1.在网页中,元素有三种布局模型: 1.流动模型(Flow) 2.浮动模型 (Float) 3.层模型(Layer) 流动(Flow)是默认的网页布局模式.流动布局模 ...