博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU - 3874 Necklace (线段树 + 离线处理)
阅读量:5917 次
发布时间:2019-06-19

本文共 3979 字,大约阅读时间需要 13 分钟。

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

转载于:https://www.cnblogs.com/yutingliuyl/p/7242912.html

你可能感兴趣的文章
saltstack之(二)软件包下载安装
查看>>
Jquery数字转盘:
查看>>
LOJ#2553 暴力写挂
查看>>
Leetcode | Length of Last Word
查看>>
MySQL Connector/Python 接口 (一)
查看>>
MyBatis 逆向工程
查看>>
页面常用布局
查看>>
Python 转义字符
查看>>
数论 - Moon Game
查看>>
Mysql数据备份和还原
查看>>
百度编辑器 Ueditor
查看>>
一个编程菜鸟的进阶之路(C/C++)
查看>>
打印出 对象的所有属性和方法
查看>>
小程序分享朋友圈
查看>>
nginx下重写隐藏index.php文件
查看>>
0502 php-变量、常量
查看>>
重写document.write实现无阻塞加载js广告
查看>>
步步为营 C# 技术漫谈 一、反射机制
查看>>
格式化数字、日期使用哪个标签库
查看>>
安卓 如何向数据库传值
查看>>