【HDU - 4056】Draw a Mess (并查集 or 线段树)
題干:
It's graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color.?
When teacher come to see what happened, without getting angry, he was surprised by the talented achievement made by students. He found the wall full of color have a post-modern style so he want to have an in-depth research on it.?
To simplify the problem, we divide the wall into n*m (1 ≤ n ≤ 200, 1 ≤ m ≤ 50000) pixels, and we have got the order of coming students who drawing on the wall. We found that all students draw four kinds of geometry shapes in total that is Diamond, Circle, Rectangle and Triangle. When a student draw a shape in pixel (i, j) with color c (1 ≤ c ≤ 9), no matter it is covered before, it will be covered by color c.?
There are q (1 ≤ q ≤ 50000) students who have make a drawing one by one. And after q operation we want to know the amount of pixels covered by each color.?
Input
There are multiple test cases.?
In the first line of each test case contains three integers n, m, q. The next q lines each line contains a string at first indicating the geometry shape:?
* Circle: given xc, yc, r, c, and you should cover the pixels(x, y) which satisfied inequality (x - xc)?2?+ (y - yc)?2?≤ r?2?with color c;?
* Diamond: given xc, yc, r, c, and you should cover the pixels(x, y) which satisfied inequality abs(x - xc) + abs(y - yc) ≤ r with color c;?
* Rectangle: given xc, yc, l, w, c, and you should cover the pixels(x, y) which satisfied xc ≤ x ≤ xc+l-1, yc ≤ y ≤ yc+w-1 with color c;?
* Triangle: given xc, yc, w, c, W is the bottom length and is odd, the pixel(xc, yc) is the middle of the bottom. We define this triangle is isosceles and the height of this triangle is (w+1)/2, you should cover the correspond pixels with color c;?
Note: all shape should not draw out of the n*m wall! You can get more details from the sample and hint. (0 ≤ xc, x ≤ n-1, 0 ≤ yc, y ≤ m-1)?
Output
For each test case you should output nine integers indicating the amount of pixels covered by each color.?
Sample Input
8 8 4 Diamond 3 3 1 1 Triangle 4 4 3 2 Rectangle 1 1 2 2 3 Circle 6 6 2 4Sample Output
4 4 4 11 0 0 0 0 0Hint
The final distribution of different colors: 00000000 03300000 03310000 00111000 00022240 00002444 00004444 00000444解題報告:
? ?其實這題建20棵線段樹也可以輕松解決,,不過因為數據加強了好像zoj上會被T(因為內存給的很寬松)hdu上會MLE
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; struct Node {char op[15];int x,y,l,w,c; } node[MAX]; int f[MAX],ans[MAX]; int n,m,q; bool vis[MAX]; int getf(int v) {return v == f[v] ? v : f[v] = getf(f[v]); } void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1!=t2) f[t2]=t1;//都歸到左側的t1上。。 } int main() { while(~scanf("%d%d%d",&n,&m,&q)) {for(int i = 1; i<=q; i++) {scanf("%s",node[i].op);if(node[i].op[0] == 'R') scanf("%d%d%d%d%d",&node[i].x,&node[i].y,&node[i].l,&node[i].w,&node[i].c);else scanf("%d%d%d%d",&node[i].x,&node[i].y,&node[i].l,&node[i].c);}memset(ans,0,sizeof ans);for(int r = 0; r<n; r++) {for(int c=0; c<m; c++) vis[c]=0,f[c]=c;for(int i = q; i>=1; i--) {int L,R,col = node[i].c;if(node[i].op[0] == 'C') {if(node[i].l < abs(r-node[i].x)) continue;int tmp = (int)sqrt(node[i].l*node[i].l - (r-node[i].x) * (r-node[i].x));//半徑^2 - 豎直長度= 投影長度 L=node[i].y-tmp;R=node[i].y+tmp; }if(node[i].op[0] == 'D') {if(node[i].l < abs(r-node[i].x)) continue;int tmp = node[i].l - abs(r-node[i].x);L=node[i].y-tmp;R=node[i].y+tmp;}if(node[i].op[0] == 'R') {if(r > node[i].x+node[i].l-1 || r < node[i].x) continue;L=node[i].y;R=node[i].y+node[i].w-1;}if(node[i].op[0] == 'T'){if(r-node[i].x >= (node[i].l+1)/2 || r<node[i].x) continue;int tmp = (node[i].l-1)/2 - (r-node[i].x);L=node[i].y-tmp;R=node[i].y+tmp;}L=max(0,L);R=min(R,m-1);int t1 = getf(L),t2;for(int c = R; c>=L; c=t2-1) {t2=getf(c);if(vis[t2]==0) ans[col]++,vis[t2]=1;//這里必須是標記t2,不能直接標記c。。因為你就不好查詢是否被標記過了,,總之這里并查集都是用根節點進行操作,所以都轉化成根節點就行了。 merge(t1,t2);}}}for(int i = 1; i<=9; i++) {printf("%d%c",ans[i],i==9?'\n':' ');}}return 0 ;}?
總結
以上是生活随笔為你收集整理的【HDU - 4056】Draw a Mess (并查集 or 线段树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡申请已受理是什么意思 或许离批卡还
- 下一篇: 【CodeForces - 474D】F