Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7323    Accepted Submission(s):
2654

Problem Description
After inventing Turing Tree, 3xian always felt boring
when solving problems about intervals, because Turing Tree could easily have the
solution. As well, wily 3xian made lots of new problems about intervals. So,
today, this sick thing happens again...

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.

 
Input
The first line is an integer T (1 ≤ T ≤ 10), indecating
the number of testcases below.
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).
 
Output
For each Query, print the sum of distinct values of the
specified subsequence in one line.
 
Sample Input
2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
 
Sample Output
1
5
6
3
6
 
翻译:t组测试,给n个数,q个查询,查询第i个数到第j个数之间的不重复的数的和是多少?
解题过程:
首先把所有查询区间记录下来,然后按照区间的右值排序,接着从左到右把每一个数更新到线段树中,并记录它出现的位置。如果一个数已经出现过,那么我们就把他上次出现的位置的值置为0,并更新它出现的位置。因为查询区间是按右值排序的,所以当前区间的左值要么和之前一样要么比之前的要大,因此把过去重复出现的数字置为0不会影响结果。当更新到某个区间的右值时,我们就查询一次该区间的答案,并把答案记录到对应的地方。
 #include <iostream>
#include<stdio.h>
#include <algorithm>
#include<string.h>
#include<cstring>
#include<math.h>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxx=;
int n,m; struct node
{
int l;
int r;
int idx;
ll ans;
};
node q[];
int a[maxx];
ll sum[maxx*]; void update(int pos,int a,int l,int r,int rt)
{
if(l==r)///拼死只为找到pos的位置,然后把最底层的sum[rt]改为a
{
sum[rt]=a;return;
}
int mid = (l+r)/;
if(pos<=mid) update(pos,a,l,mid,rt*);
else update(pos,a,mid+,r,rt*+);
sum[rt]=sum[rt*]+sum[rt*+];///每次只把一个a[i]累加到sum数组里
} ll query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
return sum[rt];
int mid=(r+l)/;
ll res=;
if(L<=mid)
res+=query(L,R,l,mid,rt*);
if(R>=mid+)
res+=query(L,R,mid+,r,rt*+);
return res;
} bool cmp1(node p1,node p2)
{
if(p1.r!=p2.r)
return p1.r<p2.r;
else
return p1.l<p2.l;
} bool cmp2(node p1,node p2)
{
return p1.idx<p2.idx;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(q,,sizeof(q));
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]); scanf("%d",&m);
for(int i=;i<=m;i++)
scanf("%d %d",&q[i].l,&q[i].r),q[i].idx=i;
sort(q+,q+m+,cmp1);
map<int,int>vis;
int j=;
for(int i=;i<=m;i++)
{
while(j<=q[i].r) ///j是一个指针,指示原数组的下标,vis[ a[i] ]则是对应a[j]这个值上次出现的位置
{
if( !vis[ a[j] ])///如果a[j]这个值没有出现过
{
update(j,a[j],,n,);
vis[ a[j] ]=j;
}
else
{
update(j,a[j],,n,);///先日常把a[j]累加到线段树里
update(vis[ a[j] ],,,n,);///然后把上一个a[j]改成0,改动这个影响了哪些sum的值。
vis[ a[j] ]=j;///更新a[j]出现的位置。
}
j++;
}
q[i].ans=query( q[i].l, q[i].r, ,n, );
}
sort(q+,q+m+,cmp2);
for(int i=;i<=m;i++)
printf("%lld\n",q[i].ans);
}
return ;
}

 

hdu3333-Turing Tree-(线段树+离散化处理)的更多相关文章

  1. HDU 3333 Turing Tree (线段树)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. HDU 3333 Turing Tree 线段树+离线处理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...

  3. SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)

    题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...

  4. HDU3333 Turing Tree 离线树状数组

    题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...

  5. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  6. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  7. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  8. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  9. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  10. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

随机推荐

  1. Java - 23 Java 抽象类

    Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不 ...

  2. Linux性能及调优指南1.2之Linux内存架构

    本文为IBM RedBook的Linux Performanceand Tuning Guidelines的1.2节的翻译原文地址:http://www.redbooks.ibm.com/redpap ...

  3. 在django中使用django_debug_toolbar进行日志记录

    一.概述 django_debug_toolbar 是django的第三方工具包,给django扩展了调试功能. 包括查看执行的sql语句,db查询次数,request,headers,调试概览等.  ...

  4. [Unity动画]01.HasExitTime & ApplyRootMotion

    参考链接: https://www.cnblogs.com/hammerc/p/4828774.html 资源下载: https://assetstore.unity.com/packages/ess ...

  5. Java课程作业之动手动脑(三)

    1.以下代码为何无法通过编译?哪儿出错了? 在Foo类中已经有了一个Foo的含参构造方法,所以在定义Foo类对象时不能使用new Foo()方法.在Foo类中再写一个无参构造方法,就能编译了. 如果类 ...

  6. 【Python爬虫实战】微信爬虫

    所谓微信爬虫,即自动获取微信的相关文章信息的一种爬虫.微信对我们的限制是很多的,所以我们需要采取一些手段解决这些限制主要包括伪装浏览器.使用代理IP等方式http://weixin.sogou.com ...

  7. 《算法》第三章部分程序 part 4

    ▶ 书中第三章部分程序,加上自己补充的代码,包括散列表.线性探查表 ● 散列表 package package01; import edu.princeton.cs.algs4.Queue; impo ...

  8. JavaScript 函数与对象的 简单区别

    直接上例子 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...

  9. 2. instr用法

    跟oracle中的instr用法一样: SQL> select count(*) from t where instr(title,‟oracle‟)>0; COUNT(*) ———- 5 ...

  10. jqGrid 获取多级标题表头

    1.jgGrid没有提供此方法获取如下标题 2.实现代码 getHeaders:function(){ var headers=[],temptrs=[]; //select the group he ...