1004(树状数组+离线操作+离散化)

本文介绍了一种解决区间内不重复数值之和的问题算法,采用离线操作和树状数组进行预处理,通过离散化及按右端点排序等步骤实现高效计算。

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

Problem Description
After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again...

Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.

Input
The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below. For each case, the input format will be like this: * Line 1: N (1 ≤ N ≤ 30,000). * Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000). * Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries. * Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).

Output
For each Query, print the sum of distinct values of the specified subsequence in one line.

Sample Input
2 3 1 1 4 2 1 2 2 3 5 1 1 2 1 3 3 1 5 2 4 3 5

Sample Output
1 5 6 3 6

题目大概和思路:

求不重合数的和。用离线操作。
首先要进行离散化。还要把区间存起来,按照右端点排序。进行预处理。

然后从第一个数开始循环,把数放到树状数组里,如果数组中存在了,就再减去。每到一个右端点,就把和存到ans里。最后输出。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;
int n;
int b[30010],b1[30010];
long long c[30010];
long long ans[100010];
int vis[30005];
struct poin
{
    int l,r,id;
}a[100010];

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


struct poin1
{
    int v,id;
}a1[30005];

int cmp1(const poin1 a,const poin1 b)
{
    if(a.v<b.v)return 1;
    else return 0;
}

int lowbit(int x)
{
    return x&(-x);
}
int add(int x,int v)
{
    while(x<=30001)
    {
        c[x]+=v;
        x=x+lowbit(x);
    }
}

long long sum(int x)
{
    long long su=0;
    while(x>0)
    {
        su+=c[x];
        x-=lowbit(x);
    }
    return su;
}
int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
   {    memset(c,0,sizeof(c));
        memset(ans,0,sizeof(ans));
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            a1[i].v=b[i];
            a1[i].id=i;

        }
         sort(a1+1,a1+n+1,cmp1);

       b1[a1[1].id]=1;
       int o=2;
       for(int i=2;i<=n;i++)
       {
        if(a1[i].v==a1[i-1].v)b1[a1[i].id]=b1[a1[i-1].id];
        else b1[a1[i].id]=o++;
       }


        int m=0;
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            int q,w;
            scanf("%d%d",&q,&w);
            a[i].l=q;a[i].r=w;a[i].id=i;
        }
        sort(a+1,a+m+1,cmp);

        int j=1;
        for(int i=1;i<=m;i++)
        {
            for(;j<=a[i].r;j++)
            {
                if(vis[b1[j]])add(vis[b1[j]],-b[j]);
                vis[b1[j]]=j;
                add(j,b[j]);

            }
            ans[a[i].id]=sum(a[i].r)-sum(a[i].l-1);
        }

        for(int i=1;i<=m;i++)
        {
            printf("%I64d\n",ans[i]);
        }


    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值