HDU 3333 Turing Tree 莫队算法
题意:
给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和。
分析:
先将序列离散化,然后离线处理所有询问。
用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = 30000 + 10;
const int maxq = 100000 + 10;
int n, m, a[maxn], b[maxn];
int cnt[maxn];
LL ans[maxq];
struct Query
{
int l, r, id;
bool operator < (const Query& t) const {
return ((l / m) < (t.l / m)) || ((l / m) == (t.l / m) && r < t.r);
}
};
Query q[maxq];
int main()
{
int T; scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++) { scanf("%d", a + i); b[i] = a[i]; }
sort(b + 1, b + 1 + n);
int tot = unique(b + 1, b + 1 + n) - b - 1;
for(int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + 1 + tot, a[i]) - b;
m = sqrt(n);
int Q; scanf("%d", &Q);
for(int i = 1; i <= Q; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q + 1, q + 1 + Q);
LL sum = 0;
int L = 1, R = 0;
memset(cnt, 0, sizeof(cnt));
for(int i = 1; i <= Q; i++) {
while(L < q[i].l) {
cnt[a[L]]--;
if(!cnt[a[L]]) sum -= b[a[L]];
L++;
}
while(L > q[i].l) {
L--;
if(!cnt[a[L]]) sum += b[a[L]];
cnt[a[L]]++;
}
while(R < q[i].r) {
R++;
if(!cnt[a[R]]) sum += b[a[R]];
cnt[a[R]]++;
}
while(R > q[i].r) {
cnt[a[R]]--;
if(!cnt[a[R]]) sum -= b[a[R]];
R--;
}
ans[q[i].id] = sum;
}
for(int i = 1; i <= Q; i++) printf("%lld\n", ans[i]);
}
return 0;
}
HDU 3333 Turing Tree 莫队算法的更多相关文章
- hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)
http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others) ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 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(离线树状数组)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- UVAlive3662 Another Minimum Spanning Tree 莫队算法
就是莫队的模板题 /* Memory: 0 KB Time: 1663 MS Language: C++11 4.8.2 Result: Accepted */ #include<cstdio& ...
- hdu 3333 Turing Tree
题目链接 给n个数, m个询问, 每次询问输出区间内的数的和, 相同的数只计算一次. 数组里的数是>-1e9 <1e9, 可以把它离散以后用莫队搞... #include <iost ...
- 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 (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
随机推荐
- spring transaction 初识
spring 事务初识 1.spring事务的主要接口,首先盗图一张,展示出spring 事务的相关接口.Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibern ...
- 原生js添加类名,删除类名
1.添加类名: document.getElementById("myDiv").classList.add('mystyle'); 2.删除类名: document.getEle ...
- uvm_regex——DPI在UVM中的实现(三)
UVM的正则表达是在uvm_regex.cc 和uvm_regex.svh 中实现的,uvm_regex.svh实现UVM的正则表达式的源代码如下: `ifndef UVM_REGEX_NO_DPI ...
- Webstrom使用手册小记
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/10883350.html,多谢~=.= 1.从git上拉取项目 或者 2.切换分支(webstro ...
- Nginx+Keepalived双主轮询负载均衡
双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...
- SAP云平台运行环境Cloud Foundry和Neo的区别
SAP云平台提供了两套运行环境:Cloud Foundry和Neo 从下图能发现,Cloud Foundry的运行环境,基础设施由第三方公司提供,比如Amazon亚马逊和Microsoft微软,SAP ...
- 设置RichTextBox控件的文本的对齐方式
实现效果: 知识运用: RichTextBox控件的SelectionAlignment属性 //获取或设置在当前选择或插入点的对齐方式 public HorizontalAlignment Sele ...
- Github的基本功能
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30 ...
- 如何在python中读写和存储matlab的数据文件(*.mat)
使用sicpy.io即可.sicpy.io提供了两个函数loadmat和savemat,非常方便. 以前也有一些开源的库(pymat和pymat2等)来做这个事, 不过自从有了numpy和scipy以 ...
- SummerVocation_Learning--java的线程机制
线程:是一个程序内部的执行路径.普通程序只有main()一条路径.如下列程序: import java.lang.Thread; //导入线程实现包 public class Test_Thread ...