Necklace

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

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
 
Source
 
Recommend
lcy
 
  1. /*
  2. 题意为查找区间去重后的和
  3. 用树状数组离线处理
  4. 将所有查询以右端点从小到大排序
  5. 按此顺序边去重边查询
  6. 前面的去重就不会影响到后面的结果了
  7. */
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int N=;
  9. const int M=;
  10.  
  11. struct node{
  12. int l,r;
  13. int id;
  14. }q[M];
  15.  
  16. int n,m,val[N],pre[N],loc[];
  17. long long arr[N],res[M];
  18.  
  19. int lowbit(int x){
  20. return x&(-x);
  21. }
  22.  
  23. void update(int i,int x){
  24. while(i<=n){
  25. arr[i]+=x;
  26. i+=lowbit(i);
  27. }
  28. }
  29.  
  30. long long Sum(int i){
  31. long long ans=;
  32. while(i>){
  33. ans+=arr[i];
  34. i-=lowbit(i);
  35. }
  36. return ans;
  37. }
  38.  
  39. bool cmp(node a,node b){
  40. return a.r<b.r;
  41. }
  42.  
  43. int main(){
  44.  
  45. //freopen("input.txt","r",stdin);
  46.  
  47. int t;
  48. scanf("%d",&t);
  49. while(t--){
  50. memset(arr,,sizeof(arr));
  51. memset(loc,-,sizeof(loc));
  52. scanf("%d",&n);
  53. for(int i=;i<=n;i++){
  54. scanf("%d",&val[i]);
  55. pre[i]=loc[val[i]];
  56. loc[val[i]]=i;
  57. update(i,val[i]);
  58. }
  59. scanf("%d",&m);
  60. for(int i=;i<=m;i++){
  61. scanf("%d%d",&q[i].l,&q[i].r);
  62. q[i].id=i;
  63. }
  64. sort(q+,q++m,cmp);
  65. int r=;
  66. for(int i=;i<=m;i++){
  67. for(int j=r+;j<=q[i].r;j++)
  68. if(pre[j]!=-)
  69. update(pre[j],-val[j]);
  70. r=q[i].r;
  71. res[q[i].id]=Sum(q[i].r)-Sum(q[i].l-);
  72. }
  73. for(int i=;i<=m;i++)
  74. printf("%I64d\n",res[i]);
  75. }
  76. return ;
  77. }

线段树:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<map>
  6.  
  7. using namespace std;
  8.  
  9. const int N=;
  10.  
  11. //#define L(rt) (rt<<1)
  12. //#define R(rt) (rt<<1|1)
  13.  
  14. #define lson l,mid,rt<<1
  15. #define rson mid+1,r,rt<<1|1
  16.  
  17. struct Tree{
  18. int l,r;
  19. int id;
  20. }q[N<<];
  21.  
  22. map<int,int> mp;
  23. int n,m,a[N];
  24. long long sum[N<<],res[N<<];
  25.  
  26. void PushUp(int rt){
  27. sum[rt]=sum[rt<<]+sum[rt<<|];
  28. }
  29.  
  30. void update(int id,int val,int l,int r,int rt){
  31. if(l==r){
  32. sum[rt]+=val;
  33. return ;
  34. }
  35. int mid=(l+r)>>;
  36. if(id<=mid)
  37. update(id,val,lson);
  38. else
  39. update(id,val,rson);
  40. PushUp(rt);
  41. }
  42.  
  43. long long query(int L,int R,int l,int r,int rt){
  44. if(L<=l && R>=r)
  45. return sum[rt];
  46. int mid=(l+r)>>;
  47. long long ans=;
  48. if(L<=mid)
  49. ans+=query(L,R,lson);
  50. if(R>mid)
  51. ans+=query(L,R,rson);
  52. return ans;
  53. }
  54.  
  55. int cmp(Tree a,Tree b){
  56. return a.r<b.r;
  57. }
  58.  
  59. int main(){
  60.  
  61. //freopen("input.txt","r",stdin);
  62.  
  63. int t;
  64. scanf("%d",&t);
  65. while(t--){
  66. scanf("%d",&n);
  67. for(int i=;i<=n;i++)
  68. scanf("%d",&a[i]);
  69. scanf("%d",&m);
  70. for(int i=;i<=m;i++){
  71. scanf("%d%d",&q[i].l,&q[i].r);
  72. q[i].id=i;
  73. }
  74. sort(q+,q++m,cmp);
  75. mp.clear();
  76. memset(sum,,sizeof(sum));
  77. int r=;
  78. for(int i=;i<=m;i++){
  79. for(int j=r+;j<=q[i].r;j++){
  80. if(mp[a[j]])
  81. update(mp[a[j]],-a[j],,n,);
  82. update(j,a[j],,n,);
  83. mp[a[j]]=j;
  84. r=q[i].r;
  85. }
  86. res[q[i].id]=query(q[i].l,q[i].r,,n,);
  87. }
  88. for(int i=;i<=m;i++)
  89. printf("%I64d\n",res[i]);
  90. }
  91. return ;
  92. }

