题意:

给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和。

分析:

先将序列离散化,然后离线处理所有询问。

用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和。

#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 莫队算法的更多相关文章

  1. hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others)    ...

  2. 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 ...

  3. HDU 3333 Turing Tree 线段树+离线处理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...

  4. HDU 3333 Turing Tree(离线树状数组)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. UVAlive3662 Another Minimum Spanning Tree 莫队算法

    就是莫队的模板题 /* Memory: 0 KB Time: 1663 MS Language: C++11 4.8.2 Result: Accepted */ #include<cstdio& ...

  6. hdu 3333 Turing Tree

    题目链接 给n个数, m个询问, 每次询问输出区间内的数的和, 相同的数只计算一次. 数组里的数是>-1e9 <1e9, 可以把它离散以后用莫队搞... #include <iost ...

  7. HDU 3333 Turing Tree (线段树)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  9. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

随机推荐

  1. git 使用流程 命令

    克隆远程分支到本地 git clone <https://github.com/cqzyl/methods.js.git> 进入master分支 git checkout master 以 ...

  2. MongoDB 3.2 在CentOS 上的安装和配置

    MongoDB 3.2 在CentOS 上的安装和配置   2016-01-06 14:41:41 发布 您的评价:       0.0   收藏     0收藏 一.安装 编辑/etc/yum.re ...

  3. spring mvc <mvc;resources>

    spring mvc 的<mvc;resources mapping="***" location="***">标签是在spring3.0.4出现的 ...

  4. .gitignore梳理

    参考来源: https://www.cnblogs.com/kevingrace/p/5690241.html 对于经常使用Git的朋友来说,.gitignore配置一定不会陌生.废话不说多了,接下来 ...

  5. aar、jar、so的引入和aar打包包含so、aar、jar文件

    so依赖   1,先建本地仓库,指向so放置的目录

  6. NopCommerce 3.80框架研究(三)替换tinymce 为KindEditor

    NopCommerce 自带的编辑器tinymce 功能不是很全.所以尝试将其替换为功能更强大的 KindEditor 并替实现文件上传和在线浏览功能 首先下载 并解压到如下位置 请注意这里是部署在N ...

  7. bootstrap table保留多选框的分页

    有时候需要完成这种情况: 1.需要设置的是如果第一页点击复选框然后点击其他页面的话,第一页的选项被保存了 2.将所有选择好的复选款的数据保存在数组中 bootstrap table官方文档http:/ ...

  8. 聊聊javascript的事件

    javascript事件1.点击事件 onclick    obtn.click=function(){};2.移入/移出事件 onmouseover/onmouseout 注意:在父级中移入移出,进 ...

  9. hdu-1532 Drainage Ditches---最大流模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...

  10. iOS 提交form表单,上传图片

    之前不会用,总感觉很难,用后感觉不是太难,本文只是简单的讲一下怎么使用的, //实例话对象 AFHTTPSessionManager *manager = [AFHTTPSessionManager ...