HDU3333 Turing Tree(线段树)
题目
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(线段树)的更多相关文章
- HDU 3333 Turing Tree (线段树)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3333 Turing Tree 线段树+离线处理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- HDU3333 Turing Tree 离线树状数组
题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...
- ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Turing Tree_线段树&树状数组
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- 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 ...
- HDU-3333 Turing Tree 分块求区间不同数和
HDU-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有几种解法.这里讲一下用分块解决的方法.( 离线树状数组解法看这里 Hdu ...
随机推荐
- R语言排序:sort(),rank(),order()示例
> x<-c(97,93,85,74,32,100,99,67) > sort(x) [1] 32 67 74 85 93 97 99 100 > order(x) [1] 5 ...
- HTML2
1. IIS是一个软件,在"客户端服务器"模型中,它是服务器端软件,它主要提供基于HTTP的文档服务,主要是WWW 的发送,以及FTP的文件下载服务. VS提供了" ...
- js高级群的一些整理6月
https://github.com/the5fire/backbonejs-learning-note/blob/master/chapters/01-hello-backbonejs.rst Ba ...
- 如何解决mathpage.dll或MathType.dll文件找不到问题
解决方法(具体图文教程): 步骤一 要确保路径被office信任.依次打开word->文件->选项->信任中心->信任中心设置->添加新位置,添加C:\Program F ...
- Windows平台下Qt中glut库的使用
用Qt中的QGLWidget窗体类中是不包括glut工具库的,难怪在myGLWidget(在我的程序中是QGLWidget的派生类)中绘制实心球体是说“glutSolidSphere”: 找不到标识符 ...
- 评价cnblogs.com的用户体验
我们作为大学计算机科学与技术专业的本科生,使用博客园主要是用来搜索和阅读技术资料,并且使用它来编辑发表博客.我们偏向于搜索便捷,界面友好的网站风格.我们对cnblogs的期望是更便捷精准,更便于编辑. ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
- phpMyAdmin提示:配置文件权限错误,无法写入!解决方法
访问phpMyAdmin提示: 配置文件权限错误,无法写入! 解决办法: chmod -R 755 ./phpmyadmin 这样设置下phpMyAdmin目录权限属性为就可以访问了.原来phpMyA ...
- PHP header函数使用大全
PHP header函数大全 header('Content-Type: text/html; charset=utf-8'); header('Location: http://52php.cnbl ...
- phpcms手机门户相关
相关标签 {$WAP['sitename']}标题 {list_url(3)} 调取栏目链接 {template "wap","header"}{templat ...