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. vue打印html

    # vue打印功能 console.log(data.doPrint); // html字符串 let newContent =data.doPrint; let oldContent = docum ...

  2. vue实现未登录跳转到登录页面

    环境:vue 2.9.3; webpack;vue-router 目的:实现未登录跳转 例子:直接在url地址栏输入...../home,但是这个页面要求需要登陆之后才能进入,判断的值就通过登陆之后给 ...

  3. 关于MySQL中pymysql安装的问题。

    一 一般情况下我们直接在终端输入: pip3 install pymysql 就能够自动安装成功. 但是有时候我们必须先指定一个python解释器: 比如我们指定python3 在终端cmd输入:py ...

  4. 网络编程socket,详细讲述osi七层协议

    一 网络编程 源方: 我们首先来说下数据在两台计算机之间的传递:操作系统控制着除应用层以外的四层 对于用户来说我们发数据一般都是在应用层,也就是我们是直接操作应用软件的,那么应用层要把数据传给传输层就 ...

  5. git创建远程项目并进行代码管理及相关命令

    1.windows下载Git     https://git-scm.com/downloads 然后一路点击安装 2.登录github,点击右上角创建仓库 3.在本地项目根目录下 输入如下命令 ss ...

  6. hwclock

    查看.设定硬件时钟.该时钟由主机板的晶振及相关电路提供,需要主机板氧化银电池提供动力. 通过命令 hwclock 访问硬件时钟获取时间信息.该命令可以显示当前时间.重新设置时间.读取系统时间.设定系统 ...

  7. django+uwsgi+nginx+sqlite3部署+screen

    note:可通过该命令查找文件未知 sudo find / -name filename 一:项目(github) ssh root@server ip         #  连接你的服务器 git ...

  8. 使用NetBox实现ASP网页封装为EXE教程

    简单的形容就是把ASP文件打包 成一个EXE文件,并且不需要在调试的机器上安装IIS即可正常调试.如果按照说明书来操作的话,观看比较繁琐,本人为方便大家使用,现制作一个简单的使用教程. 封装过程 1. ...

  9. 432 4.3.2 STOREDRV.Deliver; recipient thread limit exceeded

    最近几天Hub-Mailbox服务器时不时就CPU超过90%.在任务管理器里面看到edgetransport占用大量CPU.进入EMC的队列查看器,看到邮箱数据库堵塞,队列上万. 堵塞的邮件大多是收件 ...

  10. Mapper的.xml文件的delete的参数问题

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-// ...