HDU 4056 draw a mess (Data Structure-parallel query set)

Source: Internet
Author: User
Draw a mess Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 864 accepted submission (s): 180


Problem descriptionit's graduated season, every students shocould 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.
 
Inputthere 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 shoshould cover the pixels (x, y) which satisfied inequality (X-XC) 2 + (Y-YC) 2 ≤ R2 with color C;
* Diamond: Given XC, YC, R, C, and you shoshould 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 shoshould 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 shoshould cover the correspond pixels with color C;

Note: All shape shocould 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 ≤ s-1)
 
Outputfor each test case you shold output nine integers indicating the amount of pixels covered by each color.
 
Sample Input
8 8 4Diamond 3 3 1 1Triangle 4 4 3 2Rectangle 1 1 2 2 3Circle 6 6 2 4
 
Sample output
4 4 4 11 0 0 0 0 0HintThe final distribution of different colors:0000000003300000033100000011100000022240000024440000444400000444 
Start with the line segment tree, And the HDU will play the MLE, and then TLE, but zojac. Finally, I saw how others did it. I used and checked the set. It was a new skill! Get!
Idea: At first glance, the number of rows is only 200, which is much less than the number of columns. Therefore, each graph is processed row by row to overwrite the range of the row. The subsequent image overwrites the previous one. Therefore, we will process it from the back to the front. Now, let's take a look at how each line is handled: for example, the top layer model is painted from the right end of the line segment. When it is painted with a grid that has already been painted, I jumped directly to the next unpainted grid, which reduced the time for traversing the colored grid. But how can we jump to what we need? You can use and query the set.

#include <string>#include <cmath>#include <iostream>#include <cstdio>#include <vector>using namespace std;const double eps = 1e-8;const int maxn = 210;const int maxm = 50010;int m , n , q , color[15] , father[maxm] , vis[maxm];struct Node{char op;int xc , yc , r , l , w,c;Node(char OP = 'x' , int XC = 0, int YC = 0, int R = 0, int L = 0 , int W = 0, int C = 0){op = OP , xc = XC , yc = YC , l = L , r = R , w = W , c = C;}};vector<Node> v;int findFather(int x){if(father[x] != x) return father[x] = findFather(father[x]);return father[x];}inline bool getCircle(int pos,int xc,int yc,int r,int &L,int &R) {    int tmp = r*r-(pos-xc)*(pos-xc);    if(tmp<0) return false;    L = int(-sqrt(tmp*1.0)+yc+1.0-eps);    //L = max(0,L);    if(0 > L) L = 0;    R = int(sqrt(tmp*1.0)+yc);    //R = min(m-1,R);    if(R > m-1) R = m-1;    if(L>R) return false;    return true;}inline bool getRect(int pos,int xc,int yc,int l,int w,int &L,int &R) {    if(pos<xc || pos >= xc+l) return false;    else {        L = yc;        //L = max(0,L);        if(0 > L) L = 0;        R = yc+w-1;        //R = min(m-1,R);        if(R > m-1) R = m-1;        if(L>R) return false;        return true;    }}inline bool getDiamond(int pos,int xc,int yc,int r , int &L , int &R) {    int tmp = r - abs(xc-pos);    if(tmp < 0) return false;    L = yc-tmp;    //L = max(0,L);    if(0 > L) L = 0;    R = yc+tmp;    //R = min(m-1,R);    if(R > m-1) R = m-1;    if(L>R) return false;    return true;}inline bool getTriangle(int pos , int xc , int yc , int w , int &L , int &R){if(pos < xc) return false;L = yc-w/2+pos-xc;R = yc+w/2-(pos-xc);//L = max(L , 0);if(0 > L) L = 0;//R = min(R , m-1);if(R > m-1) R = m-1;if(L>R) return false;return true;}void initial(){for(int i = 0; i < 15; i++) color[i] = 0;v.clear();for(int i = 0; i < maxm; i++) father[i] = i , vis[i] = 0;}void computing(){char shape[20];int xc , yc , w , r , c , l , L , R;while(q--){scanf("%s" , shape);if(shape[0] == 'C'){scanf("%d%d%d%d" , &xc , &yc , &r , &c);}if(shape[0] == 'D'){scanf("%d%d%d%d" , &xc , &yc , &r , &c);}if(shape[0] == 'R'){scanf("%d%d%d%d%d" ,&xc ,&yc ,&l ,&w ,&c);}if(shape[0] == 'T'){scanf("%d%d%d%d" ,&xc,&yc,&w,&c);}v.push_back(Node(shape[0] , xc , yc , r , l , w , c));}int fl , fr;for(int i = 0; i < n; i++){//cout << i << ": ";for(int j = 0; j < m; j++) father[j] = j , vis[j] = 0;for(int k = v.size()-1; k >= 0; k--){if(v[k].op == 'C' && !getCircle(i , v[k].xc , v[k].yc , v[k].r , L , R))continue;if(v[k].op == 'D' && !getDiamond(i , v[k].xc , v[k].yc , v[k].r , L , R)) continue;if(v[k].op == 'R' && !getRect(i , v[k].xc , v[k].yc , v[k].l , v[k].w , L , R)) continue;if(v[k].op == 'T' && !getTriangle(i , v[k].xc , v[k].yc , v[k].w , L , R)) continue;c = v[k].c;//cout << L << " " << R << " " << c << endl;int fl = findFather(L);while(R>=L){if(vis[R] == 0){vis[R] = 1;color[c]++;}fr = findFather(R);R = min(fr-1 , R-1);if(fr > fl)father[fr] = fl;}}}printf("%d" , color[1]);for(int i = 2; i <= 9; i++) printf(" %d" , color[i]);printf("\n");}int main(){while(~scanf("%d%d%d" , &n , &m , &q)){initial();computing();}return 0;}


HDU 4056 draw a mess (Data Structure-parallel query set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.