Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4658    Accepted Submission(s): 1640

Problem 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
 

本题用线段树撸简单的,但我尝试了下传说中神奇的莫队算法,水过去了

用cnt数组记录每个数的个数,进行操作

莫队这东西真的很无脑,只有两个函数自己要想想,其他都模板

数据大,先离散化处理

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack> using namespace std;
#define maxn 30005
#define LL long long
LL a[maxn],b[maxn]; int m,n;
struct node
{
int l,r,id;
} qu[100005];
int pos[100005];
LL ans[100005];
int cnt[100005];
LL Ans; bool cmp(node a,node b)
{
if(pos[a.l]==pos[b.l])
return a.r<b.r; return pos[a.l]<pos[b.l]; } int get(LL x)
{
int pos;
int l=1;
int r=n;
while(l<=r)
{
int m=(l+r)/2;
if(b[m]<x)
l=m+1;
else if(b[m]==x) pos=m,r=m-1;
else r=m-1;
}
return pos;
} void add(int x)
{
if(cnt[a[x]]==0)
Ans+=b[a[x]];
cnt[a[x]]++;
} void del(int x)
{
if(cnt[a[x]]==1)
Ans-=b[a[x]];
cnt[a[x]]--;
} int main()
{
int o;
while(~scanf("%d",&o))
{
while(o--)
{
scanf("%d",&n);
memset(cnt,0,sizeof(cnt));
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int kt=sqrt(n);
for(int i=1; i<=n; i++)
{ a[i]=get(a[i]);
pos[i]=i/kt;
} scanf("%d",&m);
for(int i=1; i<=m; i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
} sort(qu+1,qu+1+m,cmp); int l=1;
int r=0;
Ans=0;
for(int i=1; i<=m; i++)
{
while(l<qu[i].l)
{
del(l);
l++;
}
while(l>qu[i].l)
{
l--;
add(l);
} while(r<qu[i].r)
{
r++;
add(r);
} while(r>qu[i].r)
{
del(r);
r--;
}
ans[qu[i].id]=Ans; }
for(int i=1; i<=m; i++)
printf("%lld\n",ans[i]); }
}
return 0;
}

hdu3333 Turing Tree 2016-09-18 20:53 42人阅读 评论(0) 收藏的更多相关文章

  1. PAT 甲 1005. Spell It Right (20) 2016-09-09 22:53 42人阅读 评论(0) 收藏

    1005. Spell It Right (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...

  2. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...

  3. House Robber 分类: leetcode 算法 2015-07-09 20:53 2人阅读 评论(0) 收藏

    DP 对于第i个状态(房子),有两种选择:偷(rob).不偷(not rob) 递推公式为: f(i)=max⎧⎩⎨⎪⎪{f(i−1)+vali,f(i−2)+vali,robi−1==0robi−1 ...

  4. ZOJ2417 Lowest Bit 2017-04-18 20:53 38人阅读 评论(0) 收藏

    Lowest Bit Time Limit: 2 Seconds      Memory Limit: 65536 KB Given an positive integer A (1 <= A ...

  5. 如何在hadoop中控制map的个数 分类: A1_HADOOP 2015-03-13 20:53 86人阅读 评论(0) 收藏

    hadooop提供了一个设置map个数的参数mapred.map.tasks,我们可以通过这个参数来控制map的个数.但是通过这种方式设置map的个数,并不是每次都有效的.原因是mapred.map. ...

  6. 使用ganglia监控hadoop及hbase集群 分类: B3_LINUX 2015-03-06 20:53 646人阅读 评论(0) 收藏

    介绍性内容来自:http://www.uml.org.cn/sjjm/201305171.asp 一.Ganglia简介 Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于测 ...

  7. Self Numbers 分类: POJ 2015-06-12 20:07 14人阅读 评论(0) 收藏

    Self Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22101   Accepted: 12429 De ...

  8. Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏

    Binary Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6355   Accepted: 2922 Descr ...

  9. NYOJ-102 次方求模 AC 分类: NYOJ 2014-02-06 18:53 184人阅读 评论(0) 收藏

    地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=102 //a^b mod c=(a mod c)^b mod c很容易设计出一个基于二分的递归 ...

随机推荐

  1. Kotlin语言学习笔记(6)

    运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.n ...

  2. libUpnp缓冲区溢出、拒绝服务等漏洞分析

    该漏洞存在于UPnP™设备的便携式SDK中,也叫做 libupnp.这个库是用来实现媒体播放(DLAN)或者NAT地址转换(UPnP IGD).智能手机上的应用程序可用这些功能播放媒体文件或者利用用户 ...

  3. 迷你MVVM框架 avalonjs 学习教程18、一步步做一个todoMVC

    大凡出名的MVC,MVVM框架都有todo例子,我们也搞一下看看avalon是否这么便宜. 我们先从react的todo例子中扒一下HTML与CSS用用. <!doctype html> ...

  4. jQuery中的几个模块总结

    Query插件,以备并希望在前端方面有所长进.请批评指正. 一,类型判断全解 JQuery判断类型扩展方法:$.type() /*type: function( obj ) { if ( obj == ...

  5. Python环境配置, atom-python配置

    环境变量 路径 在window下配置环境变量,配置到文件夹级就可以了: D:\program\python3 编码 因为windows默认的编码是ASIIC,所以使用atom时候中文是乱码,需要在环境 ...

  6. git cherry-pick基本使用

    git cherry-pick可以选择某一分支中的一个或几个commit来进行操作--commit 使用场景: 稳定版本分支1与开发版本分支2,不能直接把两个分支合并,否则会导致版本混乱,要将分支2中 ...

  7. ndarray

    ndarray ndarray-多维数组对象 ndarray数组分两部分 实际数据 描述数据的元数据(数据类型/维度等) ndarray所有元素类型相同,下标从0开始 import numpy as ...

  8. SQL数据库简单的建立与操作

    数据类型 符号标志 整数型 bigint,int,smallint,mediumint,tinyint 精确数值型 decimal,numeric 浮点型 float,real,double 位型 b ...

  9. gtftools软件简单介绍(我自己不建议用,因为我发现不好用)

    1)背景 生物信息学研究经常涉及计算或提取基因的各种特征,如基因ID作图,GC含量计算和不同类型的基因长度,通过操纵基因模型,这些模型通常以GTF格式注释,可从ENSEMBL或GENCODE数据库获得 ...

  10. logging的使用

    [logging的使用] import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.setLevel(logg ...