HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333
题意就是询问区间不同数字的和。
比较经典的树状数组应用。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
struct query {
int l, r, pos;
bool operator <(const query &cmp) const {
return r < cmp.r;
}
}q[N];
int a[];
LL bit[], ans[N];
map <int, int> mp; void update(int i, LL val) {
for( ; i <= ; i += (i&-i))
bit[i] += val;
} LL sum(int i) {
LL s = ;
for( ; i >= ; i -= (i&-i))
s += (LL)bit[i];
return s;
} int main()
{
int t, n, m;
scanf("%d", &t);
while(t--) {
memset(bit, , sizeof(bit));
mp.clear();
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
}
scanf("%d", &m);
for(int i = ; i <= m; ++i) {
scanf("%d %d", &q[i].l, &q[i].r);
q[i].pos = i;
}
sort(q + , q + m + );
int index = ;
for(int i = ; i <= n; ++i) {
if(mp.find(a[i]) != mp.end()) { //a[i]曾经出现过
update(mp[a[i]], (LL)-a[i]);
}
mp[a[i]] = i;
update(i, a[i]);
while(q[index].r == i && index <= m) {
ans[q[index].pos] = sum(q[index].r) - sum(q[index].l - );
++index;
}
}
for(int i = ; i <= m; ++i) {
printf("%lld\n", ans[i]);
}
}
return ;
}
HDU 3333 Turing Tree (树状数组)的更多相关文章
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree --树状数组+离线处理
题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3333 Turing Tree 离线 线段树/树状数组 区间求和单点修改
题意: 给一个数列,一些询问,问你$[l,r]$之间不同的数字之和 题解: 11年多校的题,现在属于"人尽皆知傻逼题" 核心思想在于: 对于一个询问$[x,R]$ 无论$x$是什么 ...
- HDU 3333 Turing Tree(树状数组/主席树)
题意 给定一个长度为 \(n\) 的序列,\(m\) 个查询,每次查询区间 \([L,R]\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...
- HDU 3333 Turing Tree 线段树+离线处理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...
- hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)
http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others) ...
- HDU 3854 Glorious Array(树状数组)
题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...
随机推荐
- Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence
Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence 如下代码: for (ind ...
- BZOJ 1617 渡河问题
普及组dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...
- 几个排序算法的JS实现
最近找工作,复习一下数据结构的知识,看到排序这一块,顺便动手改了一下. 直接插入排序: 插入排序就是把数据分为有序区和无序区,遍历到的数据和有序区域的数据进行比较,找到要插入的位置,插入位置后的数据做 ...
- codeigniter框架扩展核心类---实现前台后台视图的分离
1. 扩展核心类,主要作用就是扩展系统现在的功能. 为前台增加独立的视图文件夹: a. 自定义路径常量 :在application ->config/ constants.php中增加 /*m ...
- HDU1026 Ignatius and the Princess I
解题思路:打印路径是关键,细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> using ...
- 集合框架null与size=0
被QA人员一眼指出来的问题,唉,好丢人 上栗子
- 批量生成sqlldr文件,高速卸载数据
SQL*Loader 是用于将外部数据进行批量高速加载的数据库的最高效工具,可用于将多种平面格式文件加载到Oracle数据库.SQL*Loader支持传统路径模式以及直接路径这两种加载模式.关于SQL ...
- 更新Code First生成的数据库
1,首次访问时会自动生成数据库 2,某个Model增加一个字段后,再次访问会报,数据库不是最新 操作 1,Enable-Migrations 注意选择Default project为Star.Core ...
- beginUpdates和endUpdates
我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...
- “-bash: svn: command not found”
今天升级了Xcode5.界面更加的清爽了,但是在命令行里SVN也失去了作用了. 当我要更新的时候提示:“-bash: svn: command not found” 解决办法: 1:打开Prefere ...