hdu 3333(树状数组,离线,离散化)
来源:互联网 发布:mysql入门经典pdf下载 编辑:程序博客网 时间:2024/06/06 12:53
Turing Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4455 Accepted Submission(s): 1561
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
Author
3xian@GDUT
好像看过出题人的博客 2333333
题意:求出给定区间内不同种类的数的和值。
题解:
求区间啥啥的 八九不离十是线段树,树状数组,这题我们选用树状数组。
为了让区间和能表示我们想要的结果,同一个数只能出现一次,如果再次出现,我们需要把之前出现的删掉,再在当前位置加入。
则这样操作后,保证了当前位置前的区间是处理完的,是符合题意得。所以我们不能按照题给的顺序求区间,此时想到离线操作,我们把每个区间的右端点升序排列(此时操作里还需要一步离散化,因为最后需要按照给定区间的顺序进行输出)每加入数ai,就判断ai前有没有一个完整的区间,如果有,求出这个区间的ans。剩下操作如上面所说。
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cmath>#include <queue>#include <set>#include <map>#include <vector>#include <stack>#include <algorithm>#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))#define mem(a) memset(a, 0, sizeof(a))#define eps 1e-5#define M 100005#define N 30010#define INF 0x3f3f3f3f#define ll long longusing namespace std;map<int,int> mp;int t,n,m;ll rre[M];ll f[N];int a[N];struct node{ int l,r,id; bool operator<(const node &n)const{ return r<n.r||(r==n.r&&l<n.l); }}qu[M];int lowbit(int x){ return x&(-x);}void add(int x,int y){ while(x<=n) { f[x]+=y; x+=lowbit(x); }}ll sum(int x){ ll ans=0; while(x>0) { ans+=f[x]; x-=lowbit(x); } return ans;}int main(){ scanf("%d",&t); while(t--) { scanf("%d",&n); mem(f); for(int i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); qu[i]=(node){x,y,i}; } sort(qu,qu+m); int cur=0; mp.clear(); for(int i=1;i<=n;i++) { if(mp.find(a[i])==mp.end())//之前没加入过a[i]这个数。 { add(i,a[i]); mp.insert(make_pair(a[i],i));//记录a[I]出现的位置。 } else { add(i,a[i]); add(mp[a[i]],-a[i]); mp[a[i]]=i; } while(qu[cur].r==i)//判断之前有没有完整区间 { rre[qu[cur].id]=sum(qu[cur].r)-sum(qu[cur].l-1); cur++; } } for(int i=0;i<m;i++) cout<<rre[i]<<endl; } return 0;}
0 0
- hdu 3333(树状数组,离线,离散化)
- HDU 3333 Turing Tree (离散化+离线处理+树状数组)
- HDU 3333 树状数组(线段树) + 离散化 + 离线处理
- HDU 4417 —— Super Mario(树状数组,离散化,离线处理)
- 1004(树状数组+离线操作+离散化)
- 树状数组+离散化(hdu 5862)
- hdu 5877(树状数组+离散化)
- HDU3333 离线离散化树状数组
- HDU-3333-Turing Tree-(树状数组,离散化)
- HDU--3333(树状数组+离线处理)
- HDU 3333 树状数组离线
- HDU(3333)树状数组+离线
- hdu 3333 离线+树状数组
- HDU 4358 Boring counting(树的遍历+树状数组+离散化+离线处理)
- hdu 3607 Traversal(树状数组+离散化)
- hdu 3743 Frosh Week (树状数组的离散化)
- hdu - 4911 - Inversion(离散化+树状数组)
- HDU - 3743 Frosh Week(树状数组+离散化)
- 打表找规律+快速幂+求逆元______A Boring Question(hdu 5793 2016多校第六场)
- Jenkins与Docker相关的Plugin使用
- SQL语句总结
- ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum
- hdu 3333(树状数组,离线,离散化)
- Java 原子操作与并发(转)
- 第三章:服务设计原则
- 解决svn: E170001
- Java为什么使用System.arraycopy来操作数组
- (10)比较运算符 (11三元运算符)(12)逻辑运算符
- ubuntu 安装FastDFS v5.05
- Unity3D 通过Get与Post方式与服务器端进行交互
- UVa 11456 - Trainsorting