HDU 3333 Turing Tree (主席树)
题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和。
析:和求区间不同数的方法是一样,只要用主席树维护就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30000 + 5;
const int maxm = maxn * 100;
const int mod = 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn], T[maxn];
int lch[maxm], rch[maxm];
LL c[maxm];
int tot;
map<int, int> mp; int build(int l, int r){
int rt = tot++;
c[rt] = 0;
if(l == r) return rt;
int m = l + r >> 1;
lch[rt] = build(l, m);
rch[rt] = build(m+1, r);
return rt;
} int update(int rt, int pos, int val){
int newrt = tot++;
int tmp = newrt;
c[newrt] = c[rt] + val;
int l = 1, r = n;
while(l < r){
int m = l + r >> 1;
if(pos <= m){
lch[newrt] = tot++;
rch[newrt] = rch[rt];
newrt = lch[newrt];
rt = lch[rt];
r = m;
}
else {
rch[newrt] = tot++;
lch[newrt] = lch[rt];
newrt = rch[newrt];
rt = rch[rt];
l = m + 1;
}
c[newrt] = c[rt] + val;
}
return tmp;
} LL query(int rt, int pos){
LL ans = 0;
int l = 1, r = n;
while(pos < r){
int m = l + r >> 1;
if(pos <= m){
r = m;
rt = lch[rt];
}
else{
l = m + 1;
ans += c[lch[rt]];
rt = rch[rt];
}
}
return ans + c[rt];
} int main(){
int t; cin >> t;
while(t--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
mp.clear();
tot = 0;
T[n+1] = build(1, n);
for(int i = n; i; --i){
if(mp.count(a[i])){
int tmp = update(T[i+1], mp[a[i]], -a[i]);
T[i] = update(tmp, i, a[i]);
}
else T[i] = update(T[i+1], i, a[i]);
mp[a[i]] = i;
}
scanf("%d", &m);
while(m--){
int l, r;
scanf("%d %d", &l, &r);
printf("%I64d\n", query(T[l], r));
}
}
return 0;
}
HDU 3333 Turing Tree (主席树)的更多相关文章
- HDU 3333 Turing Tree 线段树+离线处理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...
- HDU 3333 Turing Tree(树状数组/主席树)
题意 给定一个长度为 \(n\) 的序列,\(m\) 个查询,每次查询区间 \([L,R]\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...
- HDU 3333 Turing Tree (线段树)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)
http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others) ...
- HDU 3333 Turing Tree(离线树状数组)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- hdu 3333 Turing Tree(线段树+离散化)
刚看到是3xian大牛的题就让我菊花一紧,觉着这题肯定各种高端大气上档次,结果果然没让我失望. 刚开始我以为是一个普通的线段树区间求和,然后啪啪啪代码敲完测试没通过,才注意到这个求和是要去掉相同的值的 ...
随机推荐
- Linux环境安装redis
redis官网地址:http://www.redis.io/ 最新版本:2.8.3 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载源码,解压缩后编译源码. $ wget ...
- hive外表parquet文件
外表关联parquet文件 1. 为什么关联了一次数据文件就不能二次被使用: 2. 为什么删除了employee,select还是可以而且有数据,1,2可能是一个问题 外表drop只是metada ...
- Linux 定时任务 crontab 讲解
linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...
- layui与layer同时引入冲突的问题
之前在项目中只有用layer,但是后来有用到了layui,结果发现同时引入这两个东东 会出现冲突的问题 导致代码运行不正常 后来网上找到了解决办法: 学习源头:http://fly.layui.com ...
- distinct与order by
不知为啥,当我得查询中出现distinct时,order by 中必须包含要查询的列,否则报错. SELECT DISTINCT a.DetailId, a.OrderId, a.ProductId, ...
- codeforce 977 F. Consecutive Subsequence
F. Consecutive Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Java-Maven-Runoob:Maven构建生命周期
ylbtech-Java-Maven-Runoob:Maven构建生命周期 1.返回顶部 1. Maven 构建生命周期 Maven 构建生命周期定义了一个项目构建跟发布的过程. 一个典型的 Mave ...
- lnmp一键安装包,安装多版本php,并开启redis与swoole
安装多版本的php sudo ./install.sh mphp Install ZendGuardLoader for PHP 7.1... unavailable now. Write ZendG ...
- java NIO(转载)
(原文地址:https://zhuanlan.zhihu.com/p/23488863) NIO(Non-blocking I/O,在Java领域,也称为New I/O),是一种同步非阻塞的I/O模型 ...
- Unity3D版本之我见
关心Unity版本的变化以及了解未来版本的内容是专业做Unity的同学必备的功课,下面我来说一下我对4.0以后版本的一些见解. v4.0: 这个版本比3.5有较大的跳跃,首先最大卖点是新的动作系统Me ...