题目链接:http://vjudge.net/problem/SPOJ-DQUERY

------------------------------------------------------------------------------

主席树模板题之一 求不带修改的区间不同数个数

最近学习了下发现主席树就是可持久化线段树 由于每次单点修改只会改变一条链上的信息

所以我们可以把其余部分的信息重复利用 然后再新建一条链作为这次修改后的这条链的新状态

所以每次单点修改的时间复杂度和空间复杂度都是$O(logN)$

懂了原理后就像普通线段树一样按照自己的习惯来写就好 不需要准备什么模板

而对于这种求区间不同的数的个数的问题 我们只需维护对于每种前缀区间 每个数最后一次出现位置

然后所有最后一次出现位置对整个区间有$1$的贡献

这样就可以根据询问的区间右端点对应的版本 用区间减法来求区间不同数的个数

 #include <bits/stdc++.h>
using namespace std;
const int N = 3e4 + , T = N * , L = 1e6 + ;
int sum[T], ch[T][], root[N << ], ma[N], last[L];
int croot, cnt, n, q;
void build(int x, int L, int R)
{
if(L == R)
{
sum[x] = ;
return;
}
int mid = (L + R) >> ;
ch[x][] = ++cnt;
build(cnt, L, mid);
ch[x][] = ++cnt;
build(cnt, mid + , R);
sum[x] = sum[ch[x][]] + sum[ch[x][]];
}
void update(int x, int L, int R, int y, int delta, int pre)
{
if(L == R)
{
sum[x] = sum[pre] + delta;
return;
}
int mid = (L + R) >> ;
if(y <= mid)
{
ch[x][] = ++cnt;
update(cnt, L, mid, y, delta, ch[pre][]);
ch[x][] = ch[pre][];
}
else
{
ch[x][] = ch[pre][];
ch[x][] = ++cnt;
update(cnt, mid + , R, y, delta, ch[pre][]);
}
sum[x] = sum[ch[x][]] + sum[ch[x][]];
}
int query(int x, int L, int R, int r)
{
if(R <= r)
return sum[x];
int mid = (L + R) >> ;
if(r <= mid)
return query(ch[x][], L, mid, r);
return
sum[ch[x][]] + query(ch[x][], mid + , R, r);
}
int main()
{
scanf("%d", &n);
root[++croot] = ++cnt;
ma[] = ;
build(cnt, , n);
int x, y;
for(int i = ; i <= n; ++i)
{
scanf("%d", &x);
root[++croot] = ++cnt;
update(cnt, , n, i, , root[croot - ]);
if(last[x])
{
root[++croot] = ++cnt;
update(cnt, , n, last[x], -, root[croot - ]);
}
last[x] = i;
ma[i] = root[croot];
}
scanf("%d", &q);
while(q--)
{
scanf("%d%d", &x, &y);
if(x > )
printf("%d\n", sum[ma[y]] - query(ma[y], , n, x - ));
else
printf("%d\n", sum[ma[y]]);
}
return ;
}

spoj 3267 D-query的更多相关文章

  1. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  2. SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  3. SPOJ - 3267. D-query 主席树求区间个数

    SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...

  4. 【SPOJ】375. Query on a tree(树链剖分)

    http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...

  5. SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)

    3267. D-query Problem code: DQUERY English Vietnamese Given a sequence of n numbers a1, a2, ..., an  ...

  6. SPOJ 3267 求区间不同数的个数

    题意:给定一个数列,每次查询一个区间不同数的个数. 做法:离线+BIT维护.将查询按右端点排序.从左到右扫,如果该数之前出现过,则将之前出现过的位置相应删除:当前位置则添加1.这样做就保证每次扫描到的 ...

  7. spoj 375 QTREE - Query on a tree 树链剖分

    题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...

  8. SPOJ 375 QTREE - Query on a tree

    思路 注意本题只能用C,不能用C++ 其他的都和上一题一样 代码 #include <stdio.h> #include <string.h> #define MAXN 100 ...

  9. SPOJ 3978 Distance Query(tarjan求LCA)

    The traffic network in a country consists of N cities (labeled with integers from 1 to N) and N-1 ro ...

  10. SPOJ 3267 DQUERY - D-query (主席树)(区间数的种数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

随机推荐

  1. 状态压缩dp相关

    状态压缩dp 状态压缩是设计dp状态的一种方式. 当普通的dp状态维数很多(或者说维数与输入数据有关),但每一维总 量很少是,可以将多维状态压缩为一维来记录. 这种题目最明显的特征就是: 都存在某一给 ...

  2. 56. Merge Intervals (JAVA)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  3. iOS 审核app被拒绝的各种理由以及翻译

    原 apps被拒绝的各种理由以及翻译:http://my.oschina.net/201003674/blog/356189#OSC_h1_3 1. Terms and conditions(法律与条 ...

  4. AFNetworking2.0源码解析<三>

    本篇说说安全相关的AFSecurityPolicy模块,AFSecurityPolicy用于验证HTTPS请求的证书,先来看看HTTPS的原理和证书相关的几个问题. HTTPS HTTPS连接建立过程 ...

  5. Python字符串(str)方法调用

    # str# n = 'pianYU'# v = n.capitalize() # 将字符串的首字母大写# print(v)## n = 'pianYI'# v1 = n.isupper() # 判断 ...

  6. VB.net删除节点,数据库,文件

    Private Sub mnuDel_Click()'删除节点Dim sKey As String'Dim sFile As StringDim oFS As FileSystemObjectDim ...

  7. socket客户端怎么判断http响应数据的结束

    前言 原文地址:https://blog.csdn.net/nimasike/article/details/81122784 http连接 短连接 定义:http头不包含Connection: Ke ...

  8. [效率神技]Intellij 的快捷键和效率技巧|系列一|常用快捷键

    Intellij 是个功能强大的IDE,这里只讲window下社区版的Intellij. 1. 常用快捷: Alt+回车 导入包,自动修正Ctrl+N   查找类Ctrl+Shift+N 查找文件Ct ...

  9. python数组中在某一元素前插入数据

    # 已知有一个已经排好序的数组.要求是,有一个新数据项,要求按原来的规律将它插入数组中. a=[1,2,3,4,5,6,7,8,9]num=int(input("input num:&quo ...

  10. Textarea随着文本的字数自适应高度,后来发现用 contenteditable 代替textarea 效果更佳

    做移动端项目遇到很多问题,最近比如textarea 随着文本的字数自动撑开高度, 网上也查阅了一些资料发现比较有用的方法 就是获取 textarea的行数和换行符来动态改变textarea的高度  就 ...