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 ...
随机推荐
- URL特殊字符需转义
URL特殊字符需转义 1.空格换成加号(+) 2.正斜杠(/)分隔目录和子目录 3.问号(?)分隔URL和查询 4.百分号(%)制定特殊字符 5.#号指定书签 6.&号分隔参数 转义字符的原因 ...
- HUST - 1599 Multiple
input 长度不大于3*10e5的数字串 output 不含前导0的能整除64的字串的个数(0算一个,064不算) 一般数组中找能整除一个数的字串都是用取余来做的 用一个a[64]来存下从1-i位累 ...
- ios 获得版本号
获取iphone的系统信息使用[UIDevice currentDevice],信息如下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone OS ...
- boost库之geometry<二>
#include <boost/assign.hpp> #include <boost/geometry/core/point_type.hpp> #include <b ...
- jquery的校验规则的方法
//validate 选项*********************************************************** $("form").validat ...
- 破解MyEclipse2013注册码
1.下载破解工具 http://down8.3987.com:801/2010/Myeclipse_zcj.3987.com.rar 2.打开 找到meclipse安装路径找到plugins文件夹打开 ...
- Smoke Testing(冒烟测试)
Smoke Testing 的概念最早源于制造业,用于测试管道.测试时,用鼓风机往管道里灌烟,看管壁外面是否有烟冒出来,以便检验管道是否有缝隙.这一测试显然比较初级,更深层一点的测试至少要进行渗油测试 ...
- angularjs model.service vs provider vs factory?
<!DOCTYPE html> <html ng-app="app"> <head> <script src="http://c ...
- Windows API 之 CreateToolhelp32Snapshot
CreateToolhelp32Snapshot: 参考: https://msdn.microsoft.com/en-us/library/ms682489%28VS.85%29.aspx HAND ...
- Java的SSH框架
SSH 为 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架. 1.业务流程 集成SSH框架的系统从职责上分为四层:表示层.业务逻辑层.数据 ...