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

261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
 
Sample Output

3714136


思路:

树状数组对于维护数组有很大优势,复杂度O(M*lgN)。树状数组讲解

我们用树状数组维护题中的数组,但是他要求不能计算重复的点,这里用了一个很巧妙的方法:先将问题按照最右边界从小到大排序,然后对这个数组去重,最后我们记录当前问题的答案,再做下一个问题,这样我们就能保证每个问题都去重并且没有删去元素。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=50005;
const int MAX=200005;
const int INF=1e9;
using namespace std;
map<int,int> mp;
int num[N],n;
ll ans[MAX],tree[N];
struct node{
int l,r,no;
}que[MAX];
int cmp(node a,node b){
if(a.r<b.r) return 1;
return 0;
} //树状数组操作-start
int lowbit(int x){
return x&(-x);
}
void update(int x,int val){ //更新
for(int i=x;i<=n;i+=lowbit(i)){
tree[i]+=val;
}
}
ll sum(int x){ //计算1~x的和
ll count=0;
for(int i=x;i>=1;i-=lowbit(i)){
count+=tree[i];
}
return count;
}
//树状数组操作-end int main(){
int T,m;
cin>>T;
while(T--){
memset(tree,0,sizeof(tree));
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&num[i]);
}
cin>>m;
for(int i=1;i<=m;i++){
scanf("%d%d",&que[i].l,&que[i].r);
que[i].no=i; //表示这是第几个问题
}
sort(que+1,que+m+1,cmp);
mp.clear(); //map存储num[i]的最新位置
int pos=1;
for(int i=1;i<=m;i++){
while(pos<=que[i].r){
if(mp[num[pos]]!=0){ //如果重复出现
update(mp[num[pos]],-num[pos]); //删掉之前那个
}
mp[num[pos]]=pos;
update(pos,num[pos]);
pos++;
}
ans[que[i].no]=sum(que[i].r)-sum(que[i].l-1);
}
for(int i=1;i<=m;i++){
printf("%lld\n",ans[i]);
}
}
return 0;
}

最新文章

  1. 枚举Enum
  2. webview 中 svg的坑
  3. 基于opencv的小波变换
  4. new Image()的用途
  5. 谈谈用SQLite和FMDB而不用Core Data
  6. 3000本IT书籍下载地址
  7. 使用Jquery后去div个数
  8. Java多线程使用场景
  9. JNI调用问题(部分机型崩溃)
  10. 限制容器的 Block IO - 每天5分钟玩转 Docker 容器技术(29)
  11. java基础--封装
  12. Android开发学习之路--Activity之四种启动模式
  13. Monte Carlo Method(蒙特&#183;卡罗方法)
  14. ICE的异步方法调用
  15. 搭建RESTful API来使用Fabric Node SDK 开篇
  16. 关于for,while,dowhile效率测试
  17. CentOS 7卸载Docker
  18. Java知多少(53)使用Java创建自己的异常子类
  19. HDU 6214 Smallest Minimum Cut(最少边最小割)
  20. scrapy之 downloader middleware

热门文章

  1. Shell初学(三)传参
  2. JavaScript中通过arguments对象实现对象的重载
  3. golang使用vet进行语法检查
  4. 高性能mysql 4 ,5章
  5. POJ1860:Currency Exchange(BF)
  6. 支持向量机:Numerical Optimization,SMO算法
  7. [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table
  8. [LeetCode] 697. Degree of an Array_Easy tag: Hash Table
  9. http协议基础(二)请求和响应报文的构成
  10. JS参差不齐的数组