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之main函数(笔记)

    1.标准的main函数形式 对于main函数,只要是 public static void main(String[] args) public static void main(String... ...

  2. react-native win7环境搭建

    时间:2016-08-22 晚,西安 1.安装jdk java version "1.6.0_45"Java(TM) SE Runtime Environment (build 1 ...

  3. C#Web异步操作封装

    using System; using System.Collections.Generic; using System.Web; namespace HttpAsync { /// <summ ...

  4. 下拉列表select显示ng-options

    js中如何处理: it-equipment-list-maintenance-create-controller.js 'use strict'; myApp.controller( 'itEquip ...

  5. elasticsearch使用操作部分

    本片文章记录了elasticsearch概念.特点.集群.插件.API使用方法. 1.elasticsearch的概念及特点.概念:elasticsearch是一个基于lucene的搜索服务器.luc ...

  6. Git diff 常见用法

      Git diff 用于比较两次修改的差异 1.1 比较工作区与暂存区 git diff  比较的是单个仓库的工作区与暂存区的差别,repo diff是对git diff的封装,用来分别显示各个项目 ...

  7. Performance Analyzer Tool

    PAL工具的使用大同小异,网上看到这篇文章挺不错的,直接翻译过来.如果你在过去有Exchange性能问题,你肯定知道有很多可变因素会影响Exchange整体性能,有时需要很长的时间才能找到问题的根源, ...

  8. 解决 Virtual Box 启动 Cannot load R0 module supLoadModule returned VERR_LDR_MISMATCH_NATIVE Failed to register ourselves as a PCI Bus (VERR_MODULE_NOT_FOUND)

    返回 代码:E_FAIL (0x80004005)组件:Console界面:IConsole {8ab7c520-2442-4b66-8d74-4ff1e195d2b6} 原因: 我新建了一个vdi文 ...

  9. JQuery兼容IE6问题汇总(不断更新)

    兼容IE6真是苦逼的差事,无奈中... 逗号的问题:IE6中要去掉最后的逗号 var o={ id:1, Name:"abc", //这里的逗号一定要去掉 } HTML的结构,由于 ...

  10. lua 环境揭秘

    什么是环境? http://www.lua.org/manual/5.1/manual.html#2.9 Besides metatables, objects of types thread, fu ...