LG3801 red fantasy village tree line + inclusion and exclusion

Problem Description

After the last failure, Remilia decided to launch Hongwu mutation again, but in order to prevent back rule spiritual dream, she decided to release the Hongwu strange battle.

We will be seen as a fantasy village \ (n \ times m \) squares area, did not start any of Hongwu area is covered. Remilia each standing on one area, each issued a infinitely long Hongwu four directions truck, can affect the whole row / entire column, but does not affect that area of her station. If the two patches Hongwu collision, because the density is too large and will settle disappear. Reimu aware of this mutation, the decision to solve it. But before solving, spiritual dreams to understand a range of density Hongwu. Two operations can be simply described as:

1 x yRemilia standing coordinates \ ((x, y) \ ) release position to the infinite Hongwu four directions.

2 x1 y1 x2 y2Interrogation upper left point \ ((X1, Y1) \) , a lower right point \ ((x2, y2) \ ) within the rectangle, the number of areas covered Hong Wu.


answer

The place is also considered standing settlement.

Maintenance of two tree line, a line maintenance, a maintenance column, single-point modification, the query interval (Fenwick tree can)

Set \ (R, C \) represent the rows and columns are covered with the number, the answer is $ R \ times (y2 - y1 + 1) + C \ times (x2 - x1 + 1) - 2 \ times R \ times C $ .

Minus twice \ (R \ times C \) reasons: remove ranks of statistics each time.


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int maxn = 100000 + 7;
const int maxm = 200000 + 7;

int n, m, T;

template < typename Tp >
void read(Tp &x) {
	x = 0;char ch = 1;int fh = 1;
	while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if(ch == '-') fh = -1, ch=getchar();
	while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	x *= fh;
}

void Init(void) {
	read(n); read(m); read(T);
}

struct Segment_Tree {
	#define lfc (x << 1)
	#define rgc ((x << 1) | 1)
	#define mid ((l + r) >> 1)
	int val[maxn << 2];
	void modify(int x, int l, int r, int pos) {
		if(l == r) {
			val[x] = 1 - val[x];
			return ;
		}
		if(pos <= mid) modify(lfc, l, mid, pos);
		else modify(rgc, mid + 1, r, pos);
		val[x] = val[lfc] + val[rgc];
	}
	int query(int x, int l, int r, int L, int R) {
		if(L <= l && r <= R) return val[x];
		if(r < L || l > R) return 0;
		return query(lfc, l, mid, L, R) + query(rgc, mid + 1, r, L, R);
	}
}row, column;


void operator1(void) {
	int x, y;
	read(x); read(y);
	row.modify(1, 1, n, x);
	column.modify(1, 1, m, y);
}

void operator2(void) {
	int x1, x2, y1, y2;
	read(x1); read(y1); read(x2); read(y2);
	int R = row.query(1, 1, n, x1, x2), C = column.query(1, 1, m, y1, y2);
	LL ans = (LL)(y2 - y1 + 1) * (LL)R + (x2 - x1 + 1) *(LL)C - (LL)R * (LL)C * 2ll;
	printf("%lld\n", ans);
}

void Work(void) {
	while(T--) {
		int op; read(op);
		if(op == 1) operator1();
		else operator2();
	}
}

int main(void) {
	Init();
	Work();
	return 0;
}

Guess you like

Origin www.cnblogs.com/liubainian/p/12571638.html