hdu 3333 Turing Tree

点击打开链接


Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3747    Accepted Submission(s): 1271


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

先按查询的右坐标升序排列。把每个数放进线段树的时候,先判断再之前他有没有在线段树,如果在,则删除它,并把他的位置更新到当前点


#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<string.h>
#include<algorithm>
#define N 300005
#define mod 10007
#define ll __int64
#define pii pair<int,int>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define ex 2.7182818284590452354+
#define pi 3.141592653589793239
#define eps 1e-8
#define INFF 999999999
using namespace std;
ll a[N],ans[N];
map<ll,int>vis;
struct T
{
    int l,r,id;
}op[N<<2];
bool cmp(T a,T b)
{
    return a.r<b.r;
}
struct node
{
    int l,r;
    ll sum;
    int mid()
    {
        return (l+r)>>1;
    }
}t[N<<2];
void up(node &fa,node &lson,node &rson)
{
    fa.sum=lson.sum+rson.sum;
}
void push_up(int root)
{
    up(t[root],t[root<<1],t[root<<1|1]);
}
void build(int root,int l,int r)
{
    t[root].l=l;
    t[root].r=r;
    t[root].sum=0;
    if(l==r)
        return;
    int mid=t[root].mid();
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
}
void update(int root,int pos,ll val)
{
    if(t[root].l==t[root].r)
    {
        t[root].sum+=val;
        return;
    }
    int mid=t[root].mid();
    if(pos<=mid)
        update(root<<1,pos,val);
    else
        update(root<<1|1,pos,val);
    push_up(root);
}
node query(int root,int l,int r)
{
    if(l<=t[root].l&&t[root].r<=r)
        return t[root];
    int mid=t[root].mid();
    node temp[3];
    int xx=0;
    if(l<=mid)
    {
        temp[1]=query(root<<1,l,r);
        xx++;
    }
    if(r>=mid+1)
    {
        temp[2]=query(root<<1|1,l,r);
        xx+=2;
    }
    if(xx<3)
        return temp[xx];
    up(temp[0],temp[1],temp[2]);
    return temp[0];
}
int main()
{
    int tt;
    while(scanf("%d",&tt)!=EOF)
    {
        while(tt--)
        {
            vis.clear();
            int i,k,n,m;
            scanf("%d",&n);
            for(i=1;i<=n;i++)
                scanf("%I64d",&a[i]);
            build(1,1,n);
            scanf("%d",&m);
            for(i=0;i<m;i++)
            {
                scanf("%d %d",&op[i].l,&op[i].r);
                op[i].id=i;
            }
            sort(op,op+m,cmp);
            k=1;
            for(i=0;i<m;i++)
            {
                for(;k<=op[i].r;k++)
                {
                    if(vis[a[k]]!=0)
                        update(1,vis[a[k]],-a[k]);
                    vis[a[k]]=k;
                    update(1,k,a[k]);
                }
                ans[op[i].id]=query(1,op[i].l,op[i].r).sum;
            }
            for(i=0;i<m;i++)
                printf("%I64d\n",ans[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值