2018 Summer ACM cattle off network multi-school training camp (second field) J Farm (Fenwick tree, differential, thinking)

2018 Summer ACM cattle off network multi-school training camp (second field) J Farm (Fenwick tree, differential, thinking)

Links: https://ac.nowcoder.com/acm/problem/16637 Source: Cattle-off network

Time limit: C / C ++ 4 seconds and 8 seconds in other languages
to space constraints: C / C ++ 262144K, other languages 524288K
64bit IO the Format:% LLD

Title Description

White Rabbit has a rectangular farmland of n*m. In each of the grid there is a kind of plant. The plant in the j-th column of the i-th row belongs the a[i][j]-th type.

Enter a description:

The first line of input contains 3 integers n,m,T(n*m<=1000000,T<=1000000)For the next n lines, each line contains m integers in range[1,n*m] denoting the type of plant in each grid.For the next T lines, the i-th line contains 5 integers x1,y1,x2,y2,k(1<=x1<=x2<=n,1<=y1<=y2<=m,1<=k<=n*m)

Output Description:

Print an integer, denoting the number of plants which would die.

Example 1

Entry

copy

2 2 2
1 2
2 3
1 1 2 2 2
2 1 2 1 1

Export

copy

3

Meaning of the questions:

Given a matrix of n * m a,

\ (a [i] [j ] \) has one \ (\ [n * m 1 ,]) number range, representative of the type of flower.

Then there are times t fertilization,

Every fertilization, will choose a fertilizer sub-matrix type \ (ki \) fertilizer,

If the type of flower is \ (ki \) , which is administered to the \ (kj \) type of fertilizer, it \ (ki! = KJ \) , the flower of death.

So you ask after the operation, a total number of flower of death?

Ideas:

The number of dead flowers = n * m- spent survival

The problem is converted to the number of surviving seeking flowers.

Look at what the situation is under flower survive?

1: I did not give its fertilization.

2: Fertilizer Type of all \ (a [i] [j ] \)

Then we can first get through a two-dimensional differential fertilizer every perianth how many times referred to as \ (c [i] [J] \) .

We see the \ (a [i] [j ] \) type of fertilizer, how many times to fertilize the flower (i, j), is the sum (i, j). This is a two-dimensional partial order + differential problem.

(We just need to sort their x-coordinate first and then Fenwick tree maintenance y coordinates, you can get every point is how many matrices include)

(Note that, when the X, Y coordinates are the same, the first processing operation, reprocessing ask, what can be processed in the ordering of the time)

If \ (sum (i, j) = c [i] [j] \) indicates that the flower was built in, otherwise this flower show has been linked.

detail:

Because only a given data range \ (n-m * <= 1e6 \) , can not be kept open fixed two-dimensional array \ (C [] [] \)

Can be readily opened vector dynamic array may be one-dimensional array C [], in the two-dimensional coordinates of numbered,

Then with one-dimensional array can be used as a two-dimensional array.

In this case, since the differential will have access to an array of x + coordinate this case 1, y + 1,

If n = 1, m = 1e6, then analog one-dimensional array, it will access the next labeled 2E6, so that the array C [] to open to more than 2e6 + 1.

1e6 open only then, will the array access out of bounds, resulting in RE.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
const int maxn = 2000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m, q;
int getid(int x, int y)
{
    return m * (x - 1) + y;
}
int a[maxn];
int c[maxn];
struct node {
    int op;
    int x, y;
    node() {}
    node(int opp, int xx, int yy)
    {
        op = opp;
        x = xx;
        y = yy;
    }
    bool operator < (const node &bb) const
    {
        if (x != bb.x) {
            return x < bb.x;
        } else if (y != bb.y) {
            return y < bb.y;
        } else {
            return abs(op) > abs(bb.op);
        }
    }
};

int tree[maxn];
int lowbit(int x)
{
    return -x & x;
}
int ask(int x)
{
    int res = 0;
    while (x) {
        res += tree[x];
        x -= lowbit(x);
    }
    return res;
}
void add(int x, int val)
{
    while (x <= m + 1) {
        tree[x] += val;
        x += lowbit(x);
    }
}
std::vector<node> v[maxn];
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    du3(n, m, q);
    repd(i, 1, n) {
        repd(j, 1, m) {
            int x;
            du1(x);
            v[x].push_back(node(0, i, j));
        }
    }
    int k;
    int x1, x2, yy1, y2;
    while (q--) {
        du2(x1, yy1);
        du3(x2, y2, k);
        v[k].push_back(node(1, x1, yy1));
        v[k].push_back(node(1, x2 + 1, y2 + 1));
        v[k].push_back(node(-1, x1, y2 + 1));
        v[k].push_back(node(-1, x2 + 1, yy1));
        c[getid(x1, yy1)]++;
        c[getid(x2 + 1, yy1)]--;
        c[getid(x1, y2 + 1)]--;
        c[getid(x2 + 1, y2 + 1)]++;
    }
    repd(i, 1, n ) {
        repd(j, 1, m) {
            c[getid(i, j)] = c[getid(i, j)] - c[getid(i - 1, j - 1)] + c[getid(i - 1, j)] + c[getid(i, j - 1)];
            // cout << c[getid(i, j)] << " ";
        }
        // cout << endl;
    }
    int ans = 0;
    repd(i, 1, n * m) {
        sort(ALL(v[i]));
        int len = sz(v[i]);
        for (int j = 0; j < len; ++j) {
            if (v[i][j].op == 0) {
                int res = ask(v[i][j].y);
                if (res == c[getid(v[i][j].x, v[i][j].y)]) {
                    ans++;
                }
            } else {
                add(v[i][j].y, v[i][j].op);
            }
        }
        for (int j = 0; j < len; ++j) {
            if (v[i][j].op != 0) {
                add(v[i][j].y, -1 * v[i][j].op);
            }
        }
    }
    printf("%d\n", n * m - ans );
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11799853.html