HDU #3333
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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).
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N(3e4+), M(1e5+); LL bit[N], ans[M];
int pos[N], a[N], b[N], n, q; void add(int x, int v)
{
for(; x<=n; bit[x]+=v, x+=x&-x);
} LL sum(int x)
{
LL res=;
for(; x; res+=bit[x], x-=x&-x);
return res;
} struct P
{
int l, r, id;
P(int l, int r, int id):l(l),r(r),id(id){}
P(){}
bool operator<(const P &b)const{return r<b.r;}
}p[M]; int main()
{
int T;
for(cin>>T; T--; )
{
cin>>n;
for(int i=; i<=n; i++) cin>>a[i], b[i-]=a[i];
sort(b, b+n); //error-prone
int *e=unique(b, b+n);
memset(bit, , sizeof(bit));
memset(pos, , sizeof(pos));
cin>>q;
for(int l, r, i=; i<q; i++)
{
cin>>l>>r;
p[i]={l, r, i};
}
sort(p, p+q);
for(int i=, j=, k; i<q && j<=n; )
{
for(; j<=p[i].r; j++)
{
int id=lower_bound(b, e, a[j])-b;
if(pos[id]) add(pos[id], -a[j]);
pos[id]=j, add(j, a[j]);
}
for(k=i; p[k].r==p[i].r; k++)
{
ans[p[k].id]=sum(p[k].r)-sum(p[k].l-);
}
i=k;
}
for(int i=; i<q; i++) cout<<ans[i]<<endl;
}
return ;
}
HDU #3333的更多相关文章
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- 数值标记问题 离线+树状数组 HDU 3938 + HDU 3333
HDU 3938 题目大意:给你一个长度为n的数组a,定义区间[l,r]的val为区间内所有不同的数值之和.现在有m个询问,每次询问一个区间,问区间的val是多少. 思路:将所有的询问按照右端点排序. ...
- HDU 3333 Turing Tree(离线树状数组)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)
http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others) ...
- hdu 3333 树状数组+离线处理
http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了非常久不知道怎么处理,并且答案没看懂,然后找个样例模拟下别人的代码立即懂了---以后看不懂的 ...
- 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 (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 & 3874 (线段树+离线询问)
两个题目都是求区间之内,不重复的数字之和,3333需要离散化处理................. 调试了一下午........说多了都是泪........... #include <iostr ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
随机推荐
- 当文本溢出包含的元素时加省略号之text-overflow
text-overflow是css3的属性,用来处理文本溢出,默认裁剪处理,text-overflow属性只能用于block和inline-block元素,内联的和box,flex-flow:colu ...
- JS判断数据是否是JSON类型
var isJson = function(obj){ var isjson = typeof(obj) == "object" && Object.pro ...
- GridPanel中getSelectionModel
GridPanel中getSelectionModel 更多 2014/5/1 来源:extjs学习浏览量:6783 学习标签: GridPanel extjs 本文导读:Ext.grid.GridP ...
- velocity .vm
关于.vm 后缀的文件,是velocity的文件.velocity是基于java的一种页面模板引擎,支持#if #else #foreach等写法的前台文件.$link.contextPath是该引擎 ...
- 【转】【WPF】WPF样式(Style)—触发器
样式(Styles)由三部分构成:设置器(Setter).触发器(Triggers).资源(Resources). (1)触发器,让样式的使用更加准确.灵活和高效. (2)触发器(Triggers)主 ...
- Google play billing(Google play 内支付)
准备工作 1. 通过Android SDK Manager下载extras中的Google Play services和Google Play Billing Library两个包. 2. 把下载的. ...
- C语言 文件操作6--文件打开方式详解
fopen文件打开模式 r代表read的简写,+代表可读可写,w代表write,b代表bit二进制位,t代表text r 打开只读文件,该文件必须存在r+ 打开可读可写的文件,该文件必须存在(这里的写 ...
- Apache Options Indexes FollowSymLinks详解
禁止显示Apache目录列表-Indexes FollowSymLinks如何修改目录的配置以禁止显示 Apache 目录列表.缺省情况下如果你在浏览器输入地址: http://localhost:8 ...
- Easyui Combotree问题及其相关
Easyui的setValue方法无效 今天在开发项目的时候,遇到第一个很奇怪的问题:EasyUI的setValue方法无效. $('#department_parent').combotree('s ...
- 反射中getMethods 与 getDeclaredMethods 的区别
public Method[] getMethods()返回某个类的所有公用(public)方法包括其继承类的公用方法,当然也包括它所实现接口的方法.public Method[] getDeclar ...