GYM

来源:互联网 发布:js数组each遍历 编辑:程序博客网 时间:2024/06/13 21:58

Graveyard of Bandits
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

The rain still didn't want to stop, as well as my damned life. My target was Dragon — bandit and murderer called so since he alone set fire to the entire town with all its citizens. Good Magician remained my last clue. It's hardly possible that he knew something, but there was no choice.

After economic collapse Good Magician retired and got a job as a watchman at the Central Graveyard. The former chief advisor of the King now digged graves and dusted cold gravestones. I was walking through the graveyard, and my legs were drowning in the shaky ground, which seemed to be calling me to stay. Finally, I saw the Good Magician studying the map of the graveyard.

I said I was looking for Dragon, and he replied that in that case it would be reasonable for me to reserve a piece of land on his graveyard. I would have answered with a smile if I had remembered how. He said he was busy with solving a very important problem and couldn't get distracted.

The graveyard was a checkered field of size n × m cells. One grave, depending on the size of the coffin, could occupy a rectangle of one cell width and some cells length. There were a lot of different creatures in Fairy Kingdom so the lengths of coffins could vary a lot. For each possible length of a grave, Good Magician wanted to calculate how many different variants there were to allocate land for that grave, taking into account that some cells could be already occupied by other graves. The old man was for sure crazy, but I decided to help him so that we could get to the business sooner.

Input

The first line contains two integers separated by a space: n and m (1 ≤ n, m ≤ 105n × m ≤ 106) — the sizes of the graveyard in cells.

The next n lines contain m characters each — the map of the graveyard. The i-th line on its j-th position has character «.», if the corresponding cell is free and can be used for a grave, or character «+», if the corresponding cell is occupied.

Output

Output  integers separated by spaces. On the k-th position output the number of different ways to dig a grave of length k.

Examples
input
3 4...+.+.++++.
output
6 4 1 0
input
4 3....+..+....
output
10 10 6 2



题意:给你一个坟墓,问你用 1*1,1*2,1*3,1*4......的棺材去填这个坟墓,各能填多少个。


解题思路:一个一个格子的扫,然后判断他上面和左边到达第一个不能填的位置有多少个可以填,然后用线段树更新值即可。这里不用线段树应该也行,就是暴力!……



#include <iostream>#include <algorithm>#include <string>#include <vector>using namespace std;int N, M;int ans[100005];int tree[100005 << 2];int lazy[100005 << 2];void pushup(int rt){    tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];}void pushdown(int rt, int ln, int rn){    if (lazy[rt])    {        tree[rt << 1] += lazy[rt] * ln;        tree[rt << 1 | 1] += lazy[rt] * rn;        lazy[rt << 1] += lazy[rt];        lazy[rt << 1 | 1] += lazy[rt];        lazy[rt] = 0;    }}int query(int L, int R, int l, int r, int rt){    if (L <= l && r <= R)        return tree[rt];    int m = (l + r) >> 1;    pushdown(rt, m - l + 1, r - m);    int ANS = 0;    if (L <= m)        ANS += query(L, R, l, m, rt << 1);    if (R > m)        ANS += query(L, R, m + 1, r, rt << 1 | 1);    return ANS;}void update(int L, int R, int C, int l, int r, int rt){    if (L <= l && r <= R)    {        tree[rt] += C * (r - l + 1);        lazy[rt] += C;        return;    }    int m = (l + r) >> 1;    pushdown(rt, m - l + 1, r - m);    if (L <= m)        update(L, R, C, l, m, rt << 1);    if (R > m)        update(L, R, C, m + 1, r, rt << 1 | 1);    pushup(rt);}int shang[100005];int zuo[100005];vector<string> maze;int main(){    ios::sync_with_stdio(false);    cin >> N >> M;    int len = max(N, M);    string temp;    for (int i = 0; i < N; i++)    {        cin >> temp;        maze.push_back(temp);    }    for (int i = 0; i < N; i++)    {        for (int j = 0; j < M; j++)        {            if (maze[i][j] == '.')            {                update(1, ++shang[j], 1, 1, len, 1);                update(1, ++zuo[i], 1, 1, len, 1);            //    cout << shang[j] << " " << zuo[i] << endl;            }            else            {                shang[j] = 0;                zuo[i] = 0;            }        }    }    printf("%d ", query(1, 1, 1, len, 1)/2);    for (int i = 2; i <= len; i++)        if (i != len)            printf("%d ", query(i, i, 1, len, 1));        else            printf("%d\n", query(i, i, 1, len, 1));}



原创粉丝点击