2020 cattle off winter training camp algorithm base 6 F Cross Array

https://ac.nowcoder.com/acm/contest/3007/F
Title Description

Q learned little new kind of magic, can cause damage to the enemy on the grid of N rows and M columns

The i-th magic can cross a region on the grid (i.e., xi and the first row and the second column yi) causing damage to the enemy zi for each of the squares in

Q small case now using a total of H times magic, you need to count all of the damage after casting is completed, as detailed output description

Reminder: the larger the scale of this problem input, use efficient input mode

1≤H≤500,000 1≤xi, yi, zi, N, M≤2000 1≤xi≤N, 1≤yi≤M

Thinking 

 First, for each coordinate (x_{i},y_{i}), in which it ranks are affected, so to a certain point (a,b), the end result is that the total impact of the overall impact of the line where he received + where the column received, note that if the middle once this point because the impact is born, it will be more calculated once , so with w[i][j]the impact of record for each point by using two arrays, respectively, to maintain the tree rows and columns, when receiving (x_{i},y_{i})the impact of the tree in an array x_{i}of rows and y_{i}the columns are coupled with the rear impact, then w[x_{i}][y_{i}]=w[x_{i}][y_{i}]-z_{i}, deduplication affect the current point, where the last seek influence the row and column for each point, and then w[i][j](the current point) is to affect the summation point.

Multiplied by the (i+j)sum to a modulo

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof a)
using namespace std;
const int N = 2010;
typedef long long ll;
const int mod = 1e9 + 7;
template <class T>
bool read(T & a){
	a = 0;
	int flag = 0;
	char ch;
	if((ch = getchar()) == '-'){
		flag = 1;
	}
	else if(ch >= '0' && ch <= '9'){
		a = a * 10 + ch - '0';
	}
	while ((ch = getchar()) >= '0' && ch <= '9'){
		a = a * 10 + ch - '0';
	}
	if(flag)a = -a;
	return true;
}
template <class T, class ... R>
bool read(T & a, R & ... b){
	if(!read(a))return 0;
	read(b...);
}
template <class T>
bool out(T a){
	if(a < 0)putchar('-');
	if(a >= 10)out(a / 10);
	putchar(a % 10 + '0');
	return true;
}
template <class T, class ... R>
bool out(T a, R ... b){
	if(!out(a))return 0;
	out(b...);
}
ll n, m, h;
ll w[N][N];
ll a[N][2];
ll lowbit(ll x){
	return x & -x;
}
ll add(ll x, ll val, ll ind){
	if (ind == 0){
		while (x <= n){
			// ά»¤ÐÐ
			a[x][0] += val;
			x += lowbit(x); 
		}
	}
	else {
		while (x <= m){
			a[x][1] += val;
			x += lowbit(x);
		}
	}
}
ll query(ll x, ll ind){
	ll res = 0;
	while (x){
		res += a[x][ind];
		x -= lowbit(x);
	}
	return res;
}
int main()
{
	read(n, m, h);
	mem(w, 0);
	mem(a, 0);
	for (int i = 1; i <= h; i++){
		ll x, y, z;
		read(x, y, z);
		add(x, z, 0);
		add(y, z, 1);
		w[x][y] -= z;
	}
	ll res = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			ll t1 = query(i, 0) - query(i - 1, 0);
			ll t2 = query(j, 1) - query(j - 1, 1);
			w[i][j] += t1 + t2;
			res += w[i][j] % mod * (i + j) % mod;
			res %= mod;
		}
	}
	out(res % mod);
	return 0;
}

 

Published 204 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/104332031