牛客網暑期ACM多校訓練營(第二場)J.farm (隨機數+二維樹狀陣列)
時間限制:C/C++ 4秒,其他語言8秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
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(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的矩陣藥田,藥田中每個格子都種有一種編號的為1~N*M的藥,隨後一個T表示有T次澆水操作,之後先是給出N行M列的矩陣(藥田),再給出T行,每行有5個數,X1,Y1,X2,Y2和W,表示這次操作會在左上角為(X1,Y1),右下角為(X2,Y2)的矩陣中澆W這種藥水,若是該某個藥格中的藥編號與被澆到的藥水不同,則死亡,問你T次澆水操作後整個藥田死了多少顆藥?
題解:這裡對每個藥的編號進行隨機hash得到一個大於最大編號1e6的數值,之後每次澆水操作,我們通過使用二維樹狀陣列區間更新的巧妙辦法,在已知更新的矩陣左上角和右下角情況下update四個點的值即可在O(logn*logm)時間內實現樹狀陣列的區間更新(需要注意的是這裡更新的值也是藥對應藥水的hash值才行).那麼最終想要知道某個格子的藥是否存活只需要判斷他的樹狀陣列的值%本身的hash值是否為0即可,在隨機數足夠優秀的條件下,能整除的情況說明該藥格每次澆到的藥水的是合適的(或者沒有澆到任何藥水),那麼它是存活的,反之死亡.
程式碼如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
vector<ll>mp[maxn]; //存二維矩陣
vector<ll>tre[maxn]; //存二維樹狀陣列結果
ll has[maxn];
int n, m;
void fun() { //預處理出1e6以內的所有數的has值(這裡指隨機生成的大於1e6的數)
srand(time(NULL));
for (int i = 1; i < maxn - 5; i++)
has[i] = (ll)rand()*1e6 + rand()*rand();
}
//一下三個函式使用的二維樹狀陣列
ll lowbit(ll x) {
return x&(-x);
}
void update(int x, int y, ll z)
{
for (int i = x; i <= n; i += lowbit(i))
for (int j = y; j <= m; j += lowbit(j))
tre[i][j] += z;
}
ll sum(int x, int y)
{
ll ret = 0;
for (int i = x; i >= 1; i -= lowbit(i))
for (int j = y; j >= 1; j -= lowbit(j))
ret += tre[i][j];
return ret;
}
int main() {
int t, x;
scanf("%d%d%d", &n, &m, &t);
fun();
for (int i = 1; i <= n; i++) {
mp[i].push_back(0); //使mp的列從1開始(方便後面查詢)
tre[i].push_back(0);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
mp[i].push_back(has[x]);//將給出的值進行隨機化
tre[i].push_back(0); //初始化樹狀陣列
}
}
int x1, y1, x2, y2;
ll w;
while (t--) { //給的是左上角和右下角的座標
scanf("%d%d%d%d%lld", &x1, &y1, &x2, &y2, &w);
update(x1, y1, has[w]); //通過更新這4個幾個實現區間更新
update(x2 + 1, y2 + 1, has[w]);
update(x2 + 1, y1, -has[w]);
update(x1, y2 + 1, -has[w]);
}
int ans = 0;
for (int i = 1; i <= n; i++) //若是最終在該點撒過得農藥總和值不是原值得倍數,則表示死亡
for (int j = 1; j <= m; j++)
if (sum(i, j) % mp[i][j] != 0) ans++;
printf("%d\n", ans);
return 0;
}
相關推薦
牛客網暑期ACM多校訓練營(第二場)J.farm (隨機數+二維樹狀陣列)
題目連結 時間限制:C/C++ 4秒,其他語言8秒 空間限制:C/C++ 262144K,其他語言524288K 64bit IO Format: %lld 題目描述 White Rabbit has a rectangular farmland of n*m. In
牛客網暑期ACM多校訓練營(第二場)-J-farm
(一)題面: 題目描述 White Cloud placed n containers in sequence on a axes. The i-th container is located at x[i] and there are a[i] number of p
牛客網暑期ACM多校訓練營(第二場)J-farm
時間限制:C/C++ 4秒,其他語言8秒 空間限制:C/C++ 262144K,其他語言524288K 64bit IO Format: %lld 題目描述 White Rabbit has a rectangular farmland of n*m. In each o
牛客網暑期ACM多校訓練營(第二場)J題題解(隨機化演算法/平方構造法過資料/bit操作)
首先將題目化為一維進行思考。則題目化簡為修改後,查詢區間內與修改的數不同的數的個數。 該題有多種做法,先講最簡單的一種: 1.先用兩個字首和儲存區間修改值和區間修改次數,則若是 該點修改值 = 該點修改次數 * 該點初始值,則很有可能施的是多次相同的肥料(即 8 = 4
牛客網暑期ACM多校訓練營(第一場)- J Different Integers (莫隊)
url namespace div struct different operator truct -- eof 題意:裸的莫隊題,每個查詢Li,Ri,返回區間[1,Li]和[Ri,N]區間中不同的數的個數。 分析:正常的離線查詢,是求區間[Li,Ri]中要求的答案,而該題
牛客網暑期ACM多校訓練營(第二場) I Car 思維
sizeof from rom src meet pbo nes eno rec 鏈接:https://www.nowcoder.com/acm/contest/140/I來源:牛客網 White Cloud has a square of n*n from (1,1) t
牛客網暑期ACM多校訓練營(第二場)I-car
發現 namespace set n+1 http 方案 同時 如何 一行 題意: 你要在一個n*n的矩形的邊界上方若幹輛車,所有車從同一時刻出發,以同樣的速度,從某一列的一側開到另一側或者從某一行的一側開到另一側。問最多放多少量車使得存在一種方式,這些車在行駛的過程中互不
牛客網暑期ACM多校訓練營(第二場)
text The ade class rank code thead 通過 -a Rank Solved A B C D E F G H I J K 44/452 4/11 O O . O . . . . O . . O: 當場通過 ?: 賽後通過 .:
牛客網暑期ACM多校訓練營(第二場)I.car-規律思維題
target 長度 情況下 con targe 標記 多校 class clu I.car 車只能從一邊走到另一邊,而且車和車不能相撞,車也不能走到坑裏。所以直接找規律,如果沒有坑,最多能放多少輛車。就會發現,關於對角線對稱的兩邊只能放一輛車,如果是奇數個的時候,中
牛客網暑期ACM多校訓練營(第二場)菜鳥補題QAQ
warn 分享圖片 ini lin int 技術分享 ace main bre G transform 題目大意: 數軸上有n個集裝箱,第i個集裝箱位於坐標x[i],有a[i]件貨物。現在要把集裝箱進行一些移動,求在所有貨物移動總距離不超過T的情況下,最多能把多少
牛客網暑期ACM多校訓練營(第一場) J題詳解
牛客網J題在比賽時是通過率最高的一道題,但是這道題對於時間的複雜度要求比較高。在比賽的時候,很多隊伍提交的程式都以”執行超時“而結束。那就讓我們先來看看這道看似簡單的題。 Different Integers 題目描述 Given a sequence of integers
牛客網暑期ACM多校訓練營(第一場)J Different Integers(樹狀陣列 + 離線 或 莫隊)
題目描述 Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l
牛客網暑期ACM多校訓練營(第二場).run(遞推)
時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 131072K,其他語言262144K 64bit IO Format: %lld 題目描述 White Cloud is exercising in the playground. White Clou
牛客網暑期ACM多校訓練營(第二場)car
時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 131072K,其他語言262144K 64bit IO Format: %lld 題目描述 White Cloud has a square of n*n from (1,1) to (n,n). Wh
牛客網暑期ACM多校訓練營(第二場)D-money (dp)
題目連結 題意 一共有n件商店,每個商店買賣東西的價格和不同,白兔想用差價掙錢,每次只能攜帶一種物品,問白兔最多賺多少的錢,和對應的交易次數 AC 兩個變數分別記錄當前買東西和買東西的剩餘財富,財富越大可以使得買下一件物品的損失更低,賣下一件東西
牛客網暑期ACM多校訓練營(第二場) D: money
White Cloud has built n stores numbered from 1 to n. White Rabbit wants to visit these stores in the order from 1 to n. The store number
牛客網暑期ACM多校訓練營(第二場) A
run 題解: 簡單的動態規劃。 題目提到 White Cloud 有兩種移動方式① walk 1 meters per second,② run k meters per second。 也就是說從次末狀態到末狀態有兩種方式即 “walk 1 meters”
牛客網暑期ACM多校訓練營(第二場)A-run
A-run 題 意:小明突然想運動了,他可以想跑[L,R]米,也就是說在跑了L,R米之間隨時可以停下,小明一秒可以選擇走1米或者跑k米。有q個詢問每次輸入[l,r],問有多少種方法。 資料範圍: 1<=Q<=1e5 1<=l,r<
補題:牛客網暑期ACM多校訓練營(第二場)-A-run
題目大意 白雲在健身,每秒可以走1米或跑k米,並且不能連續兩秒都在跑。 當它的移動距離在[L,R]之間時,可以選擇結束鍛鍊。 問有多少種方案結束。 題目描述 White Cloud is exercis
牛客網暑期ACM多校訓練營(第一場) A.Monotonic Matrix-非降路徑,Lindström-Gessel-Viennot引理-組合數學
mon typedef sub urn update 多校 計數 要求 oci 牛客網暑期ACM多校訓練營(第一場) A.Monotonic Matrix 這個題就是給你一個n*m的矩陣,往裏面填{0,1,2}這三種數,要求是Ai,j?