题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=3333

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

分析

题目大概说给一个序列,多次询问一个区间内不同数之和。

图灵树的来历原来是这个。。经典的线段树离线所有查询右端点排序的题目吧。。

所有询问右端点排序后,从小到大扫过去,线段树维护序列区间和,用一个map记录各个数最右边出现的位置,一遇到一个数就把之前位置消除并更新当前位置,相当于把各个数尽量向右移动。。

代码

#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
#define MAXN 33333 struct Query{
int i,l,r;
bool operator<(const Query &q) const{
return r<q.r;
}
}que[111111]; long long tree[MAXN<<2];
int N,x,y;
void update(int i,int j,int k){
if(i==j){
tree[k]+=y;
return;
}
int mid=i+j>>1;
if(x<=mid) update(i,mid,k<<1);
else update(mid+1,j,k<<1|1);
tree[k]=tree[k<<1]+tree[k<<1|1];
}
long long query(int i,int j,int k){
if(x<=i && j<=y) return tree[k];
int mid=i+j>>1;
long long ret=0;
if(x<=mid) ret+=query(i,mid,k<<1);
if(y>mid) ret+=query(mid+1,j,k<<1|1);
return ret;
} int a[MAXN];
long long ans[111111];
int main(){
int t,n,m;
scanf("%d",&t);
while(t--){
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",&que[i].l,&que[i].r);
que[i].i=i;
}
sort(que,que+m);
map<int,int> posi;
memset(tree,0,sizeof(tree));
for(N=1; N<n; N<<=1);
int p=0;
for(int i=0; i<m; ++i){
while(p<que[i].r){
++p;
if(posi.count(a[p])){
x=posi[a[p]]; y=-a[p];
update(1,N,1);
}
x=p; y=a[p];
update(1,N,1);
posi[a[p]]=p;
}
x=que[i].l; y=que[i].r;
ans[que[i].i]=query(1,N,1);
}
for(int i=0; i<m; ++i){
printf("%lld\n",ans[i]);
}
}
return 0;
}

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. ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  6. HDU3333 Turing Tree 树状数组+离线处理

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

  7. Turing Tree_线段树&树状数组

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  8. hdu3333 Turing Tree 2016-09-18 20:53 42人阅读 评论(0) 收藏

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

  9. HDU-3333 Turing Tree 分块求区间不同数和

    HDU-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有几种解法.这里讲一下用分块解决的方法.( 离线树状数组解法看这里 Hdu ...

随机推荐

  1. C#内存管理与垃圾回收

    垃圾回收还得从根说起,就像生儿育女一样. 根:根是一个位置,存放一个指针,该指针指向托管堆中的一个对象,或是一个空指针不指向任何对象,即为null.根存在线程栈或托管堆中,大部分的跟都在线程栈上,因为 ...

  2. windows7下修改hosts文件无效解决办法(转)

    通常会为了开发方便.或者屏蔽掉一些恶意网站,我们会在hosts(c:\windows\system32\drivers\etc\hosts)文件中进行相应的域名指向,例:

  3. CSS3实现阴阳鱼

    直接上代码: <!doctype html> <html> <head> <meta charset="utf-8" /> < ...

  4. windows 10卸载自带软件

    http://jingyan.baidu.com/article/14bd256e4ad268bb6c26126d.html http://jingyan.baidu.com/album/ae97a6 ...

  5. javaweb学习总结(五)——Servlet开发(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  6. 前端进阶试题css(来自js高级前端开发---豪情)既然被发现了HOHO,那我就置顶了嘿嘿!觉得自己技术OK的可以把这套题目做完哦,然后加入高级前端的社区咯

    http://www.cnblogs.com/jikey/p/4426105.html js高级前端开发加群方法(此群很难进,里面纯技术,严禁广告,水群) 完整题目做完发邮箱(jikeytang@16 ...

  7. Android 基础篇(二)

    ADB进程 adb指令 adb install xxx.apk adb uninstall 包名 adb devices adb start-server adb kill-server adb sh ...

  8. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  9. Uva 2034

    求定积分 (结果当时我没看到平均值) //正常多项式求 #include<iostream> #include<cstdio> #include<cmath> us ...

  10. python的一道面试题 __call__ 的使用.

    class Person: def __init__(self): self.age = 1 def __call__(self, *args, **kwargs): print 'age', sel ...