Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4003    Accepted Submission(s): 1330

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
对于这道题。大家能够看我博客里http://blog.csdn.net/qq_18661257/article/details/47419441提供的离线处理教程后,基本能够理解离线处理的机制了,然后我们就要对比理解题目。不能出现反复的数字,所以能够离线最右边的值,将前面的值一一删除就能够得到正确答案,当然,大家还需注意的是数值取值范围为long long ,我就是在这个上面看了接近一个小时,query()函数的返回值应该也是long long 。坑爹的地方就是这里了,其它的,大家看代码基本能够秒懂的

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define root 1, 1, N
const int MAXN = 5e4 + 5;
const int MAXM = 2e5 + 5;
const int MAXS = 1e6 + 5;
int N, M, T, pre[MAXS];
LL Ans[MAXM],Sum[MAXN << 2]; struct node {
int l, r, id;
bool operator < (const node & object) const {
return r < object.r;
}
} Node[MAXM]; void pushup(int rt) {
Sum[rt] = Sum[rt << 1] + Sum[rt << 1|1];
} void build(int rt,int l,int r) {
if(l == r) {
scanf("%I64d", &Sum[rt]);
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int rt, int l, int r) {
if(l == r) {
Sum[rt] = 0;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p, lson);
else update(p, rson);
pushup(rt);
} LL query(int L, int R, int rt, int l, int r) {
if(L <= l && r <= R) {
return Sum[rt];
}
int mid = (l + r) >> 1;
LL ret = 0;
if(L <= mid) ret += query(L, R, lson);
if(R > mid) ret += query(L, R, rson);
return ret;
} int main() {
//freopen("D://imput.txt","r",stdin);
scanf("%d", &T);
while(T --) {
scanf("%d", &N);
build(root);
scanf("%d", &M);
for(int i = 1 ; i <= M ; i ++) {
scanf("%d %d", &Node[i].l, &Node[i].r);
Node[i].id = i;
}
memset(pre, -1, sizeof(pre));
sort(Node + 1, Node + M + 1);
for(int i = 1,j = 1; i <= N; i++) {
int tmp = query(i, i, root);
if(tmp != 0 && pre[tmp] != -1) {//假设前面存在反复的数字则删除他
update(pre[tmp],root);
}
pre[tmp] = i;
while(j <= M && Node[j].r == i) {//假设右边的值等于当前的值,则进行求和,大家能够參考我博客里的教程
Ans[Node[j].id] = query(Node[j].l, Node[j].r, root);
j ++;
}
}
for(int i = 1; i <= M ; i ++) {
printf("%I64d\n", Ans[i]);
}
}
return 0;
}


 

HDU - 3874 Necklace (线段树 + 离线处理)的更多相关文章

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

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

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

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

  3. HDU 4638-Group(线段树+离线处理)

    题意: 给n个编号,m个查询每个查询l,r,求下标区间[l,r]中能分成标号连续的组数(一组内的标号是连续的) 分析: 我们认为初始,每个标号为一个组(线段树维护区间组数),从左向右扫序列,当前标号, ...

  4. HDU 4417 【线段树+离线处理】

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意:找出给定区间内,有多少个数小于等于给定的数.用线段树维护的话会超时,要用到线段树的离线操作,对询问与 ...

  5. hdu 4288 Coder (线段树+离线)

    题意: 刚开始有一个空集合.有三种操作: 1.往集合中加入一个集合中不存在的数 x 2.从集合中删除一个已经存在的数 x 3.计算集合的digest sum并输出.       digest sum求 ...

  6. HDU-3874 Necklace 线段树+离线

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 比较简单的题,题意也好懂. 先O(n)求每个数左边第一次出现的与他相同的数的位置l[i].对询问 ...

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

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

  8. HDU 3874 离线段树

    在所有数字的统计范围,,对于重复统计只有一次 离线段树算法 排序终点坐标.然后再扫,反复交锋.把之前插入树行被删除 #include "stdio.h" #include &quo ...

  9. HDU3874 线段树 + 离线处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 , 线段树(或树状数组) + 离线处理 下午做了第一道离线处理的题目(HDU4417),多少有点 ...

随机推荐

  1. Angularjs的$apply及其优化使用

    今天,我们要聊得是Angularjs中的小明星$apply.当我们数据更新了,但是view层却没反应时,总能听到有人说,用apply吧,然后,懵懂无知的我们,在赋值代码后面加了$scope.$appl ...

  2. css iframe边框去掉

    [IE6以下] iframe边框通过css设定在FF下正常在ie下却还存在边框,通过在iframe标签内部设置属性 frameborder="no" border="0& ...

  3. 关于设置组件的state时遇到的一些问题

    在使用react-bootstrap的时候设置showModel的值来控制Model的显示与隐藏,但是setState这个函数是异步的. 当你进行数据更新的时候,接着执行函数获取这个模态框的dom是获 ...

  4. iOS多线程:『GCD』详尽总结 ---(转)

    文章:https://bujige.net/blog/iOS-Complete-learning-GCD.html 文中 Demo 我已放在了 Github 上,Demo 链接:https://git ...

  5. 博客移至CSDN

    CSDN博客地址:http://blog.csdn.net/pilihaotian 博客园看心情更新.

  6. 1180: [CROATIAN2009]OTOCI

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1032  Solved: 638[Submit][ ...

  7. 【HDOJ5556】Land of Farms(最大团)

    题意:给定n*m的网格图,上面只有字符'.' 和 数字0-9.其中数字表示这是该格是古老的土地,字符'.'表示该格只是普通的土地. 可以认为一块古老的农田由四联通的所有数字相同的格组成的块,一块普通的 ...

  8. [LeetCode] 4Sum hash表

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  9. 查询/删除/建立DB2数据表的主键

    一.查询表主键. describe indexes for table <instancename>.<tablename> 例: describe indexes for t ...

  10. Android布局实现阴影效果

    最近某个模块的UI,设计想要卡片式阴影效果.之前查阅过资料,用传统的xml方式作为布局的background <?xml version="1.0" encoding=&qu ...