ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Now given a sequence of N
numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For
each Query(i, j), you are to caculate the sum of distinct values in the
subsequence Ai, Ai+1, ..., Aj.
For each case, the input format will be like this:
* Line 1: N (1 ≤ N ≤ 30,000).
* Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000).
* Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
* Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).
这个题要求区间内不同值的和,一开始没有任何思路,看了题解,原来需要对查询进行离线操作。
因为需要求区间内互异值的和,对于一个固定的区间的话,自然只需要对于相同的值只留一个,其他置零即可。
但是对于动态的查询区间,保留的那个值的位置相对关键。
通过对查询的区间进行排序可以讲区间有序的排列(以区间的右端点递增排序)。
因为这样的话,对于这个数列,从第一个逐个插入,那么区间是[1, 1]->[1, 2]->[1, 3]……这样生成的,如果我们对于a[i],把之前出现过的a[i]都置零,这样此时对于已生成的区间[1, i],我们查询区间和[k, i]的时候(因为区间是按照右端点有序查询的),必然对于任意值p,都是先包含离i最近的那个p,才会包含前面的p,而前面的p已经被置零,故不会加入计算。而离i最近的p又会加入计算,不会影响结果。
所以这样边生成区间[1, i],边对于[k, i]区间查询。对于之前出现过的a[i]置零,便可以达到查询效果。当然最好输出的结果是按照题目要求的查询顺序输出的,这里采用了保存在sum数组中。
不过这里还有一点就是,如何对于之前的a[i]置零,此处采用了map,map里保存了最右端的a[i]的脚标,这样不断更新即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; //线段树
//区间每点增值,求区间和
const int maxn = 30005;
struct node
{
int lt, rt;
LL val;
}tree[4*maxn]; //向上更新
void PushUp(int id)
{
tree[id].val = tree[id<<1].val + tree[id<<1|1].val;
} //建立线段树
void Build(int lt, int rt, int id)
{
tree[id].lt = lt;
tree[id].rt = rt;
tree[id].val = 0;//每段的初值,根据题目要求
if (lt == rt)
{
//tree[id].val = 1;
return;
}
int mid = (lt + rt) >> 1;
Build(lt, mid, id<<1);
Build(mid+1, rt, id<<1|1);
//PushUp(id);
} //更改区间内某个点的值
void Change(int lt, int rt, int id, int to)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
{
tree[id].val = to;
return;
}
int mid = (tree[id].lt + tree[id].rt) >> 1;
if (lt <= mid)
Change(lt, rt, id<<1, to);
if (rt > mid)
Change(lt, rt, id<<1|1, to);
PushUp(id);
} //查询某段区间内的he
LL Query(int lt, int rt, int id)
{
if (lt <= tree[id].lt && rt >= tree[id].rt)
return tree[id].val;
int mid = (tree[id].lt + tree[id].rt) >> 1;
LL ans = 0;
if (lt <= mid)
ans += Query(lt, rt, id<<1);
if (rt > mid)
ans += Query(lt, rt, id<<1|1);
return ans;
} struct qq
{
int from, to;
int id;
}q[100005]; bool cmp(qq a, qq b)
{
return a.to < b.to;
} int a[30005], n, m;
LL sum[100005]; void Work()
{
Build(1, n, 1);
map<int, int> s;
int t, now = 0;
for (int i = 1; i <= n; ++i)
{
t = s[a[i]];
if (t == 0)
{
Change(i, i, 1, a[i]);
s[a[i]] = i;
}
else
{
Change(t, t, 1, 0);
Change(i, i, 1, a[i]);
s[a[i]] = i;
}
for (;now < m && q[now].to == i; now++)
{
sum[q[now].id] = Query(q[now].from, q[now].to, 1);
}
}
} void Output()
{
for (int i = 0; i < m; ++i)
printf("%I64d\n", sum[i]);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = 0; times < T; ++times)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = 0; i < m; ++i)
{
scanf("%d%d", &q[i].from, &q[i].to);
q[i].id = i;
}
sort(q, q+m, cmp);
Work();
Output();
}
return 0;
}
ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)的更多相关文章
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
- ACM学习历程—HDU 2795 Billboard(线段树)
Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...
- ACM学习历程——POJ3321 Apple Tree(搜索,线段树)
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...
- HDU 3333 Turing Tree 线段树+离线处理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...
- ACM学习笔记:可持久化线段树
title : 可持久化线段树 date : 2021-8-18 tags : 数据结构,ACM 可持久化线段树 可以用来解决线段树存储历史状态的问题. 我们在进行单点修改后,线段树只有logn个(一 ...
- 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是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- HDU3333 Turing Tree 离线树状数组
题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...
- HDU3333 Turing Tree(线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...
随机推荐
- Smart Battery Specification Revision 1.1
1.SBS Specifications 2.System Management Bus (SMBus) Specification
- MySql(六):影响 MySQL Server 性能的相关因素
MySQL 最多的使用场景是WEB 应用,那么我们就以一个WEB 应用系统为例,逐个分析其系统构成,进行经验总结,分析出数据库应用系统中各个环境对性能的影响. 一.商业需求对性能的影响 这里我们就拿一 ...
- AndroidX86模拟器Genymotion的一些使用和另一款Andy模拟器
命令行启动虚拟机 当我们下载安装好,可以通过命令行运行指定名字模拟器 D:\ProgramFiles\Genymobile\Genymotion\player --vm-name "Sam ...
- servletResponse 随机生成图片验证码
/***********************************servlet页面************************************/ package response; ...
- partition by和group by对比
今天大概弄懂了partition by和group by的区别联系. 1. group by是分组函数,partition by是分析函数(然后像sum()等是聚合函数): 2. 在执行顺序上, 以下 ...
- J2EE——开发环境搭建
WEB环境搭建 1.J2EE开发环境搭建(1)——安装JDK.Tomcat.Eclipse 2.JAVA运行环境和J2EE运行环境的搭建 3.jsp开发所需要的eclipse插件(lomboz.tom ...
- 06 redis中set结构及命令详解
集合 set 相关命令 集合的性质: 唯一性,无序性,确定性 注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素 但,因为集合的无序性,无法通过下 ...
- 下面哪个进制能表述 13*16=244是正确的?)[中国台湾某计算机硬件公司V2010年5月面试题]
A.5B.7C.9D.11解析:13如果是一个十进制的话,它可以用13=1*101+3*100来表示.现在我们不知道13是几进制,那我们姑且称其X进制.X进制下的13转化为十进制可以用13=1*X1+ ...
- 关于BlockingQueue
1 什么是BlockingQueue 2 BlockingQueue有什么用 3 ArrayBlockingQueue的用途 1 它是一个线程安全的队列 2 它是一个容量固定的队列 3 它为什么叫bl ...
- 504 Gateway Timeout Error 502 Bad Gateway
总结 1. 502没有收到相应,或者收到了但不及时? cannot get a response in time 540收到了无效的响应 received an invalid response fr ...