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. iPhone6手機產品提交了進網申請

    近期,海外投資蘋果公司為iPhone6手機產品提交了進網申請,經電信設備進網檢測機構測試和我部審查,相關產品滿足進網管理要求.根據<電信條例>有關規定,我部依法定程式在法定時限內為蘋果公司 ...

  2. 关于linux密码忘记问题解决方法

    最近在试装centos,不小心把密码丢了,进不去,查了一下资料,找到了方法,试验成功.存在这里,备用. 1. 在出现grub画面时,用上下键选中你平时启动linux的那一项,然后按e键 2. 再次用上 ...

  3. DataGridView key points

    Simple Examples => http://csharp.net-informations.com/datagridview/csharp-datagridview-readonly-c ...

  4. Tomcat(JVM)性能监控方法

    Tomcat(JVM)监控方法 1.Tomcat自带的监控页面 配置详见Tomcat安装配置监控一文,如图所示为监控页面: 2.LoadRunner编写脚本实现Tomcat监控 采用编写VuGen脚本 ...

  5. JS 获取 地址栏 参数

    法一:正则表达式 /** * 采用正则表达式获取地址栏参数: **/ var GetQueryString = function (name) { var reg = new RegExp(" ...

  6. ASP.NET MVC中从后台控制器(Controller)传递数据到前台页面视图(View)方式

    方式一: 数据存储模型Model: public class CalendarEvent { public string id { get; set; } public DateTime start ...

  7. http cache 原理实战演习

    有篇博文介绍的原理已经比较清楚了,见下面链接, 本文给出实验结果. http://www.cnblogs.com/cocowool/archive/2011/08/22/2149929.html La ...

  8. 关于缓存中Cookie,Session,Cache的使用

    文章来源:http://canann.iteye.com/blog/1941173 以前实现数据的缓存有很多种方法,有客户端的Cookie,有服务器端的Session和Application. 其中C ...

  9. Windows Azure Table storage 之 动态Table类 DynamicTableEntity

    在一般情况下,当我们在.net中使用Azure table storage的时候都会为该表建立一个TableEntity的派生类,如下所示. public class CustomerEntity : ...

  10. core animation (转)

    iOS Core Animation 简明系列教程 看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽快 ...