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. python-简单的sqlite3使用

    # 导入SQLite驱动: >>> import sqlite3 # 连接到SQLite数据库 # 数据库文件是test.db # 如果文件不存在,会自动在当前目录创建: >& ...

  2. 人工智能时代,是时候学点Python了!

    “是时候学点Python了”.作为一名不怎么安分的程序员,你或许觉得,产生这样的想法并不奇怪,但学习Python却是出于自己对工作现状以及如何应对未来挑战所作出的思考.读过我以前博客的朋友,可能都知道 ...

  3. svn 提交数据

    linux

  4. mysql5.7-windows安装配置

    sonar要求mysql5.6版本以上,所以安装一下最新的mysql5.7 采用相对名路径和命令行启动,这样是为了方便迁移.也提供了加入服务的指令,但没有进行测试 解压mysql的zip压缩包 解压后 ...

  5. System.Data.DbType映射关系

    有如下类型的映射对照: System.Data.SqlClient.SqlDbType  System.Data.OleDb.OleDbType System.Data.Odbc.OdbcType S ...

  6. 快速了解CSS3当中的HSLA 颜色值怎么算

    CSS3文档中提到:(HSLA) H是色度,取值在0度~360度之间,0度是红色,120度是绿色,240度是蓝色.360度也是红色. S是饱和度,是色彩的纯度,是一个百分比的值,取值在0%~100%, ...

  7. jquery小效果:新浪游戏右侧导航菜单 (页面效果)

    偷盗:新浪游戏右侧导航菜单 http://games.sina.com.cn 效果: 随着页面的滚动,左侧页面的内容,和右侧的导航菜单的按钮文字对应: 点击右侧的导航按钮,左侧页面滚动到相应的内容 2 ...

  8. Linux 虚拟机的安全加固建议

    1.修改用户的密码口令策略:   [root@centos-73-1 chpaadmin]# cat /etc/login.defs |grep -i pass # passwd command) s ...

  9. hive 测试

    hive> use gamedw;OKTime taken: 0.049 secondshive> select current_database();OKgamedwTime taken ...

  10. DDD随笔-Axon

    1. 命令处理程序从存储库中检索域对象(聚合)并执行它们的方法来更改它们的状态.这些聚合通常包含实际的业务逻辑,因此负责维护自己的状态.聚合的状态变化导致产生领域事件.领域事件和聚合形成领域模型. 2 ...