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, ...
随机推荐
- mac上的应用提权
一个mac上的app需要在/Applications/My.app/Contents/MacOS路径下创建一个配置文件,在开启root权限的账户下运行时ok,但是在没有开启root权限的账户下运行时, ...
- mybatis中app的查询语句
SELECT * FROM ( SELECT (@rownum := @rownum + ) AS rownum,c.* FROM (SELECT @rownum := ) r, ( select * ...
- environ - 用户环境(变量)
SYNOPSIS 总览 extern char **environ; DESCRIPTION 描述 变量 environ 指向的是一个叫 'environment'(环境)的字符串数组 (这个变量必须 ...
- mybatis中配置中引入properties文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- Mysql 锁总结
锁 部分总结参考博客 http://b.codejs.cc/articles/2017/10/23/1508749325215.html http://blog.csdn.net/cug_jiang1 ...
- python基础知识04-散列类型运算优先级和逻辑运算
散列类型 1.集合 定义集合 se = {1,2,3,4} se = set()定义空集合 se = {1,3,5,7} se2 = {1,3,8,9} se & se2 {1,3} 交集 s ...
- Turtle库学习
Python Turtle (Python绘图工具) 导入库 import turtle as t ps:为了方便调用我们这里给这个模块在本程序内重命名为 t 1. 画布 顾名思义就是用于绘图的区域 ...
- pycharm的快捷键以及常用设置
1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl + Shift + Enter 语句完成 Ctr ...
- Codeforces Round #387 (Div. 2) A+B+C+D!
A. Display Size 水题,暴力(数据都是水题).0:04 int main() { int n; while(~scanf("%d",&n)) { int mi ...
- MVC4 上传图片并生成缩略图
Views @using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = &qu ...