HDU 5200 Tree (離線並查集)
Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1146 Accepted Submission(s): 363
Problem Description
Today CodeFamer is going to cut trees.There areN
trees standing in a line. They are numbered from 1
to N.
The tree numbered i
has height hi.
We say that two uncutted trees whose numbers are x
and y
are in the same block if and only if they are fitting in one of blow rules:
1)x+1=y or y+1=x;
2)there exists an uncutted tree which is numbered z, and x is in the same block with z, while y is also in the same block with z.
Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there?
1)x+1=y or y+1=x;
2)there exists an uncutted tree which is numbered z, and x is in the same block with z, while y is also in the same block with z.
Now CodeFamer want to cut some trees whose height is not larger than some value, after those trees are cut, how many tree blocks are there?
Input
Multi test cases (about
15).
For each case, first line contains two integers N and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.
In the following N lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees.
In the following Q lines, there will appear q[1],q[2],q[3],…,q[Q] which indicates CodeFamer’s queries.
Please process to the end of file.
[Technical Specification]
1≤N,Q≤50000
0≤h[i]≤1000000000(109)
0≤q[i]≤1000000000(109)
For each case, first line contains two integers N and Q separated by exactly one space, N indicates there are N trees, Q indicates there are Q queries.
In the following N lines, there will appear h[1],h[2],h[3],…,h[N] which indicates the height of the trees.
In the following Q lines, there will appear q[1],q[2],q[3],…,q[Q] which indicates CodeFamer’s queries.
Please process to the end of file.
[Technical Specification]
1≤N,Q≤50000
0≤h[i]≤1000000000(109)
0≤q[i]≤1000000000(109)
Output
For each
q[i],
output the number of tree block after CodeFamer cut the trees whose height are not larger thanq[i].
Sample Input
3 2
5
2
3
6
2
Sample Output
0
2
Hint
In this test case, there are 3 trees whose heights are 5 2 3.
For the query 6, if CodeFamer cuts the tree whose height is not large than 6, the height form of left trees are -1 -1 -1(-1 means this tree was cut). Thus there is 0 block.
For the query 2, if CodeFamer cuts the tree whose height is not large than 2, the height form of left trees are 5 -1 3(-1 means this tree was cut). Thus there are 2 blocks.Source
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5200
題目大意:有n棵樹,每棵高度位hi,q個詢問,每次砍掉高度不大於qi的樹,每個詢問後輸出當前有樹的區域被分成了幾塊
題目分析:典型的離線並查集,把沒被砍掉的樹併到一起,查詢的高度從大到小排序,因為如果qi > qj的話,qi時被合併的樹在qj時顯然還是並在一起的,注意這裡還需要對樹高從大到小排序,這是一個防止超時的優化,即如果當前的樹高度比qi小了,那就可以退出了,因為後面的樹肯定都要被砍掉,每列舉一棵樹cnt就要加1,每合併一次,cnt要減1,最後用id記錄詢問編號
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1e5 + 5;
int ans[MAX], fa[MAX], h[MAX];
int n, Q, cnt;
struct DATA
{
int id, h;
}q[MAX], s[MAX];
bool cmp(DATA a, DATA b)
{
return a.h > b.h;
}
void UF_set()
{
for(int i = 0; i <= n; i++)
fa[i] = i;
}
int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
void Union(int a, int b)
{
int r1 = Find(a);
int r2 = Find(b);
if(r1 != r2)
{
fa[r1] = r2;
cnt --;
}
}
int main()
{
while(scanf("%d %d", &n, &Q) != EOF)
{
cnt = 0;
UF_set();
for(int i = 1; i <= n; i++)
{
scanf("%d", &h[i]);
s[i].h = h[i];
s[i].id = i;
}
h[0] = h[n + 1] = 0;
for(int i = 1; i <= Q; i++)
{
scanf("%d", &q[i].h);
q[i].id = i;
}
sort(s + 1, s + n + 1, cmp);
sort(q + 1, q + Q + 1, cmp);
int j = 1;
for(int i = 1; i <= Q; i++)
{
while(j <= n && h[s[j].id] > q[i].h)
{
cnt ++;
if(h[s[j].id - 1] > q[i].h)
Union(s[j].id - 1, s[j].id);
if(h[s[j].id + 1] > q[i].h)
Union(s[j].id + 1, s[j].id);
j ++;
}
ans[q[i].id] = cnt;
}
for(int i = 1; i <= Q; i++)
printf("%d\n", ans[i]);
}
}
相關文章
- HDU 3333 Turing Tree(線段樹+離線操作)
- 樹(tree) - 題解(帶權並查集)並查集
- 並查集到帶權並查集並查集
- 查並集
- BZOJ 4195 程式自動分析【並查集+離散化】並查集
- 【並查集】【帶偏移的並查集】食物鏈並查集
- 並查集(一)並查集的幾種實現並查集
- 序列並查集的線性演算法並查集演算法
- 【並查集】【離散化】[NOI2015] 程式自動分析並查集
- 並查集(小白)並查集
- [leetcode] 並查集(Ⅱ)LeetCode並查集
- [leetcode] 並查集(Ⅲ)LeetCode並查集
- [leetcode] 並查集(Ⅰ)LeetCode並查集
- 3.1並查集並查集
- [HDU6793] Tokitsukaze and Colorful Tree
- HDU 1542 Atlantis (線段樹+離散化+掃描線)
- 並查集應用並查集
- 寫模板, 並查集。並查集
- 並查集的使用並查集
- 並查集跳躍並查集
- 各種並查集並查集
- 淺談並查集並查集
- 食物鏈(並查集)並查集
- 並查集(Union Find)並查集
- The Door Problem 並查集並查集
- 並查集練習並查集
- 並查集(二)並查集的演算法應用案例上並查集演算法
- BZOJ 3673 可持久化並查集 by zky 可持續化線段樹+並查集啟發式合併持久化並查集
- HDU 6035 Colorful Tree(樹形DP)
- 離線查詢與線上查詢
- 並查集題目合集並查集
- 並查集深度應用並查集
- 並查集java實現並查集Java
- 【轉】種類並查集並查集
- The Suspects-並查集(4)並查集
- 並查集擴充套件並查集套件
- (Day3)並查集並查集
- 並查集演算法並查集演算法
- 並查集-Java實現並查集Java