Alexandra and COSTime Limit: 8000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 541 Accepted Submission(s): 154
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.
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.
Output
For each case, output Q lines, each line contains the number of treasures that the current submarine can detect.
Sample Input
Sample Output
|
每一次询问给三个整数x,y,v 表示以点(x,y)为中心画一个大V里的所有宝藏权值是v的约数的个数 宝藏权值是max(abs(xi-x),abs(yi-y))
思路: 容易想到可以对每一行做一个预处理,存下每一行的前缀和,这样对于某一特定行的某个区间就可以O(1)得到答案
容易发现对于一个大V,d=max(abs(xi-x),abs(yi-y))其实就是d=y-yi 所以就是求所有大V里面的点满足d%v==0的个数
但是仅仅是这样,然后每次暴力去找会超时。
然后又不能直接开一个三维数组去打表保存,数组开不下。所以我们考虑一种折中的方法-分块处理。
当v比较大时,我们直接每次都是间隔v往上走的,所以不需要花太多时间。所以直接暴力即可。
但是当v比较小的时候,我们每次几乎要遍历完整个数组,所以我们可以预处理v为1~10的时候的值。这样预处理所花时间O(10*1000*1000) 5S是能承受的
可以用三维的dp数组去存某个点间隔为1~10时候的数量.
转移方程为dp[i][j][k]=dp[i-k][j-k][k]+dp[i-k][j+k][k]-dp[i-2*k][j][k]+sum[i-k][j+k]-sum[i-k][j-k-1]
sum数组为前缀和。 注意如果(i,j)点本身为宝藏,还需要加一。 0为任何数约数。
注意转移的时候处理各种边界情况。。。比较繁琐。
这里还有个疑问,网上的代码可以处理到sqrt(n),也就是说n最大为35左右? 如果把10改成sqrt(n),我交了四次只卡过去一次。。。 不是很懂网上的代码怎么过的。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1010
#define M 40
char ma[N][N];
int sum[N][N];
int dp[N][N][M];
void init(int n,int m)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
int num=0;
if(ma[i][j]=='X') num++;
if(j==0) sum[i][j]=num;
else sum[i][j]=sum[i][j-1]+num;
}
}
int d=sqrt((double)n);
for(int k=1; k<=10; k++)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
dp[i][j][k]= ma[i][j]=='X'?1:0;
if(i<k) continue;
int l=j-k,r=j+k;
if(l>=0) dp[i][j][k]+=dp[i-k][l][k];
else if(i-2*k>=0)
{
dp[i][j][k]+=dp[i-2*k][j][k];
if(j!=0) dp[i][j][k]+=sum[i-2*k][j-1];
}
if(r<m) dp[i][j][k]+=dp[i-k][r][k];
else if((i-2*k)>=0)
{
dp[i][j][k]=dp[i][j][k]+dp[i-2*k][j][k];
if(j!=m-1) dp[i][j][k]+=(sum[i-2*k][m-1]-sum[i-2*k][j]);
}
if(l>=0)
dp[i][j][k]-=sum[i-k][l];
if(r<m) dp[i][j][k]+=sum[i-k][r-1];
else dp[i][j][k]+=sum[i-k][m-1];
if(i-2*k>=0) dp[i][j][k]-=dp[i-2*k][j][k];
}
}
}
}
int main()
{
int n,m,q;
int x,y,v;
while(~scanf("%d %d %d",&n,&m,&q))
{
for(int i=0; i<n; i++)
scanf("%s",ma[i]);
init(n,m);
int d=sqrt((double)n);
while(q--)
{
scanf("%d %d %d",&x,&y,&v);
x--,y--;
if(v<=10) printf("%d\n",dp[x][y][v]);
else
{
int ans= ma[x][y]=='X'? 1:0;
for(int i=x-v,j=v; i>=0; i-=v,j+=v)
{
int l=max(0,y-j),r=min(m-1,y+j);
ans+=(sum[i][r]-sum[i][l]);
if(ma[i][l]=='X') ans++;
}
printf("%d\n",ans);
}
}
}
return 0;
}