Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- using namespace std;
- #define MAX_SIZE 400000
- class rect
- {
- public:
- int llx,lly,urx,ury;
- int color;
- };
- rect input[1001];
- int nrect;
- rect queue[MAX_SIZE];
- int q_begin = 0;
- int q_end = 0;
- void cut( rect , rect );
- int res[1001];
- int main()
- {
- ifstream fin("rect1.in");
- ofstream fout("rect1.out");
- int A,B,N;
- fin>>A>>B>>N;
- input[0].llx=0;
- input[0].lly=0;
- input[0].urx=A;
- input[0].ury=B;
- input[0].color = 1;
- for (int i = 1; i<=N ;i++)
- {
- fin>>input[i].llx>>input[i].lly
- >>input[i].urx>>input[i].ury
- >>input[i].color;
- }
- nrect = N+1;
- //rect cut;
- queue[0] = input[0];
- for (int i = 1; i<=N ; i++)
- {
- /* add rect[i] */
- int end = q_end%MAX_SIZE;
- for ( int j = q_begin ; j <= end ;j++)
- {
- cut(input[i],queue[j]);
- }
- q_end = (++q_end)%MAX_SIZE;
- queue[q_end] = input[i];
- q_begin = (end+1)%MAX_SIZE;
- }
- //output
- if (q_begin>q_end)
- {
- q_end += MAX_SIZE;
- }
- for (int j = q_begin ; j<= q_end ;j++)
- {
- res[queue[j%MAX_SIZE].color] += (queue[j%MAX_SIZE].urx-queue[j%MAX_SIZE].llx)*(queue[j%MAX_SIZE].ury-queue[j%MAX_SIZE].lly);
- }
- for (int i = 1 ; i<1001 ;i++)
- {
- if (res[i])
- {
- fout<<i<<' '<<res[i]<<endl;
- }
- }
- fin.close();
- fout.close();
- return 0;
- }
- void cut(rect i,rect j)
- {
- //i didn't overlap j
- if (i.llx>=j.urx||i.urx<=j.llx||i.lly>=j.ury||i.ury<=j.lly)
- {
- q_end = (++q_end)%MAX_SIZE;
- queue[q_end] = j;
- return;
- }
- int k1,k2,k3,k4;
- k1 = max( i.llx, j.llx );
- k2 = min( i.urx, j.urx );
- k3 = max( i.lly, j.lly );
- k4 = min( i.ury, j.ury );
- if (j.llx < k1)
- {
- q_end = (++q_end)%MAX_SIZE;
- queue[q_end].llx = j.llx;
- queue[q_end].lly = j.lly;
- queue[q_end].urx = k1;
- queue[q_end].ury = j.ury;
- queue[q_end].color = j.color;
- }
- if (j.urx>k2)
- {
- q_end = (++q_end)%MAX_SIZE;
- queue[q_end].llx = k2;
- queue[q_end].lly = j.lly;
- queue[q_end].urx = j.urx;
- queue[q_end].ury = j.ury;
- queue[q_end].color = j.color;
- }
- if (j.lly<k3)
- {
- q_end = (++q_end)%MAX_SIZE;
- queue[q_end].llx = k1;
- queue[q_end].lly = j.lly;
- queue[q_end].urx = k2;
- queue[q_end].ury = k3;
- queue[q_end].color = j.color;
- }
- if (j.ury>k4)
- {
- q_end = (++q_end)%MAX_SIZE;
- queue[q_end].llx = k1;
- queue[q_end].lly = k4;
- queue[q_end].urx = k2;
- queue[q_end].ury = j.ury;
- queue[q_end].color = j.color;
- }
- }
Add Comment
Please, Sign In to add comment