ネットワークマルチ学校の合宿(第2フィールド)オフ2018年夏ACM牛Jファーム(フェンウィックツリー、差動、思考)

ネットワークマルチ学校の合宿(第2フィールド)オフ2018年夏ACM牛Jファーム(フェンウィックツリー、差動、思考)

リンク:https://ac.nowcoder.com/acm/problem/16637出典:牛オフネットワーク

制限時間:他の言語でのC / C ++ 4秒、8秒
スペースの制約:C / C ++ 262144K、他の言語の524288Kの
64ビットIOフォーマット:%LLD

タイトル説明

白ウサギは、n×m個の長方形の農地を有します。グリッドのそれぞれにおいて、植物の種類があります。第i行の第j列内の植物は、[I] [J]番目の種類に属します。

説明を入力します。

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)

出力説明:

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

例1

エントリー

コピー

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

輸出

コピー

3

質問の意味:

N * m個のAの行列が与えられると、

\([I] [jが 】\) 有するもの\(\ [N * mの1 、]) 番号範囲、花の種類の代表。

そして、時刻t受精があり、

すべての施肥、肥料サブマトリクス型を選択します\(KI \)肥料を、

花の種類がある場合は、\(KI \)に投与された\(KJ \)肥料の種類は、それ\(KI!= KJ \) 死の花。

だからあなたは、操作の後に死の花の総数を求めますか?

アイデア:

死んだ花の数= N *がM-は生存を過ごしました

問題は、花を求めて生存数に変換されます。

状況が生き残るの花の下にあるものを見てください?

1:私は、その受精を与えていません。

2:すべての肥料の種類\([I] [J ] \)

その後、我々はまず、すべてのと呼ばれる回数花被2次元差分肥料を介して取得することができます\(C [I] [J] \)

我々は見\([I] [J ] \) 花を肥やすために何回肥料の種類、(i、j)は、合計(i、j)があります。これは、2次元部分順+微分の問題です。

(私達はちょうど彼らのx座標最初にしてフェンウィック木のメンテナンスのy座標をソートする必要がある、あなたはすべてのポイントを得ることができ、多くの行列が含まれていますか)

(なお、X、Y座標が同一である場合、最初の処理動作、再処理が求める、時間の順序で処理することができるもの)

もし\(合計(i、j)は =のC [i]は[J] \) そうでない場合は、この花のショーは、リンクされている、花が内蔵されたことを示しています。

詳細:

のみ与えられたデータ範囲のため\(N-M * <= 1E6の\) 固定された2次元アレイ開いたままにすることができない(C [] [] \)\

容易に開けることができるベクターダイナミックアレイは、番号の二次元座標において、一次元配列C []とすることができます

次いで、一次元アレイと二次元アレイとして使用することができます。

この場合、差動のアレイへのアクセスを持っているので、X +この場合1、Y + 1座標

N = 1、M = 1E6、次に一次元アレイをアナログ場合、配列C []は、2E6 + 1以上に開くようにするように、それは、次標識2E6にアクセスします。

1E6は、のみ開くRE、その結果、範囲外の配列にアクセスします。

詳細コードを参照してください。

#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';
        }
    }
}

おすすめ

転載: www.cnblogs.com/qieqiemin/p/11799853.html