Necklace (线段树单点更新+区间查询+离线操作)
2014-08-13 20:02
344 查看
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
Sample Output
按区间的右端点排序,离线查询各个区间,在查询之前 删除扫到的点的前一个位置出现的点。
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
按区间的右端点排序,离线查询各个区间,在查询之前 删除扫到的点的前一个位置出现的点。
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <iostream> #include <string> #include <map> #include <stack> #define lson o<<1, l, m #define rson o<<1|1, m+1, r using namespace std; typedef long long LL; const int maxn = 50005; const int MAX = 0x3f3f3f3f; const int mod = 1000000007; int t, n, m, A, B, in[maxn], last[maxn], pre[1000005]; //last记录每个点的前一个相同的点的位置,不存在记录为-1. LL sum[maxn<<2], ans[200005]; struct C { int l, r, num; }a[200005]; bool cmp (C x, C y) { return x.r < y.r; } void up(int o) { sum[o] = sum[o<<1] + sum[o<<1|1]; } void build(int o, int l, int r) { if(l == r) sum[o] = in[l]; else { int m = (l+r) >> 1; build(lson); build(rson); up(o); } } void update(int o, int l, int r) { if(l == r) { sum[o] = 0; return ; } int m = (l+r) >> 1; if(A <= m) update(lson); else update(rson); up(o); } LL query(int o, int l, int r) { if(A <= l && r <= B) return sum[o]; int m = (l+r) >> 1; LL res = 0; if(A <= m) res += query(lson); if(m < B ) res += query(rson); return res; } void Ini() { memset(pre, -1, sizeof(pre)); memset(last, -1, sizeof(last)); scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &in[i]); if(pre[ in[i] ] != -1) { last[i] = pre[ in[i] ]; pre[ in[i] ] = i; } else pre[ in[i] ] = i; } } int main() { scanf("%d", &t); while (t--) { Ini(); scanf("%d", &m); for(int i = 1; i <= m; i++) { scanf("%d%d", &a[i].l, &a[i].r); a[i].num = i; } sort(a+1, a+m+1, cmp); build(1, 1, n); int fr = 1; for(int i =1; i <= m; i++) { for(int j = fr; j <= a[i].r; j++) if(last[j] != -1) { A = last[j]; update(1, 1, n); } A = a[i].l; B = a[i].r; ans[ a[i].num ] = query(1, 1, n); fr = a[i].r+1; } for(int i = 1; i <= m; i++) printf("%I64d\n", ans[i]); } return 0; }
相关文章推荐
- HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)
- 蓝桥 操作格子 线段树 单点更新 区间查询
- 线段树基础操作--单点or区间更新+查询
- HDOJ 3874 Necklace 线段树 单点更新 成段查询
- HDU 2795 线段树(单点更新 区间查询)
- Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)
- HDU 3308 线段树 最长连续上升子序列 单点更新 区间查询
- 线段树(堆式)[单点更新, 区间查询]
- 【HDU4893Wow! Such Sequence!】线段树-单点+区间更新、区间查询(再次体会两种写法)
- Uva 12299 线段树求区间最小值(RMQ) 区间查询单点更新
- HDU 1540——Tunnel Warfare(线段树,区间合并+单点更新+单点查询)
- HDU 4267 线段树 离散点区间更新, 自叶子节点至根单点查询
- HDU 3308 线段树 最长连续上升子序列 单点更新 区间查询
- POJ 3264 Balanced Lineup (线段树单点更新 区间查询)
- POJ 3264-Balanced Lineup(线段树:单点更新,区间查询)
- HDU1754线段树单点更新区间查询(数组版)
- hdu 3874 Necklace 线段树单点更新区间求和
- 线段树 HDU 4217 Data Structure? 单点更新 区间查询
- HDOJ-2795 Billboard [线段树][单点更新+单点查询+维护区间最值]
- HDOJ-2795 Billboard [线段树][单点更新+单点查询+维护区间最值]