E. Zbazi in Zeydabad
来源:互联网 发布:linux常用命令有哪些 编辑:程序博客网 时间:2024/06/08 17:17
树状数组的运用->好题
首先我们能想到n^3的做法,但是会超时,所以就需要优化。
我们用树状数组维护的是从这个点开始向后满足条件的情况
树状数组是维护的斜率,需要开n+m个树状数组;
用一个vector<> 去维护长度v[i]表示长度为i,且满足条件的点
我们从长度为m的开始往里面更新,那么先进去的一定是满足后面需要的条件的。
#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>#include <bitset>#include <map>#define FOR(i,a,b) for(int i=a;i<=b;i++)#define ROF(i,a,b) for(int i=a;i>=b;i--)#define mem(i,a) memset(i,a,sizeof(i))#define rson mid+1,r,rt<<1|1#define lson l,mid,rt<<1#define mp make_pair#define ll long long#define LL long longusing namespace std;template <typename T>inline void read(T &_x_){ _x_=0;bool f=false;char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=!f;ch=getchar();} while ('0'<=ch&&ch<='9') {_x_=_x_*10+ch-'0';ch=getchar();} if(f) _x_=-_x_;}const double eps = 0.0000001;const int maxn = 3000+7;const int mod = 1e9+7;int n,m;char s[maxn][maxn];int l[maxn][maxn],r[maxn][maxn],lr[maxn][maxn];vector<pair<int,int> > p[maxn];void init(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(s[i][j]=='z') l[i][j]=l[i][j-1]+1; else l[i][j]=0; } for(int j=m;j>=1;j--){ if(s[i][j]=='z') r[i][j]=r[i][j+1]+1; else r[i][j]=0; } } for(int i=n;i>=1;i--){ for(int j=1;j<=m;j++){ if(s[i][j]=='z'){ lr[i][j]=lr[i+1][j-1]+1; }else lr[i][j]=0; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { p[j + r[i][j] - 1].push_back(mp(i,j)); } } }int bit[maxn*2][maxn];ll ans=0; int sum(int i, int x) { int ans = 0; while(x > 0) { ans += bit[i][x]; x -= x & -x; } return ans; } void add(int i, int x, int d) { while(x <= m) { bit[i][x] += d; x += x & -x; } }int main(){ read(n),read(m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); init(); for(int i=m;i>=1;i--){ int len=p[i].size(); for(int j=0;j<len;j++){ int x = p[i][j].first; int y = p[i][j].second; add(x+y,y,1); } for(int j=1;j<=n;j++){ if(s[j][i]!='z')continue; int len = min(l[j][i],lr[j][i]); ans += sum(j+i,i)-sum(i+j,i-len); } } printf("%I64d\n",ans); return 0;}
阅读全文
0 0
- E. Zbazi in Zeydabad
- Zbazi in Zeydabad CodeForces
- Educational Codeforces Round 8 E. Zbazi in Zeydabad(BIT)
- CodeForces 628E Zbazi in Zeydabad(树状数组)
- Educational Codeforces Round 8 E - Zbazi in Zeydabad
- codeforces 628E Zbazi in Zeydabad(线段树||树状数组优化)
- Educational Codeforces Round 8(E. Zbazi in Zeydabad(树状数组优化))
- Codeforces edu 8 E. Zbazi in Zeydabad 树状数组 处理技巧
- codeforces628 E. Zbazi in Zeydabad 选择枚举对象+用顺序减少重复计算+树状数组优化
- Codeforces 628E:Zbazi in Zeydabad 树状数组的奇妙用法
- Educational Codeforces Round 8(E. Zbazi in Zeydabad(树状数组优化))★ ★
- Educational Codeforces Round 8 E. Zbazi in Zeydabad(给你一个n*m矩阵,每个点可能是'.'或者'z',问有多少种Z子型(Z为一个正方形))
- e-books in English
- poj1265 in e area
- [Built-in Functions] - E
- E. Ants in Leaves
- Trio in E 橙花
- E. Mishap in Club (CF 245E)
- poj3253 Fence Repair —huffman&&贪心(离散数学233)
- 队爷的 Au Plan(dp+单调队列)
- Dos(cmd)窗口下运行 JAVA程序(文件复制)
- Maven基本配置
- 108. Convert Sorted Array to Binary Search Tree
- E. Zbazi in Zeydabad
- 2017wust暑假集训第一阶段 动态规划基础专题小总结
- java提高篇(七)-----关键字static
- 对VMware下的Linux扩展磁盘空间(以CentOS6.9为例)
- Linux中常用目录作用
- java基础--1.基本语法
- HDU 5793 A Boring Question【快速幂+逆元】
- Boost编译的一些注意事项
- red hat常用命令4