SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)
| Time Limit: 227MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
| English | Vietnamese |
Given a sequence of n numbers a1, a2, ..., an 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 ai, ai+1, ..., aj.
Input
- Line 1: n (1 ≤ n ≤ 30000).
- Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
- 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).
Output
- For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.
Example
Input
5
1 1 2 1 3
3
1 5
2 4
3 5 Output
3
2
3
Hint
| Added by: | Duc |
| Date: | 2008-10-26 |
| Time limit: | 0.227s |
| Source limit: | 50000B |
| Memory limit: | 1536MB |
| Cluster: | Cube (Intel Pentium G860 3GHz) |
| Languages: | All except: ERL JS NODEJS PERL 6 VB.net |
| Resource: | © VNOI |
给出若干个数,给出q次询问,每次询问有一个区间,问在这个区间内有多少个不同的数,输出其数量
利用主席树套模板即可
//主席树
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
/*
* 给出一个序列,查询区间内有多少个不相同的数
*/
const int MAXN = ;
const int M = MAXN * ;
int n,q,tot;
int a[MAXN];
int T[M],lson[M],rson[M],c[M];
int build(int l,int r)
{
int root = tot++;
c[root] = ;
if(l != r)
{
int mid = (l+r)>>;
lson[root] = build(l,mid);
rson[root] = build(mid+,r);
}
return root;
}
int update(int root,int pos,int val)
{
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = , r = n;
while(l < r)
{
int mid = (l+r)>>;
if(pos <= mid)
{
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid+;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int root,int pos)
{
int ret = ;
int l = , r = n;
while(pos < r)
{
int mid = (l+r)>>;
if(pos <= mid)
{
r = mid;
root = lson[root];
}
else
{
ret += c[lson[root]];
root = rson[root];
l = mid+;
}
}
return ret + c[root];
} int main(){
while(scanf("%d",&n) == )
{
tot = ;
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
T[n+] = build(,n);
map<int,int>mp;
for(int i = n;i>= ;i--)
{
if(mp.find(a[i]) == mp.end())
{
T[i] = update(T[i+],i,);
}
else
{
int tmp = update(T[i+],mp[a[i]],-);
T[i] = update(tmp,i,);
}
mp[a[i]] = i;
}
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(T[l],r));
}
}
return ;
}
SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)的更多相关文章
- SPOJ - 3267. D-query 主席树求区间个数
SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...
- D-query SPOJ - DQUERY 主席树查询区间内不同数出现的次数
我们不以权值建立主席树,而是区间端点作为值建立线段树,一个个插入a[i],我们发现这个数之前是存在的,就需要在上个版本的主席树上减去原来的位置,并加上现在的位置,这样我们在i版本的主席树,维护1-r中 ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
- SPOJ:D-query(非常规主席树求区间不同数的个数)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- SPOJ - DQUERY (主席树求区间不同数的个数)
题目链接:https://vjudge.net/problem/SPOJ-DQUERY 题目大意:给定一个含有n个数的序列,有q个询问,每次询问区间[l,r]中不同数的个数. 解题思路:从左向右一个一 ...
- 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)
主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...
- 主席树——求区间第k个不同的数字(向右密集hdu5919)
和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- top 进程管理
top 动态查看进程 前五行解释: 第一行参数说明: top - 07:06:19 当前时间 up 10 min, 系统运行时间,格式为时:分 1 user, 当前登录用户数 load av ...
- 使用nginx+tomcat实现静态和动态页面的分离
博主最近在优化一个javaweb项目,该项目之前一直都是使用tomcat处理用户请求的,无论静态还是动态的东西,一律交给tomcat处理.tomcat主要是负责处理servlet的,静态的文件还是交给 ...
- JavaScript中,关于class的调用
PS:class的调用,其实是可以叠加的,当然了这要求样式不同的情况下,如果样式相同,则后一个样式会覆盖前一个样式. 1.举例如下: <div id="test" class ...
- OpenCV中CvMat的初始化[转]
一)cvCreateMat创建和分配数据CvCreateMat会创建CvMat,并为CvMat分配数据.cvCreateMat可以配合cvInitMatHeader来初始化CvMat对象.因为CvCr ...
- Azure 项目构建 – 托管静态网站
本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 Azure Web 应用的静态托管网站, 实践讲解如何使用 Azure 门户创建 Web 应用, 部署静态网站源代码,设置自定义域名等. ...
- [文章泛读] The varying faces of a program transformation systems (ACM Inroads, 2012)
Beevi S. Nadera, D. Chitraprasad, and Vinod S. S. Chandra. 2012. The varying faces of a program tran ...
- Linux Device Driver 学习(1)
Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...
- flask--Django 基本使用
#导入flaskfrom flask import Flask #创建应用 app = Flask(__name__) #创建根路径视图 @app.route('/') def hello_world ...
- abp viewmodel的写法
我的写法 public class QuotaCreateOrEditViewModel { public QuotaDto LoanQuota { get; set; } public bool I ...
- VC-基础:隐藏不安全函数的warning-_CRT_SECURE_NO_WARNINGS
>tmp.cpp(): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strc ...