您的位置:首页 > 其它

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

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: