Problem Description
Alexandra and her little brother are playing a game called "Clash of Submarines" (COS, for short). In this game, one can build so many(about 100,000) submarines, and detect and collect underwater treasures.
A submarine use radar to detect treasures. Alexandra's submarines are all face north. For each submarine, its radar can detect treasures between northwest and northeast. For example, the "S" is the submarine, and the "+" is a grid which the submarine can detect, and the "-" is which it cannot detect:
++++-
+++--
-S---
-----
Note: the submarine can also detect the grid it occupies.
To simplify the problem, we build coordinate system on the map. X-axis goes from north to south, and Y-axis from west to east. We define the distance between two points (x1,y1) and (x2,y2) to be max(abs(x1-x2),abs(y1-y2)).
There is something special with the radar. Each submarine has a value D. The submarine can only detect a treasure, if D is a divisor of the distance between the submarine and the treasure. Don't forget any D is a divisor of 0.
Here comes the problem: Given the map of size N*M, and Q submarines' position and D, for each submarine please output the number of treasures it can detect.
Note: in this problem, we only detect treasures and won't collect them, so the submarines don't affect each other.
A submarine use radar to detect treasures. Alexandra's submarines are all face north. For each submarine, its radar can detect treasures between northwest and northeast. For example, the "S" is the submarine, and the "+" is a grid which the submarine can detect, and the "-" is which it cannot detect:
++++-
+++--
-S---
-----
Note: the submarine can also detect the grid it occupies.
To simplify the problem, we build coordinate system on the map. X-axis goes from north to south, and Y-axis from west to east. We define the distance between two points (x1,y1) and (x2,y2) to be max(abs(x1-x2),abs(y1-y2)).
There is something special with the radar. Each submarine has a value D. The submarine can only detect a treasure, if D is a divisor of the distance between the submarine and the treasure. Don't forget any D is a divisor of 0.
Here comes the problem: Given the map of size N*M, and Q submarines' position and D, for each submarine please output the number of treasures it can detect.
Note: in this problem, we only detect treasures and won't collect them, so the submarines don't affect each other.
Input
There are multiple test cases (no more than 30).
For each case, the first line is three positive integers N, M and Q.
Next N lines is a map of size N*M. "X" means treasure and "." means no treasure.
Next Q lines, each line is the description of a submarine. There are three positive integers X, Y, D, which means its position is (X,Y), and its value is D.
1≤N,M≤1000 .
1≤Q≤500,000 .
1≤X≤N,1≤Y≤M .
1≤D≤1000 .
Number of cases with max(N∗M,Q)>1,000 is no more than 3.
Huge data. Fast I/O may be needed.
For each case, the first line is three positive integers N, M and Q.
Next N lines is a map of size N*M. "X" means treasure and "." means no treasure.
Next Q lines, each line is the description of a submarine. There are three positive integers X, Y, D, which means its position is (X,Y), and its value is D.
1≤N,M≤1000 .
1≤Q≤500,000 .
1≤X≤N,1≤Y≤M .
1≤D≤1000 .
Number of cases with max(N∗M,Q)>1,000 is no more than 3.
Huge data. Fast I/O may be needed.
Output
For each case, output Q lines, each line contains the number of treasures that the current submarine can detect.
Sample Input
4 5 3 ..... XXXXX XXXXX XXXXX 4 3 1 4 3 2 4 3 3 1 1 1 X 1 1 1
Sample Output
9 6 1 1
参考:http://www.cnblogs.com/bin-gege/p/5696090.html
代码:
#include<bits/stdc++.h>
#define mem(p,k) memset(p,k,sizeof(p));
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N=1e3 + 10;
int n,m,q;
int dp[N][N][10],mp[N][N];
char s[N];
void init(){
mem(dp,0);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)for(int k=1;k<4;k++){
dp[i][j][k]=mp[i][j]-mp[i][j-1];
if(i>k){
dp[i][j][k]+=mp[i-k][min(m,j+k-1)]-mp[i-k][max(0,j-k)];
if(j>k && j+k<=m)dp[i][j][k]+=dp[i-k][j-k][k]+dp[i-k][j+k][k]-(i>2*k?dp[i-2*k][j][k]:0);
if(j>k && j+k>m)dp[i][j][k]+=dp[i-k][j-k][k]+(i>2*k?mp[i-2*k][m]-mp[i-2*k][j]:0);
if(j<=k && j+k<=m)dp[i][j][k]+=dp[i-k][j+k][k]+(i>2*k?mp[i-2*k][j-1]:0);
if(j<=k && j+k>m)dp[i][j][k]+=(i>2*k?mp[i-2*k][m]-mp[i-2*k][j]+mp[i-2*k][j-1]+dp[i-2*k][j][k]:0);
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&q)){
for(int i=1;i<=n;i++){
mp[i][0]=0;
scanf("%s",s+1);
for(int j=1;j<=m;j++){
mp[i][j]=mp[i][j-1]+(s[j]=='X');
}
}
init();
while(q--){
int x,y,k,ans=0;
scanf("%d%d%d",&x,&y,&k);
if(k<4){
ans=dp[x][y][k];
}
else{
for(int i=x,j=0;i>=1;i-=k,j++){
ans+=mp[i][min(m,y+j*k)]-mp[i][max(0,y-j*k-1)];
}
}
cout<<ans<<endl;
}
}
return 0;
}
/*
4 5 100
.....
XXXXX
XX.XX
XXXXX
*/