[Fzyzoj 2132] Zrn God Ben together to see the meteor shower

Source: Internet
Author: User

P2132--Zrn god Ben together to see the meteor shower

Time limit: 1000MS

Memory Limit: 131072KB

Description

Zrn God Ben with sister paper came to different degrees of space to see meteor shower.

There is a rectangular star plate, there is a total of n*m, meteor showers will fall there. Just, the meteor shower there is very peculiar, are very rhythmic, lined up to fall. Every moment will fall a row of meteors, they fall on the plate, forming a star group.

and zrn god Ben and sister paper for the definition of the STAR Group is not the same. Zrn God Ben that a star and the four stars around it belong to the same star group. And sister paper thinks that a star and eight of the stars around it belong to the same star group.

For example, in the first moment, the star plate may be like this:

At this time Zrn god Ben and sister paper have seen 2 star Group.

And then a row of stars fell:

At this time in Zrn God Ben appears to have 2 star Group, and in sister paper eye only 1 stars.

And dropped the third row of stars:

At this time zrn god Ben See there are 3 stars, and in the sister paper eye still only a star group.

Sister paper to see some tired, then rely on the Zrn god Ben Body. Zrn God Ben also stretched out his arm and gently hugged her.

Then the sister paper said: "Do you know at any time we see the number of clusters of stars?" Can't say You kissed me! ”

Zrn God Ben of course don't want to say! So you can count on it.

Input Format

First row two integers n,m

The next n-row m-column matrix represents the layout on the star disk at nth time, "*" indicates that the lattice has a star, "" indicates that the lattice has no stars.

The following line is an integer q, which represents the number of queries.

The next Q line, a positive integer t for each line, indicates that the time of the first T is asked.

Output Format

Output Q line, for each t, output a row of two integers, respectively, at the time of the first zrn god Ben See the number of stars and sister paper see the number of star groups.

Sample Input
3 * * * *  3123
Sample Output
2 22 13 1
Hint
# N M Q
1 10 10 1
2 2000 2000 1
3 3000 2000 2
4 50 50 50
5 40 40 5000
6 50 50 10000
7 1000 1000 50000
8 2000 2000 100000
9 5000 200 50000
10 20000 200 100000

Guaranteed 1≤t≤n.

Exercises

Is and check set, sister's open one, Zrn open one.

Then enter the time to maintain and maintain each time period.

Scroll the array for optimization.

And check the set when I open the same one to operate =-= unexpectedly t!!!!!

And then I wrote it separately, AC =-=.

We need to separate to optimize ...

1#include <bits/stdc++.h>2 using namespacestd;3 intf[2][40000010],r[20001][2001],t[2],n,m,ans[2][20001],k;4 intGETF0 (intx) {returnf[0][x]==x?x:f[0][X]=GETF0 (f[0][x]);}5 intGETF1 (intx) {returnf[1][x]==x?x:f[1][X]=GETF1 (f[1][x]);}6 voidJoin0 (intAintb) {7     intFa=getf0 (a), fb=getf0 (b);8     if(FA!=FB) f[0][fa]=fb,t[0]--;9 }Ten voidJoin1 (intAintb) { One     intFA=GETF1 (a), fb=GETF1 (b); A     if(FA!=FB) f[1][fa]=fb,t[1]--; - } - intRead () { the     intx=0;intfx=1; -     CharCh=GetChar (); -      while(ch<'0'|| Ch>'9') {if(ch=='-') fx=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} +     returnx*FX; - } + intMain () { AN=read (), m=read (); at      for(intI=0; i<n;++i) { -          for(intj=0; j<m;++j) { -             CharC=GetChar (); -             if(c=='*') { -++K; -f[0][k]=f[1][k]=r[i][j]=K; int[0]++,t[1]++; -                 if(j&&r[i][j-1]) { toJoin0 (r[i][j],r[i][j-1]); +Join1 (r[i][j],r[i][j-1]); -                 } the                 if(i&&r[i-1][j]) { *Join0 (r[i][j],r[i-1][j]); $Join1 (r[i][j],r[i-1][j]);Panax Notoginseng                 }                 -                 if(i&&j&&r[i-1][j-1]) Join1 (r[i][j],r[i-1][j-1]); the                 if(i&&j+1<m&&r[i-1][j+1]) Join1 (r[i][j],r[i-1][j+1]); +             } A         } the GetChar (); +ans[0][i]=t[0]; -ans[1][i]=t[1]; $     } $     intT,rx; -t=read (); -      while(t--) { theRx=read (); rx--; -printf"%d%d\n", ans[0][rx],ans[1][rx]);Wuyi     }     the     return 0; -}
View Code

Finally the most abnormal point optimization to 0.92s, but also added to read the optimization, or 0.98s it =-=

[Fzyzoj 2132] Zrn God Ben together to see the meteor shower

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.