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.
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).
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;}
阅读全文
0 0
- HDU 3333 Turing Tree (离线线段树,经典)
- HDU 3333 Turing Tree(离线线段树)
- [HDU 3333] Turing Tree (线段树+离线算法)
- HDU 3333 Turing Tree(线段树+离线操作)
- HDU 3333 Turing Tree (离线询问+线段树)
- hdu 3333 Turing Tree(线段树)
- HDU 3333 Turing Tree(线段树)
- hdu 3333 Turing Tree(线段树求区间内不同值之和+离线处理)
- HDU 3333 Turing Tree(离线操作+线段树||树状数组)
- HDU-3333-Turing Tree(离线+哈希+树状数组|线段树)
- hdu 3333 Turing Tree 线段树
- hdu 3333 Turing Tree 线段树
- 【HDU】3333 Turing Tree 线段树
- hdu 3333 Turing Tree(线段树)
- HDU 3333 Turing Tree (线段树)
- 【线段树+离散化+离线方法】杭电 hdu 3333 Turing Tree
- hdu 3333 Turing Tree 线段树 离线查询(区间内不同的数之和)
- hdu 3333 Turing Tree(线段树+离散化)
- Android Wi-Fi 四次握手简介
- 大数据和空间限制问题
- [LintCode]372.在O(1)时间复杂度删除链表节点
- Yocto 支持从sysvinit 到 systemd
- Bookshelf 2 -- 动态规划01背包稍作改变
- HDU 3333 Turing Tree (离线线段树,经典)
- Python_matlab_01
- 第一次发表博客
- 冒泡事件
- 制作jffs2根文件系统
- 模仿ofo小黄人眼睛跟随手机移动
- 欢迎使用CSDN-markdown编辑器
- PAT (Advanced Level) Practise 1071 Speech Patterns (25)
- 判断b[]是否是a[]的子字符串,不区分大小写问题