题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3874

Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3929    Accepted Submission(s): 1296

Problem Description
Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6.

Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.

Input
The first line is T(T<=10), representing the number of test cases.
  For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
Output
For each query, output a line contains an integer number, representing the result of the query.
Sample Input
2 6 1 2 3 4 3 5 3 1 2 3 5 2 6 6 1 1 1 2 3 5 3 1 1 2 4 3 5
Sample Output
3 7 14 1 3 6
题意: 求区间不重复元素和
题解: 询问是5个0 所以肯定要找一种可以扫描一次得出结果不会重复计算的算法,这里介绍一种巧妙的方法: 离线操作,所谓离线就是将所有的询问都读如后,根据需要进行排序,这里一定要记录一个id来保存输入的顺序,然后一个ans数组按照输入顺序储存答案,然后从左到右的扫描一边将所有的以当前扫描的点为终止点的答案都保存起来,最后按顺序输出ans 数组即可
对于这个题:一般按照右端点排序,因为右端点代表这一个查询的结束,所以按右端点排序有特殊的意义,因为每次要去除重复的数字,可以考虑设一个last数组,标记其在之前是否出现过,如果出现过的化,将之前出现的地方的这个值更改成0 然后last数组的当前值更新成当前的位置,这样树状数组中存放的值就肯定没有重复元素了。
这种离线的思想很重要,一定要利用好将所有的数据排序后每次可以将相同的结尾的值一起算出来的性质,指针只用扫描一边,每向后扫描后都将以这个值结尾的所有结果都处理出来。离线最大的好处是在依次向后更新last数组的时候前面的与其相关的询问已经处理过了,所以去除前面的值,保留后面的不会影响前面的结果,也不会影响后面的计算。如果不离线,last 数组如果这样更新的话,会影响到再次涉及前面的区间的和
代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 #define N 1000005
 6 #define M 50005
 7 #define numq 200005
 8 #define ll long long
 9 int Last[N];
10 ll shusz[M];
11 int gaga[M];
12 ll ans[numq];
13 struct Q {
14     int l ;
15     int r;
16     int id;
17     bool operator < (const Q a) const
18     {
19         return r<a.r;
20     }
21 }qq[numq];
22 int lb(int i)
23 {
24     return i&(-i);
25 }
26 void add(int j , int t)
27 {
28     for(int i =j ;i < M ;i+=lb(i))
29     {
30         shusz[i]+=t;
31     }
32 }
33 ll sum (int x)
34 {
35     ll ans = 0 ;
36     for(int i = x ; i > 0 ; i-=lb(i))
37     {
38         ans+=shusz[i];
39     }
40     return ans;
41 }
42 ll sum(int x , int y)
43 {
44     ll ans = sum(y)-sum(x-1);//注意是x-1
45     return ans;
46 }
47 int main()
48 {
49     int T ;
50     scanf("%d",&T);
51     for(int i =0 ;i < T ; i++)
52     {
53         int n;
54         scanf("%d",&n);
55         int tm;
56         for(int j = 1 ; j <= n ; j++)
57         {
58             scanf("%d",&tm);
59             gaga[j] = tm;
60         }
61         int m ;
62         scanf("%d",&m);
63         for(int j = 1 ;j <= m ;j++)
64         {
65             int l , r ;
66             scanf("%d%d",&l,&r);
67             qq[j].l = l ;
68             qq[j].r = r;
69             qq[j].id = j;
70         }
71         memset(Last,-1,sizeof(Last));
72         memset(ans,0,sizeof(ans));
73         memset(shusz,0,sizeof(shusz));
74         sort(qq+1,qq+m+1);
75         int cur = 1;//记录扫描到第几个询问
76         for(int j = 1 ; j <= n ; j++)//扫描n个点
77         {
78             if(Last[gaga[j]] != -1)
79                 add(Last[gaga[j]], -gaga[j]);
80             Last[gaga[j]] = j;
81             add(j, gaga[j]);
82             while(j == qq[cur].r)
83             {
84                 ans[qq[cur].id] = sum(qq[cur].l, qq[cur].r);
85                 cur++;
86             }
87         }
88         for(int j = 1; j <= m; j++)
89             printf("%lld\n", ans[j]);
90     }
91     return 0 ;
92 }

