Necklace (线段树单点更新+区间查询+离线操作)
来源:互联网 发布:唱歌的软件 编辑:程序博客网 时间:2024/05/16 05:11
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.
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.
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
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
Sample Output
3714136
按区间的右端点排序,离线查询各个区间,在查询之前 删除扫到的点的前一个位置出现的点。
#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; }
0 0
- Necklace (线段树单点更新+区间查询+离线操作)
- HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)
- HDU 3874 Necklace(线段树啊 单点更新 区间求和)
- 线段树基础操作--单点or区间更新+查询
- HDOJ 3874 Necklace 线段树 单点更新 成段查询
- zoj (单点更新区间查询:线段树)
- 线段树单点更新和区间查询
- hdu1166(线段树单点更新区间查询)
- 线段树(堆式)[单点更新, 区间查询]
- HDOJ3016Man Down(线段树(区间更新,单点查询)+DP)
- HDU1754线段树单点更新区间查询(数组版)
- hdu 4046 Panda (线段树 单点更新 区间查询)
- Flipping Parentheses (线段树 单点更新 区间查询)
- 线段树(一)单点更新,区间查询
- hdu5239 Doom(线段树单点更新区间查询+数论)
- HDU1166 敌兵布阵(线段树 单点更新 区间查询)
- 线段树版(单点更新,区间查询)
- HDU 1754 (线段树+单点更新+区间查询)
- Qt 中文显示
- HDU-#1879 继续畅通工程(Prim & Kruskal)
- U盘修复技巧大全
- 自制QQ 数据库SQL语句 代码
- Jquery获取select选中的option的文本信息
- Necklace (线段树单点更新+区间查询+离线操作)
- LeetCode-Permutation Sequence
- 2014.08.13总结
- AC自动机
- optimizer_index_cost_adj和optimizer_index_caching
- uitableView 选择跳转后 再跳回来 颜色不变 问题
- ios客户端发现_华为爱奇艺手机活动开发总结
- poj 2907 Collecting Beepers dfs
- cmake 常用变量和常用环境变量查表手册---整理 .