hdu 3333 Turing Tree
来源:互联网 发布:波尔津吉斯体测数据 编辑:程序博客网 时间:2024/06/05 18:34
点击打开链接
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.
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
先按查询的右坐标升序排列。把每个数放进线段树的时候,先判断再之前他有没有在线段树,如果在,则删除它,并把他的位置更新到当前点
#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 999999999using 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
- 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
- 【35.39%】【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-离散化思想
- C++宏定义详解
- App调试内存泄露之Context篇(上)
- Linux操作系统文件系统基础知识
- NYOJ 题目236 心急的C小加
- GDB使用方法
- hdu 3333 Turing Tree
- [SQL-码农]数据库错误:在执行批处理时出现错误。错误消息为: 目录名无效
- 关于jboss与tomcat的远程调试
- java 编码方式
- 探测目标哪些端口可用
- kernel thread completion demo
- 完美配置Tomcat的Https
- App调试内存泄露之Context篇(下)
- PLSQL带中文条件查询无法读出记录