hdu3874-Necklace 线段树+离散化

本文介绍了一种使用线段树数据结构解决特定项链价值计算问题的方法。该问题涉及计算项链中连续部分的总价值,需确保相同价值的珠子仅被计算一次。文章详细阐述了解题思路,并提供了完整的AC代码。

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

Necklace

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


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
题意:有一串项链,项链上的每一颗珍珠都有一定的价值,看起来一样的珍珠的价值是一样的,现在需要求从第i颗到第j颗珍珠的价值和,注意的是如果有相同的珍珠则只计算一次。
解题思路:如果按照普通的思路这道题会超时,在这里我们需要用到的是线段树求区间和以及离散化缩小数据规模,离散化的具体知识可以看我的博客: 点击打开链接http://blog.youkuaiyun.com/wang_heng199/article/details/75324299
ac代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson l,m,o<<1
#define rson m+1,r,o<<1|1
typedef __int64 ll;
int a[50010];
int n;
ll sum[50010<<2];
struct node{
    int l;
    int r;
    int i;
};

node q[200010];
int vis[1000010];
ll ans[200010];

void pushup(int o)
{
    sum[o]=sum[o<<1]+sum[o<<1|1];
}
void build()
{
    memset(sum,0,sizeof(sum));
}

void update(int l,int r,int o,int p,int x)
{
    if(l==r)
    {
        sum[o]+=x;
        return;
    }
    int m=l+r>>1;
    if(p<=m)update(lson,p,x);
    else update(rson,p,x);
    pushup(o);
}

ll query(int l,int r,int o,int L,int R)
{
    if(L<=l&&r<=R)return sum[o];

    int m=l+r>>1;
    ll ans=0;
    if(L<=m)ans+=query(lson,L,R);
    if(m<R)ans+=query(rson,L,R);
    return ans;
}

bool cmp(node a,node b)
{
    return a.r<b.r;
}

int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int i;

        scanf("%d",&n);
        for(i=0;i<n;i++)scanf("%d",&a[i]);

        build();


        int Q;

        scanf("%d",&Q);

        for(i=0;i<Q;i++){
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].i=i;
        }

        sort(q,q+Q,cmp);
        int ll=0;
        memset(vis,-1,sizeof(vis));
        for(i=0;i<Q;i++)
        {
            while(ll<=q[i].r-1)
            {
                if(vis[a[ll]]!=-1)update(0,n-1,1,vis[a[ll]],-a[ll]);

                vis[a[ll]]=ll;

                update(0,n-1,1,ll,a[ll]);
                ll++;
            }
            ans[q[i].i]=query(0,n-1,1,q[i].l-1,q[i].r-1);
        }

        for(i=0;i<Q;i++)printf("%I64d\n",ans[i]);
    }

    return 0;
}

题目链接: 点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=3874

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值