D. Necklace

Time Limit: 5000ms
Memory Limit: 32768KB

64-bit integer IO format: %I64d      Java class name: Main

 
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

解题:离线树状数组,为什么要把y坐标从小到大进行排序呢?因为树状数组的特性所致,右边的节点可能包含左边的节点的值,所以从左往右,不断去掉重复的数值更简便。

离线处理即先一次性把所有询问存储起来。最后一次性回复。用一个数组pre[i]记录元素d[i]距离i最近的一次出现的下标。pre[i] == -1即表示该元素d[i]目前是唯一的,不存在重复元素的。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct query {
int x,y,id;
} q[maxn];
LL tree[];
int pre[],loc[],n,m,d[];
LL ans[maxn];
bool cmp(const query &a,const query &b) {
return a.y < b.y;
}
int lowbit(int x) {
return x&(-x);
}
void update(int x,int val) {
for(; x <= n; x += lowbit(x)) {
tree[x] += val;
}
}
LL sum(int x) {
LL ans = ;
for(; x; x -= lowbit(x))
ans += tree[x];
return ans;
}
int main() {
int t,i,j;
scanf("%d",&t);
while(t--) {
memset(loc,-,sizeof(loc));
memset(tree,,sizeof(tree));
scanf("%d",&n);
for(i = ; i <= n; i++) {
scanf("%d",d+i);
pre[i] = loc[d[i]];
loc[d[i]] = i;
update(i,d[i]);
}
scanf("%d",&m);
for(i = ; i <= m; i++) {
scanf("%d %d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q+,q+m+,cmp);
int y = ;
for(i = ; i <= m; i++) {
for(j = y+; j <= q[i].y; j++) {
if(pre[j] != -) update(pre[j],-d[j]);
}
y = q[i].y;
ans[q[i].id] = sum(q[i].y) - sum(q[i].x-);
}
for(i = ; i <= m; i++) {
printf("%I64d\n",ans[i]);
}
}
return ;
}

最新文章

  1. Java基础知识梳理《一》
  2. DataRow循环取出
  3. PHP 设计模式 笔记与总结(4)PHP 链式操作的实现
  4. .Net 两个对像之间的映射
  5. about家庭智能设备部分硬件模块功能共享【协同工作】solution
  6. github 查看单个文件的历史记录命令
  7. 作品第一课----获取批量checkbox选中的值
  8. 系统的启动模式(启动级别)的改动---使用upstart启动机制的
  9. 关于bootstrap--导航栏
  10. Android切换页面效果的实现一:WebView+ViewFlipper
  11. 使用Java中间MessageDigest该文本MD5加密(Java中间MD5样品加密算法演示)
  12. Day4-生成器generator
  13. Git详细教程(2)---多人协作开发
  14. ARC时代的内存管理
  15. Django升级1.8的一些问题
  16. SSM-SpringMVC-16:SpringMVC中小论注解式开发之访问方式篇
  17. nodejs中使用crypto-js先HmacSha1加密后转Base64
  18. car的旅行路线
  19. 最全的MonkeyRunner自动化测试从入门到精通(5)
  20. MNIST数据可视化

热门文章

  1. html的meta总结
  2. Android adb命令,linux中各种命令
  3. MySQL存储过程(批量生成论坛中发帖、回帖、主题等数据)
  4. MFC U盘检测
  5. Caused by: java.lang.NoClassDefFoundError: com/sun/tools/javac/util/List at
  6. Assertion failure layoutSublayersOfLayer:], /SourceCache
  7. 通过例子理解 k8s 架构【转】
  8. faster rcnn需要理解的地方
  9. C++11:移动构造函数的测试
  10. 几句话总结一个算法之RNN、LSTM和GRU