HDU 3874 Necklace
莫队算法。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = + ;
int n, m;
int a[maxn],pos[maxn],cnt[+];
long long ans[+], Ans;
int L, R;
struct X
{
int l, r, id;
}s[+]; bool cmp(const X&a, const X&b)
{
if (pos[a.l] == pos[b.l]) return a.r < b.r;
return a.l < b.l;
} int main()
{
int T; scanf("%d",&T);
while (T--)
{
memset(cnt,,sizeof cnt);
scanf("%d", &n);
int sz = sqrt(n);
for (int i = ; i <= n; i++)
{
pos[i] = i / sz;
scanf("%d", &a[i]);
} scanf("%d", &m);
for (int i = ; i <= m; i++) {
scanf("%d%d", &s[i].l, &s[i].r);
s[i].id = i;
}
sort(s + , s + + m, cmp);
Ans=;
for (int i = s[].l; i <= s[].r; i++)
{
cnt[a[i]]++;
if(cnt[a[i]]==) Ans=Ans+(long long)a[i];
}
ans[s[].id] = Ans; L = s[].l; R = s[].r; for (int i = ; i <= m; i++)
{
while (L < s[i].l)
{
cnt[a[L]]--;
if(cnt[a[L]]==) Ans=Ans-(long long)a[L];
L++;
}
while (L > s[i].l)
{
L--;
cnt[a[L]]++;
if(cnt[a[L]]==) Ans=Ans+(long long)a[L];
}
while (R < s[i].r)
{
R++;
cnt[a[R]]++;
if(cnt[a[R]]==) Ans=Ans+(long long)a[R];
}
while (R > s[i].r)
{
cnt[a[R]]--;
if(cnt[a[R]]==) Ans=Ans-(long long)a[R];
R--;
}
ans[s[i].id] = Ans;
} for (int i = ; i <= m; i++) printf("%lld\n", ans[i]);
}
return ;
}
HDU 3874 Necklace的更多相关文章
- HDU 3874 Necklace 区间查询的离线操作
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3874 对需要查询的区间按右端点排序,然后从左到右依次加入序列中的元素,同时更新,更新的方法是,把上一次出 ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- hdu 3874 Necklace(bit树+事先对查询区间右端点排序)
Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful v ...
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- HDU - 3874 Necklace (线段树 + 离线处理)
欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 3874 Necklace(线段树)
这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...
- HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
随机推荐
- Unable to open connection to supplicant on "/data/misc/wifi/sockets/wlan0"
在调试android wifi UI 的时候,出现了 logcat: Unable to open connection to supplicant on "/data/misc/wifi ...
- grub修复
sudo mount /dev/sda1 /boot/efi sudo modprobe efivarfs sudo grub-install /dev/sda sudo update-grub su ...
- 打开另外一个App
/** * 打开一个app * * @param packageName * @param data * @return */ public static boolean lanuchApp(Cont ...
- sql查询百分号的方法
select * from [tablename] where [col] like '%100/%%' escape '/'
- mouse的各种事件
IE测试对象为IE9,不全 mousemove(元素内部移动) 鼠标在元素内部移动时触发,只要鼠标移动,即使只是又移动了一个像素,也会触发,这就意味着短时间内会触发多次事件 支持情况: js onmo ...
- OpenGL ES着色器语言之着色概览(官方文档)
OpenGL ES着色器语言之着色概览(官方文档第二章) 事实上,OpenGL ES着色语言是两种紧密关联的语言.这些语言用来在OpenGL ES处理管线的可编程处理器创建着色器. 在本文档中,除非另 ...
- linux视频学习3(linux安装,shell,tcp/ip协议,网络配置)
linux系统的安装: 1.linux系统的安装方式三种: 1.独立安装linux系统. 2.虚拟机安装linux系统. a.安装虚拟机,基本是一路点下去. b.安装linux. c.linux 安装 ...
- 动画——animation(2)
日常中,我们使用的动画来源有两个方面—— 第一个,自己去定义. 通过@keyframes去定义即可,格式如下: @keyframe animatename{ 0%{ //这里面写初始的对象的css样式 ...
- sql 日结
--生成日结数据 ==================================== -- Author: <Author,,Name> -- Create date: <Cr ...
- C#,js数据排序及其操作
List<int> listint=new List<int>{2,1,7,3,8,5,4,6};listint.Sort((x, y) => x - y); var a ...