线段树(树型专线型)hdu4358
来源:互联网 发布:网络分配器 编辑:程序博客网 时间:2024/05/17 02:49
Online JudgeOnline ExerciseOnline TeachingOnline ContestsExercise AuthorF.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical ChartsBest Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
***重磅消息——[BestCoder Round #4]冠军将获得iPad Mini一部!
《BestCoder用户手册》下载
Total Submission(s): 1972 Accepted Submission(s): 561
Hand In Hand
Online Acmers
Forum | Discuss
Statistical ChartsBest Coder beta
VIP | STD Contests
Virtual Contests
DIY | Web-DIY beta
Recent Contests
lee
Mail 0(0)
Control Panel
Sign Out
Mail 0(0)
Control Panel
Sign Out
***重磅消息——[BestCoder Round #4]冠军将获得iPad Mini一部!
《BestCoder用户手册》下载
Boring counting
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)Total Submission(s): 1972 Accepted Submission(s): 561
Problem Description
In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?
Input
The first line of the input contains an integer T( T<= 5 ), indicating the number of test cases.
For each test case, the first line contains two integers N and K, as described above. ( 1<= N <= 105, 1 <= K <= N )
Then come N integers in the second line, they are the weights of vertice 1 to N. ( 0 <= weight <= 109 )
For next N-1 lines, each line contains two vertices u and v, which is connected in the tree.
Next line is a integer Q, representing the number of queries. (1 <= Q <= 105)
For next Q lines, each with an integer u, as the root of the subtree described above.
For each test case, the first line contains two integers N and K, as described above. ( 1<= N <= 105, 1 <= K <= N )
Then come N integers in the second line, they are the weights of vertice 1 to N. ( 0 <= weight <= 109 )
For next N-1 lines, each line contains two vertices u and v, which is connected in the tree.
Next line is a integer Q, representing the number of queries. (1 <= Q <= 105)
For next Q lines, each with an integer u, as the root of the subtree described above.
Output
For each test case, output "Case #X:" first, X is the test number. Then output Q lines, each with a number -- the answer to each query.
Seperate each test case with an empty line.
Seperate each test case with an empty line.
Sample Input
13 11 2 21 21 33213
Sample Output
Case #1:111
树上的查询。
思路:首先dfs对节点进行编号,转化成线性的,然后离线处理。更新的时候注意,下面举个例子:
0 1 2 3 4
2 2 2 2 2
当处理到位置4的时候,要对2位置-2,1位置+1,3位置+1,因为2位置先前是1,-2的目的是,当查询(2,4)是结果是零,同理,原来1位置是-1,+1后变成0,这样同样保证查询的正确性。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100100;int N,K,Q,idx;int data1[maxn],data2[maxn];int low[maxn],high[maxn],ans[maxn];map<int,int> m;vector<int> g[maxn];vector<int> pos[maxn];struct QU{ int s,e,id; bool operator < (const QU & a)const { return e<a.e; }}qu[maxn];struct IntervalTree{ int sum[maxn<<3]; void build(){memset(sum,0,sizeof(sum));} void update(int o,int l,int r,int pos,int val) { sum[o]+=val; if(l==r)return ; int mid=(l+r)>>1; if(pos<=mid)update(o<<1,l,mid,pos,val); else update(o<<1|1,mid+1,r,pos,val); } int query(int o,int l,int r,int q1,int q2) { if(q1<=l&&r<=q2)return sum[o]; int mid=(l+r)>>1; int ans=0; if(q1<=mid)ans+=query(o<<1,l,mid,q1,q2); if(q2>mid)ans+=query(o<<1|1,mid+1,r,q1,q2); return ans; }}tree;void dfs(int u,int fa){ low[u]=++idx; data2[idx]=data1[u]; int len=g[u].size(); for(int i=0;i<len;i++) { int v=g[u][i]; if(fa==v)continue; dfs(v,u); } high[u]=idx;}void init(){ m.clear(); for(int i=0;i<=N;i++) { g[i].clear(); pos[i].clear(); } idx=0;}int main(){ int T,cas=1,first=1; scanf("%d",&T); while(T--) { if(first)first=0; else printf("\n"); scanf("%d%d",&N,&K); int dataid=0; init(); for(int i=1;i<=N;i++) { scanf("%d",&data1[i]); if(m.find(data1[i])==m.end()) m[data1[i]]=dataid++; } for(int i=1;i<N;i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); } dfs(1,-1); scanf("%d",&Q); for(int i=1;i<=Q;i++) { int s; scanf("%d",&s); qu[i].s=low[s]; qu[i].e=high[s]; qu[i].id=i; } sort(qu+1,qu+1+Q); tree.build(); int num=1; for(int i=1;i<=N;i++) { int tmp=m[data2[i]]; pos[tmp].push_back(i); int cnt=pos[tmp].size(); if(cnt>=K) { if(cnt>K)tree.update(1,1,idx,pos[tmp][cnt-K-1],-2); if(cnt>K+1)tree.update(1,1,idx,pos[tmp][cnt-K-2],1); tree.update(1,1,idx,pos[tmp][cnt-K],1); } while(num<=Q&&qu[num].e==i) { ans[qu[num].id]=tree.query(1,1,idx,qu[num].s,qu[num].e); num++; } } printf("Case #%d:\n",cas++); for(int i=1;i<Q;i++)printf("%d\n",ans[i]); printf("%d\n",ans[Q]); } return 0;}
0 0
- 线段树(树型专线型)hdu4358
- hdu4358 线段树之离线累加处理
- hdu4358 树形转线性 线段树
- 专线
- hdu4358-Boring Counting-一道以树为背景的题的跟踪体会
- 【数据结构+线段树】连续型/离散型线段树
- 初识线段树(线段树总结)
- codevs1080线段树练习(线段树)
- 线段树?线段树!
- 线段树?线段树!
- poj2777-线段树应用(线段覆盖)
- 线段_线段树
- 线段_线段树
- 线段树(poj2528)
- 线段树(1)
- poj2823(线段树)
- 线段树(1)
- 线段树(2)
- 【cocos2d-x从c++到js】17:使用FireFox进行JS远程调试
- 互联网思维
- 整理Linux (以Ubuntu 为例) 基本命令 apt-get
- 2983Magic-Pen3
- Ubuntu 12.04下PHP环境的搭建(LAMP)
- 线段树(树型专线型)hdu4358
- acm暑假集训已经过半...
- gets strtok sscanf 实现读入一行并分割字符
- Tower of Cubes - UVa 10051 dp
- Ubuntu配置PPTP的VPN
- Hdu4155The Game of 31(DFS+博弈论)
- 韩语学习之第六课
- Array类及其重载的运算符
- 准备工作-用python绘制金融数据曲线的进阶实例