HDU 3333 Turing Tree (线段树)
Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4768 Accepted Submission(s): 1686
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.
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).
2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
1
5
6
3
6询问任意区间里有多少个不相同的数字,首先要想到,判断某个数字是否出现区间里,肯定是看的它的位置是否在区间内,那么重复的数字怎么办呢,我们只要记录这个数字最后一个位置就好了,因为这个数字是否出现在区间里,通过它最后一个出现的位置,完全可以决定。遇到这种10万个询问区间的问题,如果题目不是强制要求在线,我们应该考虑离线做,这样会简单很多把区间按照右端点排序,也可以按照左端点,然后逐个将数字插入线段树中,遇到右端点就开始查询查询这个区间的和,点更新的时候,如果之前出现过,先删除,再更新位置。这些操作,都可以用线段树解决#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <map> using namespace std;
typedef long long int LL;
const int maxn=3*1e4;
LL num[maxn*4+5];
LL a[maxn+5];
LL aa[maxn+5];
int cot;
LL c[maxn+5];
map<LL,int> m;
int st[maxn+5];
struct Node
{
int l,r;
int tag;
LL ans; }b[100005];
int n;
void pushup(int node)
{
num[node]=num[node<<1]+num[node<<1|1];
}
void update(int node,int l,int r,int val,LL tag)
{
if(l==r)
{
num[node]=tag;
return;
}
int mid=(l+r)>>1;
if(val<=mid) update(node<<1,l,mid,val,tag);
else update(node<<1|1,mid+1,r,val,tag);
pushup(node);
}
LL query(int node,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
return num[node];
int mid=(l+r)>>1;
LL ret=0;
if(L<=mid) ret+=query(node<<1,l,mid,L,R);
if(R>mid) ret+=query(node<<1|1,mid+1,r,L,R);
return ret;
}
int cmp(Node a,Node b)
{
if(a.r==b.r)
return a.l<b.l;
return a.r<b.r;
}
int cmp2(Node a,Node b)
{
return a.tag<b.tag;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(num,0,sizeof(num));
m.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
c[i]=a[i];
}
sort(a+1,a+n+1);
int pos=0;
for(int i=1;i<=n;i++)
if(!m[a[i]]) m[a[i]]=++pos;
int q;
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&b[i].l,&b[i].r);
b[i].tag=i;
}
sort(b+1,b+1+q,cmp);
int k=1;
memset(st,0,sizeof(st));
for(int i=1;i<=n;i++)
{
if(st[m[c[i]]])
update(1,1,n,st[m[c[i]]],0);
st[m[c[i]]]=i; update(1,1,n,i,c[i]);
while(i==b[k].r&&k<=q)
{
b[k].ans=query(1,1,n,b[k].l,b[k].r);
k++;
}
}
sort(b+1,b+q+1,cmp2);
for(int i=1;i<=q;i++)
printf("%lld\n",b[i].ans);
}
return 0;
}
HDU 3333 Turing Tree (线段树)的更多相关文章
- 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是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- HDU 3333 Turing Tree(树状数组/主席树)
题意 给定一个长度为 \(n\) 的序列,\(m\) 个查询,每次查询区间 \([L,R]\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...
- HDU 3333 Turing Tree (主席树)
题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和. 析:和求区间不同数的方法是一样,只要用主席树维护就好. 代码如下: #pragma comment(linker, ...
- hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)
http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others) ...
- hdu 3333 Turing Tree(线段树+离散化)
刚看到是3xian大牛的题就让我菊花一紧,觉着这题肯定各种高端大气上档次,结果果然没让我失望. 刚开始我以为是一个普通的线段树区间求和,然后啪啪啪代码敲完测试没通过,才注意到这个求和是要去掉相同的值的 ...
- HDU 3333 Turing Tree 离线 线段树/树状数组 区间求和单点修改
题意: 给一个数列,一些询问,问你$[l,r]$之间不同的数字之和 题解: 11年多校的题,现在属于"人尽皆知傻逼题" 核心思想在于: 对于一个询问$[x,R]$ 无论$x$是什么 ...
- 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 (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
随机推荐
- github提交一个空目录
github默认不上传空目录,有的时候需要空目录来保持程序的结构. 二个小问题. 1.始终保持空目录,即时里面有文件,也全部忽略掉. 建立一个.gitignore文件放到空目录内. mkdir emp ...
- Atitit.软件GUI按钮与仪表盘--db数据库区--导入mysql sql错误的解决之道
Atitit.软件GUI按钮与仪表盘--db数据库区--导入mysql sql错误的解决之道 Keyword::截取文本文件后部分 查看提示max_allowed_packet限制 Target Se ...
- 再谈API GateWay服务网关
前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...
- [转]C艹中的各种const总结
Ps: 难免碰到C家族的代码 ,各种const直接搞晕,搜集各种资料备用.... ----------------------------------------------------------- ...
- CentOS minimal 版安装图形界面的步骤分享,中文语言包
1.连接网络: CentOS minimal.iso安装好后,进入终端,默认是不开网络的, 首先启用网卡, 自动获取ip. ifconfig eth0 up dhclient eth0 这时候再 if ...
- 用c++封装linux系统调用
#include <pthread.h> #include <cstdlib> #include <ctime> #include <iostream> ...
- Android基础总结(五)网络通信2
HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...
- asp.net treeview 总结
网上关于Treeview的代码虽然多 但是都是很乱 实用性和正确性也不是很好 只好自己写一套了,时间比较紧张 性能可能还需调整 以用户组织的一个实际例子来讲诉Treeview的用法吧 组织表结构: 用 ...
- node.js发http请求
标准库中默认的HTTP模块 const https = require('https'); https.get('https://api.nasa.gov/planetary/apod?api_key ...
- Web app root system property already set to different value 错误原因及解决
http://yzxqml.iteye.com/blog/1761540 ——————————————————————————————————————————————————————————————— ...