hdu3333-Turing Tree-(线段树+离散化处理)
Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7323 Accepted Submission(s):
2654
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.
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).
specified subsequence in one line.
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
5
6
3
6
#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-(线段树+离散化处理)的更多相关文章
- 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 ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- Mayor's posters (线段树+离散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
随机推荐
- 【Jmeter自学】Jmeter性能测试报告(八)
http://www.cnblogs.com/YatHo/p/6092599.htmlhttp://blog.csdn.net/xiaojianpitt/article/details/4821554 ...
- C#找出第n到m个素数之间所有之和
static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); //开始的数 int m = int.Parse(Co ...
- Java,Hello World,《算法》环境搭建中的问题,用 cmd 和 IntelliJ Idea 分别编译和运行 Java 程序
▶ IntelliJ idea 下载和安装(http://www.jetbrains.com/idea/) ▶ 新建项目(如图),注意选择 SDK 类型和位置 ● 在 src 目录中新建一个 Pack ...
- Linux的JDK配置
1.下载jdk-7u1-linux-i586.rpm2.cd 到 jdk-7u1-linux-i586.rpm 所在的目录3.su 获得 root 权限4.执行安装命令: rpm -ivh jdk-7 ...
- FDLocalSQL
FDLocalSQL http://docwiki.embarcadero.com/Libraries/Berlin/en/FireDAC.Phys.SQLiteVDataSet.TFDLocalSQ ...
- 使用linux的shell脚本实现在当前行重复动态显示时间等字符串信息(不另起新行)
###本脚本在Suse11sp2当中验证正确 #!/bin/sh )) do echo -ne "\r$(date)" sleep 0.3 done ###关键在 echo 的 & ...
- maven使用fingbugs插件
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plu ...
- URL中文乱码及特殊字符处理
一.中文乱码 IE高版本(应该是9以上,不确定),在get方式请求中中文传到后台容易出现乱码问题.解决方法如下: 1.第一种,换成post方式 如果可以得话换成post方式就可以.如果采用表单或者aj ...
- setTimeout闭包常见问题
经常会遇到这样的问题,setTimeout按序输出循环数字,而不是最后输出同一个数字: 题目: for (var i = 0; i < 5; i++) { setTimeout(function ...
- 原生java读取存储为xml格式的数据,并存储到java bean里
一.举例读取的文件为:X-bond可交易债券信息_20180917.xml <?xml version="1.0" encoding="UTF-8"?&g ...