HDU 3333 Turing Tree (离线线段树,经典)

来源:互联网 发布:电气工程师设计软件 编辑:程序博客网 时间:2024/04/28 06:56

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
231 1 421 22 351 1 2 1 331 52 43 5
 

Sample Output
15636

题意:多次查询区间中不同数之和

分析:所有询问右端点排序后,从小到大扫过去,线段树维护序列区间和,用一个map记录各个数最右边出现的位置,一遇到一个数就把之前位置消除并更新当前位置,相当于把各个数尽量向右移动。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>#define mem(p,k) memset(p,k,sizeof(p));#define rep(a,b,c) for(int a=b;a<c;a++)#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define inf 0x6fffffff#define ll long longusing namespace std;const int maxn=30005;struct que{    int l,r,i;    bool operator <(que a){            return r<a.r;    }}que[100010];int n,q;ll a[maxn];ll  sum[maxn<<2],ans[100010];map<ll,int> pos;void update(int k,int val,int l,int r,int rt){    sum[rt]+=val;    if(l==r)return;    int m=(l+r)>>1;    if(k<=m)update(k,val,lson);    else update(k,val,rson);}ll query(int L,int R,int l,int r,int rt){    if(L<=l && r<=R){        return sum[rt];    }    int m=(l+r)>>1;    ll ans=0;    if(m>=L) ans+=query(L,R,lson);    if(m<R) ans+=query(L,R,rson);    return ans;}int main(){    int t;    cin>>t;    while(t--){        mem(sum,0);        int n,k=1;        scanf("%d",&n);        for(int i=1;i<=n;i++)scanf("%lld",a+i);        scanf("%d",&q);        for(int i=1;i<=q;i++)scanf("%d%d",&que[i].l,&que[i].r),que[i].i=i;        sort(que+1,que+q+1);        pos.clear();        for(int i=1;i<=n;i++){            if(pos.count(a[i])){                update(pos[a[i]],-a[i],1,n,1);            }            pos[a[i]]=i;            update(i,a[i],1,n,1);            while(k<=q && i==que[k].r){                ans[que[k].i]=query(que[k].l,que[k].r,1,n,1);                //cout<<que[k].l<<que[k].r<<"=="<<ans[que[k].i]<<endl;                k++;            }        }        for(int i=1;i<=q;i++){            printf("%lld\n",ans[i]);        }    }    return 0;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝卖家不举证怎么办 咸鱼收到假货怎么办 伤没钱看病怎么办 steam付款失败怎么办 配偶没有公积金怎么办 玩吃鸡配置不够怎么办 华为AL20黑屏怎么办 华为照片不见了怎么办 电话卡暂停服务怎么办 电脑屏幕居中了怎么办 冰箱霜太多怎么办 qq账号忘了怎么办没有邮箱怎么办 车祸赔不起怎么办对方天天闹怎么办 专家解读有口臭怎么办口气重怎么办 油锅起火时应该怎么办或者怎么办 淘宝买了东西想改地址怎么办怎么办 手机显示程序异常这是怎么办怎么办 钢笔笔尖坏了怎么办 陶瓷刀钝了怎么办 地下城fps低怎么办 床头只能朝西怎么办 钢笔笔尖歪了怎么办 钢笔笔尖漏墨怎么办 被蜈蚣咬了怎么办 信用卡被盗刷了怎么办 练太极拳膝盖痛怎么办 僵尸毛毛虫咬了怎么办 迅雷下载速度慢怎么办 驾驶证换证过期怎么办 发生地震时该怎么办 尿常规潜血十3怎么办 尿酮体弱阳性怎么办 蔡英文当选大陆怎么办 维基百科打不开怎么办 陶瓷刀不快了怎么办 豆浆机刀片钝了怎么办 床头只能向西怎么办 施耐德钢笔漏墨怎么办 被人肉搜索了怎么办 智齿牙龈肿了怎么办 长智齿牙龈肿痛怎么办