spoj 3267 D-query
题目链接: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的更多相关文章
- 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 主席树求区间个数
SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...
- 【SPOJ】375. Query on a tree(树链剖分)
http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...
- SPOJ 3267. D-query (主席树,查询区间有多少个不相同的数)
3267. D-query Problem code: DQUERY English Vietnamese Given a sequence of n numbers a1, a2, ..., an ...
- SPOJ 3267 求区间不同数的个数
题意:给定一个数列,每次查询一个区间不同数的个数. 做法:离线+BIT维护.将查询按右端点排序.从左到右扫,如果该数之前出现过,则将之前出现过的位置相应删除:当前位置则添加1.这样做就保证每次扫描到的 ...
- spoj 375 QTREE - Query on a tree 树链剖分
题目链接 给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值. 树链剖分模板. #include <iostream> #includ ...
- SPOJ 375 QTREE - Query on a tree
思路 注意本题只能用C,不能用C++ 其他的都和上一题一样 代码 #include <stdio.h> #include <string.h> #define MAXN 100 ...
- 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 ...
- SPOJ 3267 DQUERY - D-query (主席树)(区间数的种数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
随机推荐
- C++基础-类和对象
本文为 C++ 学习笔记,参考<Sams Teach Yourself C++ in One Hour a Day>第 8 版.<C++ Primer>第 5 版.<代码 ...
- 基类子类在Qt信号量机制下的思考
背景知识: 基类 superClass class superClass { public: superClass() { std::string m = "superClass() &qu ...
- python列表的复制,扯一下浅拷贝与深拷贝的区别
将一个列表的数据复制到另一个列表中.使用列表[:],可以调用copy模块 import copy A = [21,22,23,24,['a','b','c','d'],25,26] B = A #直接 ...
- Python 如何用列表实现栈和队列?
1.栈结构,其实就是一个后进先出的一个线性表,只能在栈顶压入或弹出元素.用列表表示栈,则向栈中压入元素,可以用列表的append()方法来实现,弹出栈顶元素可以用列表的pop()方法实现. >& ...
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
MySQL 5.6 警告信息 command line interface can be insecure 修复 在命令行输入密码,就会提示这些安全警告信息. Warning: Using a pas ...
- sass和less的对比
); < { ; { { ; } ); } ); } ); // if 条件 @dr: if(@my-option = true, { button { ...
- 华为设备acl配置
拓扑图: 需求: 1.-vlan10内所有的主机,只能通过http访问vlan30-server的服务器;不能访问vlan40-server服务器2.-vlan20-pc1主机,可以访问vlan40- ...
- C++ md5类,封装好
在网上看到很多md5类,不过封好的很少,我就在网上看到一篇把他写的封装 头文件 #ifndef _MD5_H #define _MD5_H #pragma warning(disable:4786)/ ...
- 关于ResultSet中getDate\getTime\getTimestamp的区别的记录
getDate() 返回时间的年月日 getTime() 返回时间的时分秒 getTimestamp () 返回时间的年月日 时分秒
- Tensorflow fintune
https://zhuanlan.zhihu.com/p/42183653 tf2.0中有更简单的做法,和keras一样