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是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
随机推荐
- Python 栅栏凯撒
def fence_Crypto(msg,priority="row"): ''' usage: fence_Crypto(msg[, priority])->msg to ...
- ABP入门系列(5)——创建应用服务
一.解释下应用服务层 应用服务用于将领域(业务)逻辑暴露给展现层.展现层通过传入DTO(数据传输对象)参数来调用应用服务,而应用服务通过领域对象来执行相应的业务逻辑并且将DTO返回给展现层.因此,展现 ...
- Corotational 模型代码
今天看了Corotational模型的代码. 在Vega中,获得模型内力的方法是先构造一个ForceModel对象,再调用其对应方法. 对于Corotational模型,构造的流程为: 构造Corot ...
- 首次安装Pycharm出现No Python interpreter selected解决方法
刚装完Pycharm,新建Project的时候,出现了No Python interpreter selected.网上的教程里path interpret栏里应该选中python.exe,但是我搜遍 ...
- 【WPF】FillRule
获取或设置如何组合此 GeometryGroup 中所包含对象的相交区域. Dependency property identifier field: FillRuleProperty FillRul ...
- crontab任务取消发送邮件
1. 方式一,每一个计划任务后加上 >/dev/null 2>&1 */5 * * * * sh /web/adm/Shell/checkin_user_count_everyda ...
- cobbler
原理: http://www.cnblogs.com/mchina/p/centos-pxe-kickstart-auto-install-os.html 一键脚本 http://tshare365. ...
- wireshark添加ip.id字段
wireshark添加ip.id字段 为了在多个设备上追踪同一个数据包. 如果是同一个会话,则可以计算延迟, 如sta和应用服务器慢,这种问题,可以根据这个加上ip.id追踪数据到哪里慢了.
- pandas 透视表 pivot_table
The function pandas.pivot_table can be used to create spreadsheet-style pivot tables. It takes a num ...
- LeetCode-Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...