zoukankan      html  css  js  c++  java
  • 牛客暑期多校第二场 J.farm

    描述

    传送门:我是传送门

    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.
    White Cloud wants to help White Rabbit fertilize plants, but the i-th plant can only adapt to the i-th fertilizer. If the j-th fertilizer is applied to the i-th plant (i!=j), the plant will immediately die.
    Now White Cloud plans to apply fertilizers T times. In the i-th plan, White Cloud will use k[i]-th fertilizer to fertilize all the plants in a rectangle (x1[i]…x2[i])(y1[i]…y2[i]).
    White rabbits wants to know how many plants would eventually die if they were to be fertilized according to the expected schedule of White Cloud.

    输入

    The first line of input contains 3 integers n,m,T(nm<=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.

    样例

    输入

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

    输出

    3

    思路

    牛客网暑期ACM多校训练营(第二场)J.farm (随机数+二维树状数组)

    之前一直想补这个题目,但是当时没看明白题解(主要是没明白大家说的随机是什么意思

    今天回头补题的时候看到了这个博客,感觉写的很好,豁然开朗

    PS:

    注意在牛客提交的时候选择C++11

    我的二维树状数组模板

    代码

    /*
     * ===============================================================
     *
     *       Filename:  J.cpp
     *
     *           Link:  https://www.nowcoder.com/acm/contest/140/J
     *
     *        Version:  1.0
     *        Created:  2018/10/21 19时50分25秒
     *       Revision:  none
     *       Compiler:  g++
     *
     *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
     *   Organization:  QLU_浪在ACM
     *
     * ================================================================
     */
    #include <bits/stdc++.h>
    using namespace std;
    #define clr(a, x) memset(a, x, sizeof(a))
    #define rep(i,a,n) for(int i=a;i<=n;i++)
    #define pre(i,a,n) for(int i=n;i>=a;i--)
    #define ll long long
    #define max3(a,b,c) fmax(a,fmax(b,c))
    #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    const double eps = 1e-6;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7;
    const int N = 1e6+10;
    int t,n,m,x,x11,y11,x22,y22;
    vector<ll> tree[N];
    vector<ll> mp[N];
    ll has[N];
    void init()
    {
        srand(time(NULL));
        for(int i = 1;i < N-5;i++)
            has[i] = (ll)rand()*1e6+rand()*rand();
    }
    
    
    void add(int x,int y,ll z)
    {
        int memy_y = y;
        while(x <= n)
        {
            y = memy_y;
            while(y <= m)
                tree[x][y] += z,y += y&(-y);
            x += x&(-x);
        }
    }
    
    void range_add(int xa,int ya,int xb,int yb,ll z)
    {
        add(xa,ya,z);
        add(xa,yb+1,-z);
        add(xb+1,ya,-z);
        add(xb+1,yb+1,z);
    }
    
    ll ask(int x,int y)
    {
        ll res = 0,memy_y = y;
        while(x)
        {
            y = memy_y;
            while(y)
                res += tree[x][y],y -= y&(-y);
            x -= x&(-x);
        }
        return res;
    }
    
    int main()
    {
        ios
    #ifdef ONLINE_JUDGE
    #else
            freopen("in.txt","r",stdin);
        // freopen("out.txt","w",stdout);
    #endif
        scanf("%d %d %d",&n,&m,&t);
        init();
        rep(i,1,n)
        {
            mp[i].push_back(0);
            tree[i].push_back(0);
            rep(j,1,m)
            {
                scanf("%d",&x);
                mp[i].push_back(has[x]);
                tree[i].push_back(0);
            }
        }
        ll ans = 0,xx;
        while(t--)
        {
            scanf("%d %d %d %d %lld",&x11,&y11,&x22,&y22,&xx);
            range_add(x11,y11,x22,y22,has[xx]);
        }
        rep(i,1,n)
            rep(j,1,m)
                if(ask(i,j) % mp[i][j])
                    ans++;
        printf("%lld
    ",ans);
        fclose(stdin);
        // fclose(stdout);
        return 0;
    }
    
  • 相关阅读:
    JavaScript 19 数组(二)
    JavaScript 19 数组(一)
    JavaScript 18 字符串(三)
    JavaScript 18 字符串(二)
    JavaScript 18 字符串(一)
    History 5 : Silk Roads / Mongols / Ottoman and Mughal Empires
    History : Pictures of History
    001 markdown基本语法
    c#知识结构图
    c#知识结构图
  • 原文地址:https://www.cnblogs.com/duny31030/p/14305226.html
Copyright © 2011-2022 走看看