HDU3874 Necklace(树状数组)

本文介绍了一个关于项链问题的算法解决方案,旨在解决从连续部分中计算唯一美丽值的问题。通过使用预处理技术和有效的数据结构,文章详细说明了如何快速响应查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4241    Accepted Submission(s): 1426


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
 

Source
思路:先用数组记录下每个值最初出现的位置,然后作预处理,把询问顺序按右端点从小到大排列。
设置点lastR = 1,对于每个询问,先遍历lastR~ask[i].r,当发现出现的数值与前面所记录的该数值最早位置不一样时候,把最早位置上的数值删除,并更新最早位置。这样就可以保证每个访问区间内没有重复的数值。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 50050
#define ll long long int
using namespace std;
struct Ask{
    int l, r, index;
}ask[4*maxn];
int N, last[20*maxn], v[maxn], M;
ll ans[4*maxn], bit[maxn];
bool cmp(Ask a, Ask b){
    return a.r < b.r;
}
ll sum(int i){
    ll s = 0;
    while(i > 0){
        s += bit[i];
        i -= i & -i;
    }
    return s;
}
void add(int i, int x){
    while(i <= N){
        bit[i] += x;
        i += i & -i;
    }
}
int main()
{
    int T, i, j, lastR;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &N);
        memset(last, 0, sizeof last);
        memset(bit, 0, sizeof bit);
        for(int i = 1;i <= N;i++){
            scanf("%d", &v[i]);
            add(i, v[i]);
            if(!last[v[i]]) last[v[i]] = i;
        }
        scanf("%d", &M);
        for(i = 1;i <= M;i++){
            scanf("%d %d", &ask[i].l, &ask[i].r);
            ask[i].index = i;
        }
        sort(ask+1, ask+M+1, cmp);
        lastR = 1;
        for(i = 1;i <= M;i++){
            for(j = lastR;j <= ask[i].r;j++){
                if(last[v[j]] != j){
                    add(last[v[j]], -v[j]);
                    last[v[j]] = j;
                }
            }
            lastR = ask[i].r;
            ans[ask[i].index] = sum(ask[i].r) - sum(ask[i].l-1);
        }
        for(i = 1;i <= M;i++)
            printf("%I64d\n", ans[i]);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值