Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3847    Accepted Submission(s): 1306

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
 
Author
3xian@GDUT
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1542 3397 1394 1540 1255 

题目描述: 求给定一个序列中某段子区间的相异数的和。

要求相异数的和,那就把相同的数去掉就行,可是由于查询的区间不确定,假设要查被删数之前包含被删数的区间的和,就不好办了。

所以我们首先读入所有的查询,然后在建立树状数组的过程中可以一次回答以i结尾的数之前的所有查询,如果有重复的就删掉,

因为比i小的以它结尾所有的查询都存下。由于要按顺序输出它的结果,所以我们给每个查询操作编号,又由于要从小到大回答以i结尾

的所有查询,所以我们采用一个结构体,成员变量分别是L,R,ID;以R来对结构体排序,将结果存入map<int,int > s;

s[ID]中,采用二维数组浪费空间,采用map进行映射.

最后一点要注意的是c数组的大小要是n的最大值×4;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#define maxn 55555*4
#define LL long long
#define inf 0x3f3f3f3f3f3f3f3f3f3f
#define lowbit(x) x & (-x)
using namespace std;
//int visit[maxn];
//int VISIT[maxn];
map <LL,LL > visit;
map <LL ,LL > VISIT;
LL A[maxn];
int n,cnt;
struct node
{
int L,R,id;
};
node Q[maxn];
LL c[maxn];
LL ans[maxn];
void init()
{
memset(c,,sizeof(c));
//memset(visit,0,sizeof(visit));
//memset(VISIT,0,sizeof(VISIT));
visit.clear();
VISIT.clear();
cnt=;
}
LL sum(int x)
{
LL ret=;
while(x>)
{
ret+=c[x];
x-=(x&(-x) );
}
return ret;
}
void add(int x,LL d)
{
while(x<=n)
{
c[x]+=d;
x+=(x& (-x) );
}
}
void solve()
{
for(int i=;i<=n;i++)
{
if(visit[A[i]]!=)
{
add(visit[A[i]],-A[i]);
}
visit[A[i]]=i;
add(i,A[i]);
if(VISIT[i]!=)
{
for(int j=;j<=VISIT[i];j++)
{
cnt++;
ans[Q[cnt].id]=sum(Q[cnt].R)-sum(Q[cnt].L-);
//printf("%lld\n",ans[Q[cnt].id]);
}
}
}
}
bool cmp(node a,node b)
{
if(a.R==b.R)
return a.L<b.L;
else
return a.R<b.R;
}
int main()
{
//freopen("test.txt","r",stdin);
int t,q;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&A[i]);
scanf("%d",&q);
for(int i=;i<=q;i++)
{
scanf("%d%d",&Q[i].L,&Q[i].R);
Q[i].id=i;
// printf("%d\n",Q[i].id);
++VISIT[Q[i].R];
}
sort(Q+,Q+q+,cmp); /* for(int i=1;i<=q;i++)
{
printf("%d\n",Q[i].id);
}*/
solve();
for(int i=;i<=q;i++)
{
printf("%lld\n",ans[i]);
} }
return ;
}

hdu 3333(树状数组 + 离线操作)的更多相关文章

  1. hdu 3333 树状数组+离线处理

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了非常久不知道怎么处理,并且答案没看懂,然后找个样例模拟下别人的代码立即懂了---以后看不懂的 ...

  2. HDU 3333 树状数组离线查询

    题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ...

  3. hdu 3333 树状数组

    思路:定义一个map容器用来记录数ai上次出现的位置.将查询区间按右边界升序进行排序,当插入第i个数ai时,pre[ai]+1---->i的区间就会多一个不同的数,其值就是ai,那么可以用upd ...

  4. Turing Tree HDU - 3333 (树状数组,离线求区间元素种类数)

    After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because ...

  5. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 4630 No Pain No Game 树状数组+离线操作

    题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...

  8. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  9. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

随机推荐

  1. msp430项目编程05

    msp430中项目---TFT彩屏显示(续) 1.TFT彩屏工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习

  2. windows 配置 apache的多个站点

    windows 配置apache的多个站点 第一步打开apache的conf/extra/httpd-vhosts.conf,复制<VirtualHost></VirtualHost ...

  3. JS基本数据类型&流程控制

    JS基本数据类型 number     -----  数值 boolean    -----  布尔值 string     -----  字符串 [x,y] -------数组 undefined  ...

  4. Populating Next Right Pointers in Each Node (DFS,没想到)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  5. new String()理解

    public static void main(String[] args){ String a=new String("ddy"); String b=new String(&q ...

  6. SQL2008安装时,“provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接) (.Net SqlClient Data Provider)” 错误的解决方案

    错误提示: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provide ...

  7. [转]linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结

    原文:http://www.jb51.net/LINUXjishu/43356.html ------------------------------------------------------- ...

  8. Guava ---- Concurrent并发

    Guava在JDK1.5的基础上, 对并发包进行扩展. 有一些是易用性的扩展(如Monitor). 有一些是功能的完好(如ListenableFuture). 再加上一些函数式编程的特性, 使并发包的 ...

  9. hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)

    链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...

  10. Android 自己定义View须要重写ondraw()等方法

    Android  自己定义View须要重写ondraw()等方法.这篇博客给大家说说自己定义View的写法,须要我们继承View,然后重写一些 方法,方法多多,看你须要什么方法 首先写一个自己定义的V ...