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. delphi常用函数和方法

     uses ShellApi, ActiveX, ComObj, ShlObj; function HasText(Text: string; const Values: array of strin ...

  2. Spring Cloud feign

    Spring Cloud feign使用 前言 环境准备 应用模块 应用程序 应用启动 feign特性 综上 1. 前言 我们在前一篇文章中讲了一些我使用过的一些http的框架 服务间通信之Http框 ...

  3. IE低版本浏览器兼容问题

    head标签中填写如下代码 <meta name="renderer" content="webkit"/> <meta name=" ...

  4. css position定位详解

    position:static 默认方式: position:relative 相对定位,相对于原有位置进行移动,并且保留它在文件流中的占位: position:absolute 绝对定位,相对于最近 ...

  5. Maven国内镜像-阿里云

    国外的maven下载速度堪忧,大部分国内网络访问都很慢国内的阿里云同样提供了maven的文件镜像使用:1.在maven的setting.xml加入下段代码即可使用阿里云的maven镜像 <mir ...

  6. JAVA序列化和反序列化 对象<=>IO流 对象<=>字节数组

    http://developer.51cto.com/art/201202/317181.htm http://blog.csdn.net/earbao/article/details/4691440 ...

  7. async与await

    在方法上可以加 async,方法体内需要有 await,没有await的话,会出现warn警告.async单独出现是没有用的. await只能出现在Task前面.await Task的后面的代码会被封 ...

  8. TPshop学习

    笔记大纲: tpshop目录结构 功能模块 函数库 重要配置 助手函数 插件 模板 1.TPshop目录结构 目录结构(来自官方视频截图) 看这个图,目录结构一目了然.下面要讲的内容也是根据这个图展开 ...

  9. YAML教程

    一.简介 YAML是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编程语言中使用的数据类型进行编码.YAML是YAML Ain't Markup Language简写,和GNU(" ...

  10. 数学整合 新(LUOGU)

    1.卡特兰数(P2532) 递推式:h(n)=C(2n,n)/(n+1) (n=0,1,2,...) 前十项(从零开始):1, 1, 2, 5, 14, 42, 132, 429, 1430, 486 ...