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. IOS网络第二天 - 01-基本的HTTP请求

    ***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...

  2. 响应式网站通用css

    /* core.css v1.1 | MIT License | corecss.io */ html { font-family: sans-serif; font-size: 100%; line ...

  3. GDC2016 Epic Games【Bullet Train】 新风格的VR-FPS的制作方法

    追求“舒适”和“快感”的VR游戏设计方法   http://game.watch.impress.co.jp/docs/news/20160318_749016.html     [Bullet Tr ...

  4. 一些asp.net使用

    1.英文格式的日期转为中文格式 eg:string englishDate="03-04-2012"; string chineseDate=Regex.replace(engli ...

  5. 第三方支付过程中session失效问题

    第三方支付过程中session失效问题 时间 2015-05-13 12:36:23  IT社区推荐资讯 原文  http://itindex.net/detail/53436-session-问题 ...

  6. Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现

    http://blog.csdn.net/wave_1102/article/details/39271693 分类: Android(105) 作者同类文章X Android4.3 规范了BLE的A ...

  7. 分析app和wap手机网站的不同

    随着手机3G.4G时代的到来,手机功能的越来越强大,手机App的市场是越来越火爆,时代在更新随之而来的事物也在更新,不能更上时代的步伐是落后的表现,所以不仅仅是手机APP在完善,手机网站WAP也在不断 ...

  8. Hadoop学习笔记: 全排序

    在Hadoop中实现全排序有如下三种方法: 1. 只使用一个reducer 2. 自定义partitioner 3. 使用TotalOrderPartitioner 其中第一种方法显然违背了mapre ...

  9. 20145320 《Java程序设计》第6周学习总结

    20145320 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 流(Stream)是对「输入输出」的抽象,注意「输入输出」是相对程序而言的 10.1 Input ...

  10. 时光煮雨 Unity3D实现2D人物动画① UGUI&Native2D序列帧动画

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...