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 ...
随机推荐
- Android 6.0动态申请权限时,权限框闪一下就消失的问题;
Android 蓝牙BLE开发需要位置权限,不然扫描不到周围的蓝牙信息: 位置权限申请: if (Build.VERSION.SDK_INT < 23){return;} //判断是否有权限 i ...
- 在django中使用django_debug_toolbar进行日志记录
一.概述 django_debug_toolbar 是django的第三方工具包,给django扩展了调试功能. 包括查看执行的sql语句,db查询次数,request,headers,调试概览等. ...
- 关于MySQL中pymysql安装的问题。
一 一般情况下我们直接在终端输入: pip3 install pymysql 就能够自动安装成功. 但是有时候我们必须先指定一个python解释器: 比如我们指定python3 在终端cmd输入:py ...
- jQuery操作DOM节点的方法总结
1.parent():获得当前匹配元素集合中每个元素的父元素,该方法只会向上一级对 DOM 树进行遍历 $('li.item-a').parent().css('background-color', ...
- oracle取出多个字段列中的最大值和最小值
greatest 函数和least函数 select serverid, greatest(e.core0, e.core1, e.score2 ) from e
- 微信小程序笔记<七>视图层 —— wxml
微信小程序的视图层由 *.wxml 组成,wxml与html一样属于标签语言,但wxml与html的标签截然不一样. xwml特性 一.数据绑定 <!--wxml--> <view& ...
- [Unity算法]弧度和角度
参考链接: https://zhidao.baidu.com/question/576596182.html 1.弧度和角度的转换 2.sin函数 3.cos函数 4.tan函数 5.特殊的三角函数值 ...
- Oracle多行记录合并/连接/聚合字符串的几种方法
怎么合并多行记录的字符串,一直是oracle新手喜欢问的SQL问题之一,关于这个问题的帖子我看过不下30个了,现在就对这个问题,进行一个总结. 什么是合并多行字符串(连接字符串)呢,例如:SQL& ...
- laravel 的 intervention-image 图像处理笔记
安装: https://blog.csdn.net/beyond__devil/article/details/62230610 需求: PHP >= 5.4 Fileinfo 扩展 GD库 & ...
- Android权限管理
使用系统权限 为了保护系统的完整性和用户隐私权,Android 在访问受限的沙盒中运行每款应用.如果应用需要使用其沙盒以外的资源或信息,则必须明确请求权限.根据应用请求的权限类型,系统可能会自动授予权 ...