转载于:https://www.cnblogs.com/shanyr/p/4725462.html

Necklace(树状数组+离线操作)相关推荐

  1. HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

  2. HDU 4630 No Pain No Game 树状数组+离线操作

    题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...

  3. Codeforces 369E Valera and Queries --树状数组+离线操作

    题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,"正难则反",我们试着求有多少 ...

  4. 8.8线段树和树状数组

    题目链接   http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28619#overview 密码 acmore 还是感觉不怎么会线段树,还是 ...

  5. 【树状数组】Bzoj1878[SDOI2009] HH的项链

    Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...

  6. hdu4417 Super Mario(树状数组+离线区间操作)

    题目链接 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jum ...

  7. HDU 3333HDU-3874 Necklace 离线树状数组

        这题是要求一段区间内的不重复的数字之和.我们通过对询问区间的右端点进行排序,然后记录每一数字的上一次的出现的位置,由于询问都是不回溯的那么就可以线性的更新了.      #include &l ...

  8. P1972 [SDOI2009]HH的项链(离线树状数组)

    整理的算法模板合集: ACM模板 #include<cstdio> #include<algorithm> #include<cstring> #include&l ...

  9. D. Multiset(树状数组 + 二分)

    Multiset 可能更好的阅读体验 思路 二分 + 树状数组做法 我们发现每个数的范围是$ <= 1e6$的,所以可以直接在线操作,不用离散化离线操作. 这个时候我们的treetreetree ...

最新文章

  1. 流氓网站5599.net修改ie主页分析
  2. Linux软件安装-----apache安装
  3. Spartan-6的时钟资源、全局时钟缓冲器/多路复用器
  4. mysql停止服务命令_0789不停止MySQL服务重做备库的方法
  5. onethink swfupload upload.php,OneThink--ThinkPHP3.2 缓存文件泄露
  6. (计算机组成原理)第五章中央处理器-第五节2:指令流水线影响因素和分类及多发技术
  7. 国土空间适宜性评价与承载力评价之间的逻辑关系是什么?
  8. office2010 反应慢_office2010打开时间太慢。怎么办?
  9. 柱状图如何添加数字标签_分类堆叠柱状图顺序排列及其添加合适条块标签
  10. 实用新型专利申请文件撰写示例
  11. 加速度传感器工作原理及应用
  12. 台式计算机组装攻略,台式机如何组装 电脑组装详细步骤【图文】
  13. PHP语言之MySQL操作
  14. 【课程·研】自然辩证法 | 课堂汇报:工程师的伦理规范
  15. 2022谈谈手机充电头/快充头/电源适配器
  16. 荣耀手机怎么使用鸿蒙系统,荣耀供应链6月全面恢复,安卓系统依然是第一选择,鸿蒙只是备选...
  17. 智慧树源码_公众号题库源码
  18. 如何扎实的学好ABAP?我的个人经验
  19. CPU 时间片轮转机制 (RR 调度)
  20. Spring Boot 实战(3)静态资源配置

热门文章

  1. 少一点张扬,多一点谦虚;少一点英雄主义,多一点实事求是——读2008第11期《IT经理世界》
  2. 语料库资源————(三)
  3. 台式电脑切换集成显卡和独立显卡
  4. csgo自建局域网服务器,CSGO(KZ模式)服务器搭建常见问题答疑
  5. PWNFEST黑客大会:苹果Safari与微软Edge浏览器均被攻破
  6. SuperMap iDesktop常见问题解答集锦(七)
  7. Linux:WCP知识库安装及配置
  8. docker日常操作
  9. 无聊的逗 蓝桥杯 python
  10. PS室内植物素材合成教程分享骞