【HDU
来源:互联网 发布:苹果mac如何卸载软件 编辑:程序博客网 时间:2024/05/22 00:34
B - Necklace
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 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 intervalsL,R L,R (1<=L<=R<=N) and you must tell her F(L,R) of them.
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
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.
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
3714136
题意:有n颗珠子,每颗珠子都有其价值,现在给你m次询问,计算区间内的珠子价值为多少,且相同价值的珠子只能被计算一次。
分析:很明显的一道线段树,不同的是每个相同的值只能计算一次。我们可以先记录问题,并按照区间右端点从小到大排序,这样在后面计算问题的答案时便于删除区间中相同的值,而不对其他问题产生影响。用map记录相同价值的值和其所在的位置。
代码如下:
#include <set>#include <map>#include <queue>#include <cmath>#include <vector>#include <cctype>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define lson l, m, rt << 1#define rson m+1, r, rt << 1|1#define mod 835672545#define INF 0x3f3f3f3f#define LL long longusing namespace std;const int N = 50005;const int MX = 200005;int n, m;LL a[N], ans[MX];struct node{ int index, l, r;}q[MX];struct tree{ int l, r; LL w;}T[N<<2];void build(int l, int r, int rt){ T[rt].l = l; T[rt].r = r; T[rt].w = 0; if(l == r) return ; int m = (l + r) >> 1; build(l, m, rt << 1); build(m+1, r, rt << 1|1);}void update(int p, LL v, int rt){ T[rt].w += v; if(T[rt].l == p && T[rt].r == p) return ; int m = (T[rt].l + T[rt].r) >> 1; if(p <= m) update(p, v, rt << 1); else update(p, v, rt << 1|1);}LL query(int l, int r, int rt){ if(l <= T[rt].l && T[rt].r <= r){ return T[rt].w; } LL ret = 0; int m = (T[rt].l + T[rt].r) >> 1; if(l <= m) ret += query(l, r, rt << 1); if(r > m) ret += query(l, r, rt << 1|1); return ret;}bool cmp(node a, node b){ return a.r < b.r;}map<LL, int> mp;int main(){ int t; scanf("%d", &t); while(t--){ mp.clear(); scanf("%d", &n); for(int i = 1; i <= n; i++){ scanf("%lld", &a[i]); } build(1, n, 1); scanf("%d", &m); for(int i = 1; i <= m; i++){ scanf("%d%d", &q[i].l, &q[i].r); q[i].index = i; } sort(q+1, q+m+1, cmp); int id = 1; for(int i = 1; i <= n; i++){ update(i, a[i], 1); if(mp[a[i]]) update(mp[a[i]], -a[i], 1); mp[a[i]] = i; while(id <= m && q[id].r == i){ ans[q[id].index] = query(q[id].l, q[id].r, 1); id++; } } for(int i = 1; i <= m; i++){ printf("%lld\n", ans[i]); } } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 嵌入式软件工程师面试题
- 移动端滚动穿透的问题解决
- 关于正则表达式的一道面试题
- 什么是副作用(Side Effect)
- [A-frame文档学习笔记]HTML&Primitives
- 【HDU
- 关于Spring MVC的一个经验
- web基础知识回顾
- java代码混淆工具Jocky的介绍
- 【HDU-1234】 开门人与关门人
- How to Install TestLink on CentOS 7
- AtCoder GC 018A: Getting Difference 题解
- Fox and Card Game CodeForces
- linux SVN路径带用户名拉取