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. jetBrains phpstorm/webstorm 编辑器使用诀窍

    下载地址 https://www.jetbrains.com/products.html?fromMenu CTRL + F12 / COMMAND + F12 打开类中的结构 Ctrl + b / ...

  2. HTML中禁用表单控件的两种方法readonly与disabled

    时候我们会希望表单上的控件是不可修改的,比如在修改密码的网页中,显示用户名的文本框就应该是不可修改状态的,下面与大家分享下禁用表中控件的两种方法 在网页的制作过程中,我们会经常使用到表单.但是有时候我 ...

  3. ThinkPHP 3.2.3 多模块 和 多应用 的配置

    多模块 在 ThinkPHP 3.2.3 中,默认的应用目录是 ./Application,下面的默认模块是 Home 模块,如果此时需要添加一个 Admin 模块用于后台应用,根据手册 http:/ ...

  4. Rewrite服务器和robots文件屏蔽动态页面

    Rewrite服务器使用robots文件屏蔽动态页面.

  5. springMVC中利用model在JSTL进行回填值

    1.ringMVC中利用model回填值 后台中,利用model返回值,如 model.addAttribute("MS_info" , MS_info); 前台回填值: text ...

  6. Unity3D 系统宏

    Platform Defines The platform defines that Unity supports for your scripts are:     Property: Functi ...

  7. webstorm 代码自动换行

    代码自动换行 菜单File→Settings→Editor→General

  8. docker入门的文章

    PART 1: OVERVIEW OF MICROSERVICE ARCHITECTURE & CONTAINERIZATION PART II: GETTING SET-UP AND STA ...

  9. RDIFramework.NET ━ 9.2 员工管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.2  员工管理 -Web部分 员工(职员)管理主要是对集团.企事业内部员工进行管理.在后面的章节可以看到有一个用户管理,这两者 ...

  10. Scrum 项目1.0

     1.内容: NABCD模型  1.需求   在当今的时代,把钱存进银行确实是比较稳妥的方式,但收益实在少得可怜.因此投资便是一个比较好的方式,当然,在投资前你需要一个软件去帮你直观地显现出各种投资的 ...