题意:

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

分析:

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

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

#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. vue3.0学习笔记(二)

    一.选择合适的ide 推荐使用vs code编辑器,界面清晰.使用方便,控制台功能很好用.webstorm也可以,看个人喜好. 二.ui框架选择 目前,pc端一般是选择element ui(饿了么), ...

  2. 工作中遇到的有关echarts地图和百度地图的问题

    工作中遇到的有关echarts地图和百度地图的问题 *** 前言:在做项目中需要制作一个场景是左边是柱状图,右边是地图,地图上悬浮一个按钮可以切换echarts地图和百度地图.*** 功能: 在点击左 ...

  3. Android 使用greenDAO 3.2.2 操作外部数据库

    项目开发中有时需要用到一些写死的数据,如公司的产品信息之类的.这就需要我们先把数据库文件保存在资源文件夹下,然后当应用创建时将数据库文件拷到应用安装目录的/databases/文件夹下,然后再对数据进 ...

  4. YII2 定义页面提示

    控制器里面这样写: 单条消息: 键值是规定好的,不要去自定义哦! \Yii::$app->getSession()->setFlash('error', 'This is the mess ...

  5. Dll注入:Windows消息钩子注入

    SetWindowsHook() 是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理函数之 ...

  6. app再次进入数据不加载问题

    问题原因:触发点击事件在加载页面之前完成. 1.调整了一下页面加载顺序 2.增加了settime的时间

  7. UVA - 1639 Candy (概率,精度)

    X表示剩下的糖数量,如果最后打开的是p对应的盒子.划分:Xi表示剩下i个糖,最后一次选的概率为p, 前面的服从二项分布.根据全概率公式和期望的线性性,求和就好了. 精度处理要小心,n很大,组合数会很大 ...

  8. Aizu 0121 Seven Puzzle(变进制数的完美hash)

    一遍预处理跑完所有情况,O(1)回答就好.状态记录我用的康拓和逆康拓. #include<bits/stdc++.h> using namespace std; ]; ]; ]; int ...

  9. RHEL7 本地yum源配置

    配置yum 源 1.挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别  [root@ mnt]# mount /dev/cdrom /mnt 2.在目录/etc/yum.r ...

  10. 用ComboBox控件制作浏览器网址输入框

    实现效果: 知识运用: ComboBox控件的FindString public int FindString(string s) //查找数据项集合中指定数据项的索引 和Select方法 publi ...