HDU 3874 Necklace (树状数组 | 线段树 的离线处理)的更多相关文章

  1. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  2. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  3. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  4. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  7. hdu 1166 敌兵布阵——(区间和)树状数组/线段树

    pid=1166">here:http://acm.hdu.edu.cn/showproblem.php?pid=1166 Input 第一行一个整数T.表示有T组数据. 每组数据第一 ...

  8. HDU 1166 敌兵布阵 树状数组||线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 题目大意: 给定n个数的区间N<=50000,还有Q个询问(Q<=40000)求区间和. 每个 ...

  9. HDU 3303 Harmony Forever 前缀和+树状数组||线段树

    Problem Description We believe that every inhabitant of this universe eventually will find a way to ...

  10. hdu 1166 树状数组(线段树)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 安卓奇葩问题之:设置webView超时

    我只想说:what a fucking day! 今天要做一个webView的超时功能,于是开始百度,一看貌似很简单啊,于是开始copy了下面的代码. import java.util.Timer; ...

  2. zabbix监控tomcat

    服务端配置修改 编译zabbix的时候需要添加参数--enable-java --enable-java 修改zabbix_java配置文件 vim /usr/local/zabbix-2.4.6/s ...

  3. [UE4]武器碰撞

    实现武器战斗伤害系统,击中时如何发出碰撞事件产生伤害,目前探索的有通过物理碰撞和LineTrace两种方法. 物理碰撞通过Overlap事件的方法,优点是易于实现,缺点是无法具体到碰撞骨骼位置,低帧数 ...

  4. 那些年我们学过的PHP黑魔法

    那些年我们学过的PHP黑魔法 提交 我的评论 加载中 已评论 那些年我们学过的PHP黑魔法 2015-04-10 Sco4x0 红客联盟 红客联盟 红客联盟 微信号 cnhonker_huc 功能介绍 ...

  5. centos下网络配置方法(网关、dns、ip地址配置)

    本文介绍了centos网络配置的方法,centos网络配置主要包括dns.网关.IP地址: 1.IP地址配置: /etc/sysconfig/network-scripts/ifcfg-eth0 2. ...

  6. ASP.NET WEB API 中的路由调试与执行过程跟踪

    路由调试 RouteDebugger 是调试 ASP.NET MVC 路由的一个好的工具,在ASP.NET WEB API中相应的有 WebApiRouteDebugger ,Nuget安装 Inst ...

  7. 设计模式之美:Builder(生成器)

    索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Builder 为每个构件定义一个操作. 实现方式(二):Builder 将构件返回给 Director,Director 将构 ...

  8. input text 的事件及方法

    事件 描述onactivate 当对象设置为活动元素时触发.onafterupdate 当成功更新数据源对象中的关联对象后在数据绑定对象上触发.onbeforeactivate 对象要被设置为当前元素 ...

  9. Book Review: PowerShell 3.0 Advanced Administration Handbook

    Recently I read a book, PowerShell 3.0 Advanced Administration Handbook, which I found really worthy ...

  10. Microsoft Azure开发体验 – 网络报名系统

    去年底RP好抢到了中国版Azure的使用机会,最近社团里讨论到9月份招新的问题,就用Azure Website和Azure Table Storage打造了这个报名系统. 网站放在 http://jo ...