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. Inteiilj IDEA 团队代码格式规范

    目录 Intellij IDEA code format Tabs and Indents Spaces Wrapping and Braces Imports 更新 Intellij IDEA co ...

  2. DirectShow 制作在Unity3D中可以设置进度的视频播放插件

    如果想在Unity3D中去播放视频文件,那么最方便的方法就是使用它自带的MovieTexture. 可以实现简单的视频播放功能. Play Pause Stop. 有也只有这三个功能,  如果你想要一 ...

  3. RN中关于组件中属性的传递

    比如: 组件A想要给组件B中的组件C传递一个属性prop class A extends Component{ render(){ return( <B title = "这是一个标题 ...

  4. Web标准:六、html列表

    Web标准:六.html列表 知识点: 1.ul无序和ol有序列表 2.改变项目符号样式或用图片定义项目符号 3.横向图文列表 4.浮动后父容器高度自适应 5.IE6的双倍边距bug   1)ul无序 ...

  5. Matrix(二分套二分)

    Matrix http://poj.org/problem?id=3685 Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8 ...

  6. Pocket Cube

    Pocket Cube http://acm.hdu.edu.cn/showproblem.php?pid=5983 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. c++继承赋值兼容

    其实还是不明白,红色部分,,,求解 #include <iostream>#include <time.h>using namespace std; class B0{publ ...

  8. 4-计算九位数以内各个位数字和为s的种类

    /*假设我们当前要求的是100以内各位数之和和等于s的数有多少个,可以想到就是10以内各位数之和等于s的数的个数再加上10到100内的各位数之和等于s的个数.令dp[i][j]就代表10的j次方内各位 ...

  9. Python高级用法篇——笔记

    1.Python3字典中items()和python2.x中iteritems()的区别 在Python2.x中,items( )用于 返回一个字典的拷贝列表[Returns a copy of th ...

  10. 详解JMeter正则表达式

    详解JMeter正则表达式(1) 1.概览 JMeter中包含范本匹配软件Apache Jakarta ORO .在Jakarta网站上有一些关于它的文档,例如a summary of the pat ...