hdu3333Turing Tree
来源:互联网 发布:蚂蚁网络电视安卓版 编辑:程序博客网 时间:2024/06/03 16:03
Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5972 Accepted Submission(s): 2138
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思路:这是线段树离线操作的一个应用,首先我们先将查询的区间排序,按照区间右端小的放在前面,然后从[1,1],[1,2],[1,3],这样更新树,然后前面出现过的数字,就变为0,这样不断更新。代码:#include <iostream>#include <stdio.h>#include <string.h>#include <map>#include <algorithm>using namespace std;const int M=30005;struct node{ int l,r; long long int sum;}a[M*4];struct no{ int l,r; int i;}b[100005];void build(int i,int l,int r){ a[i].l=l; a[i].r=r; a[i].sum=0; if(a[i].l==a[i].r)return ; int mid=(a[i].l+a[i].r)/2; build(i*2,l,mid); build(i*2+1,mid+1,r);}void change(int l,int r,int x,int y){ if(l<=a[x].l&&r>=a[x].r) { a[x].sum=y; return ; } int mid=(a[x].l+a[x].r)/2; if(l<=mid) { change(l,r,x*2,y); } if(r>mid)change(l,r,x*2+1,y); a[x].sum=a[x*2].sum+a[x*2+1].sum;}long long int findx(int i,int l,int r){ if(l<=a[i].l&&r>=a[i].r) { return a[i].sum; } int mid=(a[i].l+a[i].r)/2; long long int ans=0; if(l<=mid)ans+=findx(i*2,l,r); if(r>mid)ans+=findx(i*2+1,l,r); return ans;}bool cmp(no a,no b){ return a.r<b.r;}long long int d[100005];int c[M];int main(){ int T; cin>>T; while(T--) { int n; cin>>n; int i; build(1,1,n); map<int,int>mm; for(i=1;i<=n;i++) { scanf("%d",&c[i]); } int m; scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d%d",&b[i].l,&b[i].r); b[i].i=i; } sort(b,b+m,cmp); int z=0; int t; for(i=1;i<=n;i++) { t=mm[c[i]]; if(t==0) { change(i,i,1,c[i]); mm[c[i]]=i; } else { change(t,t,1,0); change(i,i,1,c[i]); mm[c[i]]=i; } for(;z<m&&b[z].r==i;z++) { d[b[z].i]=findx(1,b[z].l,b[z].r); } } for(i=0;i<m;i++) { cout<<d[i]<<endl; } } return 0;}
阅读全文
0 0
- hdu3333Turing Tree
- Tree
- tree
- tree
- TREE
- Tree
- Tree
- tree
- tree
- tree
- tree
- tree
- tree
- Tree
- Tree
- tree
- TREE
- tree
- chap1 lambda 表达式
- 拦截器和过滤器监听器的区别执行顺序
- 将指定的form表单中所有的输入项转为json数据{key:value,key:value}
- VS调试时断点无法进入或命中的原因及解决方法(PDB引发的)
- 大O符号
- hdu3333Turing Tree
- 在用vtk8.0.0进行编程时,发现拾取actor时总是不对
- java: 正则匹配字符串中多个目标子串(贪婪匹配的模拟实现)
- 菲波那切数列
- Hdu 1181 -- 简单深搜的剪枝以及vector小结
- 顶会文章链接
- JAVA基础之思维导图
- Linux system DNS
- 为类取别名