Turing Tree

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

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
 
 
题意:给T组数据,每组数据n个数及m个询问,每个询问[l,r]问区间不同的数的和为多少?
 
题解:每个询问离线处理,先按r递增排,再按l递增排序,定义一个last[]用于记录数最后出现的位置,维护一个已经更新的区间[ll,rr],每次只要加入[ll,l],[rr,r]的区间,放入树状数组进行求和
注意:加入树状数组的时候要判断last[],如果出现过就要删除原来的,保证树状数组里每个数只出现一次而且在最后一个位置
每个询问答案是sum[rr]-sum[ll-1]
 
#include<bits/stdc++.h>
#define N 145000
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+;
const int MAX = 0x7ffffff;
using namespace std;
ll c[N], n, a[N];
ll ans[N];
map<ll,int>last;
struct node{
int l,r,k;
}q[N];
int lowbit(int x){
return x&(-x);
}
int cmp(node a, node b){
if(a.r == b.r) return a.l<b.l;
return a.r<b.r;
}
int cmp1(node a, node b){
return a.k<b.k;
}
ll sum(int x){
ll sum=;
while(x > ){
sum = sum+c[x];
x -= lowbit(x);
}
return sum;
}
void add(int x,int d){
while(x <= n){
c[x] = c[x]+d;
x += lowbit(x);
}
}
int main()
{
int T, i, j, m, l, r;
while(~scanf("%d", &T)){
while(T--){
scanf("%d", &n);
for(i=;i<=n;i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for(i=;i<m;i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].k = i;
}
sort(q, q+m,cmp);
mes(c);
last.clear();
l = q[].l;
r = q[].l;
for(i=;i<m;i++){
for(j=q[i].l;j<=l;j++){
if(!last[a[j]]){
last[a[j]] = j;
add(j, a[j]);
}
}
for(j=r+;j<=q[i].r;j++){
if(!last[a[j]]){
last[a[j]] = j;
add(j, a[j]);//
}
else{
add(last[a[j]], -a[j]);
last[a[j]] = j;
add(j, a[j]);
}
}
ans[q[i].k] = sum(q[i].r)-sum(q[i].l-);
l = min(l,q[i].l);
r = max(r,q[i].r);
}
for(i=;i<m;i++)
printf("%I64d\n", ans[i]);
}
}
return ;
}
 
 
 

HDU3333 Turing Tree 树状数组+离线处理的更多相关文章

  1. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  2. HDU 3333 Turing Tree --树状数组+离线处理

    题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...

  3. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  4. 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...

  5. SPOJ DQUERY树状数组离线or主席树

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  6. Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

    Necklace HDU - 3874  Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...

  7. D-query SPOJ 树状数组+离线

    D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...

  8. [bzoj1935][shoi2007]Tree 园丁的烦恼(树状数组+离线)

    1935: [Shoi2007]Tree 园丁的烦恼 Time Limit: 15 Sec  Memory Limit: 357 MBSubmit: 980  Solved: 450[Submit][ ...

  9. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

随机推荐

  1. java 关于hashcode和equals的测试

    package thinkingInJava; import java.util.HashMap; /* * 测试在向以hashcode为基础的集合(HashSet , HashMap , HashT ...

  2. 上传下载后台函数以及前端脚本(webuploader) 备份

    import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import ...

  3. Sharepoint 2013 user permissions(用户权限)

     Read: This permission level gives you read-only access to the website. Contribute:  In addition to ...

  4. 给RecyclerView实现的GridView加上HeaderView和FooterView

    给RecyclerView设置布局管理器 GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); 写适配器,添加子项 ...

  5. 20145224&20145238《信息安全系统设计基础》实验一 开发环境的熟悉

    20145224陈颢文20145238荆玉茗 <信息安全系统设计基础>第一次实验报告 课程:信息安全系统设计基础 班级: 1452 姓名:荆玉茗 陈颢文 学号:20145238 20145 ...

  6. iOS图片压缩处理

    理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...

  7. word-break、word-wrap和其他文字属性

    word-break: break-all; 控制是否断词.(粗暴方式断词)break-all,是断开单词.在单词到边界时,下个字母自动到下一行.主要解决了长串英文的问题. word-wrap: br ...

  8. Vcenter server 5.5安装部署

    1.安装VMware ESXi Server 虚拟主机安装方法请看本人博客 "实践记忆": http://www.cnblogs.com/zoulongbin/p/5896836. ...

  9. Ubuntu(14.04)远程连接windwos(server2008 r2)

    只需安装rdeasktop即可, sudo apt-get install rdesktop rdesktop 192.168.1.100 -g 800x600 其他参数 -f 其实用Ubuntu自带 ...

  10. Android--Retrofit的简单使用(一)

    1,如果不太了解retrofit的同学可以先去官网学习一下简单使用:http://square.github.io/retrofit/,这里我们以一个简单的Get请求的例子来练习一下 2,https: ...