莫队算法。

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

  1. HDU 3874 Necklace 区间查询的离线操作

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=3874 对需要查询的区间按右端点排序,然后从左到右依次加入序列中的元素,同时更新,更新的方法是,把上一次出 ...

  2. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. hdu 3874 Necklace(bit树+事先对查询区间右端点排序)

    Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful v ...

  4. HDU - 3874 Necklace (树状数组、离线处理)

    题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...

  5. HDU - 3874 Necklace (线段树 + 离线处理)

    欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/3 ...

  6. hdu 3874 Necklace(线段树)

    这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...

  7. HDU 3874 Necklace 树状数组

    题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...

  8. Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

    Necklace HDU - 3874  Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...

  9. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

随机推荐

  1. s7-300 第9讲 定时器

  2. wlcore: firmware chunk too long

    insmod wlcore_sdio.ko 的时候出现的错误 43.767890] Powering on wl12xx [  143.846003] Powering off wl12xx [  1 ...

  3. JQuery获取input type="text"中的值的各种方式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. LVS + keepalived + tomcat负载均衡及高可用实现(初级)

    1.首先检测Linux服务器是否支持ipvs 执行如下命令:modprobe -l|grep ipvs 输出: kernel/net/netfilter/ipvs/ip_vs.ko kernel/ne ...

  5. Coupons and Discounts

    Coupons and Discounts time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. NullPointerException

    if(userName.equals("zhansan")){}   可能会报空指针异常

  7. 新建aix实例

    http://www.cnblogs.com/kfarvid/archive/2010/12/21/1912553.html   DB2数据库 http://wenku.baidu.com/view/ ...

  8. nagios总结

    主要功能 网络服务监控(SMTP.POP3.HTTP.NNTP.ICMP.SNMP.FTP.SSH) 主机资源监控(CPU load.disk usage.system logs),也包括Window ...

  9. HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

    Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  10. Bootstrap学习 - JavaScript插件

     模态框 <div class="modal" id="myModal" tabindex="-1" role="dialo ...