【BestCoder】 HDOJ 5110 Alexandra and COS

如果按照sqrt(n)做。。。不是TLE就是MLE。。。所以改成3-15都能过。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1005
#define maxm 1000005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head

char g[maxn][maxn];
int sum[maxn][maxn];
int dp[maxn][maxn][11];
int sr[maxn][maxn][11];
int s, n, m, mm;

void read(void)
{
	s = 8;
	for(int i = 1; i <= n; i++) scanf("%s", g[i]+1);
}

void init(void)
{
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			sum[i][j] = sum[i][j-1] + (g[i][j] == 'X');
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= m; j++)
			for(int k = 0; k <= s; k++)
				dp[i][j][k] = sr[i][j][k] = 0;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			for(int k = 1; k <= s; k++) {
				sr[i][j][k] += (g[i][j] == 'X');
				if(i + k <= n && j - k >= 1) sr[i + k][j - k][k] += sr[i][j][k];
			}
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			for(int k = 1; k <= s; k++)
				sr[i][j][k] += sr[i][j-1][k];
	for(int i = 1;  i<= n; i++)
		for(int j = 1; j <= m; j++)
			for(int k = 1; k <= s; k++) {
				dp[i][j][k] += (g[i][j] == 'X');
				if(i - k <= 0) continue;
				int b = min(m, j + k);
				int a = max(1, j - k);
				dp[i][j][k] += dp[i - k][a][k];
				dp[i][j][k] += sr[i - k][b][k] - sr[i - k][a][k];
			}
}

void work(void)
{
	int x, y, d;
	while(mm--) {
		scanf("%d%d%d", &x, &y, &d);
		if(d > s) {
			int t = 0, ans = 0;
			for(int i = x; i > 0; i -= d) {
				int b = min(m, y + t);
				int a = max(0, y - t - 1);
				ans += sum[i][b] - sum[i][a];
				t += d;
			}
			printf("%d\n", ans);
		}
		else printf("%d\n", dp[x][y][d]);
	}
}

int main(void)
{
	while(scanf("%d%d%d", &n, &m, &mm)!=EOF) {
		read();
		init();
		work();
	}

	return 0;
}


你可能感兴趣的:(HDU)