Turing Tree(离线处理+线段树/树状数组+思维)

题目链接
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
    题意:给一串序列,求某区间内在不同值的和。比如1 3 3 4 这个区间的和就是1+3+4=8.
    题解:线段树+离线处理什么是离线处理呢
    将所有的区间先存储,不作处理。所有的区间都输入后,按区间右端点升序排序。线段树初始化为0。按照顺序处理区间,每处理到一个新的区间,就看这个右区间端点对应的val值在这个点之前是否存在过了,如果没有,就标记这个点是这个val值的最靠右的端点。如果这个点之前有其他点对应这个val,就让那个点初始化为0.总的来说,就是尽量让val向右移动。
    记得开long long。w

AC代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define lowbit(x) (x&(-x))
#define MT(a,b) memset(a,b,sizeof(a))
const int maxn=1E6+5;
const int ONF=-0x3f3f3f3f;
const int INF=0x3f3f3f3f;
int num[maxn];
ll tree[maxn];
ll ans[maxn];
map<int,int>vis;
struct node
{
    int first,second,i;
}p[maxn];
bool cmp(node a,node b)
{
    return a.second<b.second;
}
void change(int l,int r,int ql,int qr,int val,int root)
{
    if (l==ql&&r==qr){
        tree[root]=val;
        return;
    }
    int mid=(l+r)>>1;
    if (qr<=mid) change(l,mid,ql,qr,val,root<<1);
    else if (ql>mid) change(mid+1,r,ql,qr,val,root<<1|1);
    else{
        change(l,mid,ql,mid,val,root<<1);
        change(mid+1,r,mid+1,qr,val,root<<1|1);
    }
    tree[root]=tree[root<<1]+tree[root<<1|1];
}
ll query(int l,int r,int ql,int qr,int root)
{
    if (l==ql&&r==qr){
        return tree[root];
    }
    int mid=(l+r)>>1;
    if (qr<=mid) return query(l,mid,ql,qr,root<<1);
    else if (ql>mid) return query(mid+1,r,ql,qr,root<<1|1);
    else return query(l,mid,ql,mid,root<<1)+query(mid+1,r,mid+1,qr,root<<1|1);
}
int main ()
{
    int t,n,m;
    scanf ("%d",&t);while (t--) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &num[i]);
        }
        scanf("%d", &m);
        for (int i = 1; i <= m; i++) {
            scanf("%d%d", &p[i].first, &p[i].second);
            p[i].i=i;
        }
        sort(p + 1, p + 1 + m, cmp);
        vis.clear();
        MT(tree,0);
        MT(ans,0);
        int tot=1;
        for (int i=1;i<=n;i++){
            int tmp=vis[num[i]];
            if (tmp==0){
                change(1,n,i,i,num[i],1);
            } else if (tmp!=0){
                change(1,n,tmp,tmp,0,1);
                change(1,n,i,i,num[i],1);
            }
            vis[num[i]]=i;
            while (p[tot].second<=i&&tot<=m){
                ans[p[tot].i]=query(1,n,p[tot].first,p[tot].second,1);
                tot++;
            }
        }
        for (int i=1;i<=m;i++){
            printf("%lld\n",ans[i]);
        }
    }
    return 0;
}

发布了33 篇原创文章 · 获赞 15 · 访问量 913

猜你喜欢

转载自blog.csdn.net/weixin_43925900/article/details/97633644
今日推荐