FOJ 有奖月赛 4月(校赛热身赛)D题 小茗的魔法阵
这题不太会,不过看完大神的博客就会了
现在写下自己的理解吧
- 定义ltop[i][j],rtop[i][j],l[i][j]
左上角,右上角,左边延伸出去连续的x,包含本身
预处理一下
一行一行的考虑,考虑点i,j作为右下角能构成三角形的情况
则左边最远能延伸到lm = max(j-l[i][j]+1,j-2*(ltop[i][j]-1))
对于任意当前行的x ∈ [lm , j ] 能构成三角形必须满足 2*(x + rtop[i][x]-1) >= x + j
即 x + 2*(rtop[i][x] - 1) >= j
到了这里 若定义f[i][j] = j + 2*(rtop[i][j] - 1) ,lm[i][j] = max(j-l[i][j]+1,j-2*(ltop[i][j]-1))
就是对于 当前(i,j)来说求多少当前行的x∈[lm[i][j],j],f[i][x] >= j
很显然我们可以想到树状数组来求解
但是如何解决这里f权值的限制
把所有的f和j加入结构体进行排序,并把所有的f标记为1,所有的j标记为2,按权值降序排,想到则先排f
然后再一遍过去统计答案
若当前结点是f ,那么加入树状数组
若当前结点是j,那么就可以统计答案,因为显然前面加入的权值不小于j,这样就可以区间求和了
注意这里要开奇偶树状数组,因为能构成三角形底边要么是 1 3 5 7的情况 要么是2 4 6 8 的情况
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#define LL long long
using namespace std;
const int maxn = 1e3 + 10;
char s[maxn][maxn];
int n , m;
int ltop[maxn][maxn],rtop[maxn][maxn],l[maxn][maxn];
int q[2][maxn];
int lowbit(int x)
{
return x&(-x);
}
int getsum(int type,int pos)
{
int ans = 0;
while(pos>0)
{
ans+=q[type][pos];
pos-=lowbit(pos);
}
return ans;
}
void add(int type,int pos,int c)
{
while(pos<=m)
{
q[type][pos]+=c;
pos+=lowbit(pos);
}
}
struct node
{
int f,lm,pos,flag;
node() {};
node(int f,int lm,int pos,int flag):f(f),lm(lm),pos(pos),flag(flag) {};
bool operator<(const node &rhs)const
{
if(rhs.f == f) return flag < rhs.flag;
return f > rhs.f;
}
} P[maxn*2];
int main()
{
int T;
scanf("%d",&T);
for(int cas = 1; cas<=T; cas++)
{
scanf("%d%d",&n,&m);
for(int i = 1; i <=n; i++) scanf("%s",s[i]+1);
memset(l,0,sizeof(l));
memset(ltop,0,sizeof(ltop));
memset(rtop,0,sizeof(rtop));
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
if(s[i][j]=='x')
{
l[i][j] = l[i][j-1] + 1;
rtop[i][j] = rtop[i-1][j+1] + 1;
ltop[i][j] = ltop[i-1][j-1] + 1;
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
memset(q,0,sizeof(q));
int tot = 0;
for(int j = 1; j<=m; j++)
{
if(s[i][j]=='x')
{
P[tot++] = node(j+2*(rtop[i][j]-1),0,j,1);
P[tot++] = node(j,max(j-l[i][j]+1,j-2*(ltop[i][j]-1)),j,2);
}
}
sort(P,P+tot);
for(int i = 0; i < tot;i++){
if(P[i].flag == 1) add(P[i].pos%2,P[i].pos,1);
else ans+=getsum(P[i].pos%2,P[i].pos)-getsum(P[i].pos%2,P[i].lm-1);
}
}
printf("Case #%d: %d\n",cas,ans);
}
return 0;
}