HDU3333 离线离散化树状数组

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

是区间gcd的弱化版
没啥好优化的,裸的水题
就是看看前边有没有,有更新坐标,没有就加上。
感觉树状数组用的比线段树舒服
没有递归感觉舒服的很

#include<iostream>
#include<algorithm>
#include<memory.h>
#include<vector>
using namespace std;
long long n, q, lisan[30001], lixian[100001],shuru[30001],tu[30001],zuihou[30001];
long long lowb(long long x) { return x&-x; }
void gengxin(long long zuobiao, long long zhi)
{
    for (long long a = zuobiao;a <= n;a += lowb(a))
    {
        tu[a] += zhi;
    }
}
struct p
{
    long long zuo,tihao;
};
long long gs(long long zuobiao)
{
    long long s = 0;
    for (long long qq = zuobiao;qq > 0;qq -= lowb(qq))
    {
        s += tu[qq];
    }
    return s;
}
vector<p> dd[30001];
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        memset(lixian, 0, sizeof(lixian));
        memset(lisan, 0, sizeof(lisan));
        memset(shuru, 0, sizeof(shuru));
        memset(tu, 0, sizeof(tu));
        memset(zuihou, 0, sizeof(zuihou));
        for (int a = 1;a <= n;a++)
        {
            scanf("%lld", &shuru[a]);
            lisan[a] = shuru[a];
            dd[a].clear();
        }
        sort(lisan + 1, lisan + 1 + n);
        int m;
        cin >> m;
        long long qw, we;
        for (int a = 1;a <= m;a++)
        {
            scanf("%lld%lld", &qw, &we);
            dd[we].push_back({ qw,a });
        }
        for (int a = 1;a <= n;a++)
        {
            long long zuobiao = lower_bound(lisan + 1, lisan + n + 1, shuru[a])-lisan;
            if (a > zuihou[zuobiao])
            {
                if (zuihou[zuobiao])gengxin(zuihou[zuobiao], -shuru[a]);
                gengxin(a, shuru[a]);
                zuihou[zuobiao] = a;
            }
            for (int b = 0;b < dd[a].size();b++)
            {
                lixian[dd[a][b].tihao] = gs(a) - gs(dd[a][b].zuo - 1);
            }
        }
        for (int a = 1;a <= m;a++)printf("%lld\n", lixian[a]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值