P9917 「RiOI-03」网格

P9917 「RiOI-03」网格

行列染色,求红色网格周长。

矩阵是凸的,定义贡献,若 (x,y)(x,y) 为红色,(x+1,y)(x+1,y) 为白色,则竖直范围内有 22 贡献;水平同理。

动态二维数点非常麻烦。

考虑到单行单列染色,记录行列最后染的颜色以及染色时间。

假如有相邻两行,及一列满足竖直条件。

分别记三个时间轴变量 a,b,ca,b,c,若 <0<0 则代表染白色,否则红色,绝对值为时间。

分类讨论按照绝对值排序。

  • abc:c>0,b<0c>0,b<0
  • bca:c<0,a>0c<0,a>0
  • cab:a>0,b<0a>0,b<0
  • cba:a>0,b<0a>0,b<0

每种情况的贡献不重,且总和即为竖直方案数。

a,ba,b 分为以上四类。

cc 分成 >0>0<0<0 两类。

若插入 a,ba,b,那么 cc 的个数可以用两个树状数组求出。

若插入 cc,那么 a,ba,b 的个数可以用四个维护 a,ba,b 的树状数组求出。

时间复杂度可以做到 O(qlogq)\mathcal O(q\log q)

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize("Ofast,fast-math")
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;
#define F(i, a, b) for(int i = a; i <= (b); ++i)
#define F2(i, a, b) for(int i = a; i < (b); ++i)
#define dF(i, a, b) for(int i = a; i >= (b); --i)
template<typename T> void debug(string s, T x) {
	cerr << "[" << s << "] = [" << x << "]\n";
}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {
	for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++;
	else if (s[i] == ')' || s[i] == '}') b--;
	else if (s[i] == ',' && b == 0) {
		cerr << "[" << s.substr(0, i) << "] = [" << x << "] | ";
		debug(s.substr(s.find_first_not_of(' ', i + 1)), args...);
		break;
	}
}
#ifdef ONLINE_JUDGE
#define Debug(...)
#else
#define Debug(...) debug(#__VA_ARGS__, __VA_ARGS__)
#endif
#define pb push_back
#define fi first
#define se second
#define Mry fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0)
#define Try cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
typedef long long ll;
// namespace Fread {const int SIZE = 1 << 17; char buf[SIZE], *S, *T; inline char getchar() {if (S == T) {T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n';} return *S++;}}
// namespace Fwrite {const int SIZE = 1 << 17; char buf[SIZE], *S = buf, *T = buf + SIZE; inline void flush() {fwrite(buf, 1, S - buf, stdout), S = buf;} inline void putchar(char c) {*S++ = c;if (S == T) flush();} struct NTR {~NTR() {flush();}} ztr;}
// #ifdef ONLINE_JUDGE
// #define getchar Fread::getchar
// #define putchar Fwrite::putchar
// #endif
inline int ri() {
	int x = 0;
	bool t = 0;
	char c = getchar();
	while (c < '0' || c > '9') t |= c == '-', c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return t ? -x : x;
}
inline void wi(int x) {
	if (x < 0) {
		putchar('-'), x = -x;
	}
	if (x > 9) wi(x / 10);
	putchar(x % 10 + 48);
}
inline void wi(int x, char s) {
	wi(x), putchar(s);
}
bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3f;
const int _ = 3e6 + 50;

int n, hang[_], lie[_];

struct abc {
	struct Bit {
		int C[_];
		void upd(int x, int v) {
			for(int i = x; i <= 3 * n + 10; i += i & -i) C[i] += v;
		}
		void upd2(int l, int r, int v) {
			upd(l, v), upd(r + 1, -v);
		}
		int qry(int x) {
			int res = 0;
			for(int i = x; i; i -= i & -i) res += C[i];
			return res;
		}
	} tr1, tr2, tr3, tr4;
	/*
	tr1 acb:c>0,b<0
	tr2 bca:c<0,a>0
	tr3 cab:a>0,b<0
	tr4 cba:a>0,b<0
	*/
	struct Bit2 {
		int C[_];
		void upd(int x, int v) {
			for(int i = x; i <= n + n + n + 10; i += i & -i) C[i] += v;
		}
		int qry(int x) {
			int res = 0;
			for(int i = x; i; i -= i & -i) res += C[i];
			return res;
		}
		int qry2(int l, int r) {
			return qry(r) - qry(l - 1);
		}
	} tr5, tr6;
	// tr5 c > 0, tr6 c < 0
	ll ans;
	void updh(int a, int b, int v, bool fl) {
		int aa = abs(a), bb = abs(b);
		if(aa < bb && b < 0) {
			tr1.upd2(aa, bb, v);
			if(fl) ans += v * tr5.qry2(aa, bb);
		}
		if(a > 0 && bb < aa) {
			tr2.upd2(bb, aa, v);
			if(fl) ans += v * tr6.qry2(bb, aa);
		}
		if(a > 0 && b < 0 && aa < bb) {
			tr3.upd2(1, aa, v);
			if(fl) ans += v * (tr5.qry2(1, aa) + tr6.qry2(1, aa));
		}
		if(a > 0 && b < 0 && aa > bb) {
			tr4.upd2(1, bb, v);
			if(fl) ans += v * (tr5.qry2(1, bb) + tr6.qry2(1, bb));
		}
	}
	void updl(int c, int v, bool fl) {
		int cc = abs(c);
		if(c > 0) tr5.upd(cc, v);
		else tr6.upd(cc, v);
		if(fl) {
			if(c > 0) ans += v * tr1.qry(cc);
			if(c < 0) ans += v * tr2.qry(cc);
			ans += v * tr3.qry(cc);
			ans += v * tr4.qry(cc);
		}
	}
} A, B;

bool Med;
signed main() {
	// Mry;
	n = ri();
	// >0 red <0 white
	// x -> time
	F(i, 1, n) hang[i] = i, lie[i] = i + n;
	hang[n + 1] = -(n + n + n + 1), lie[n + 1] = -(n + n + n + 2);
	F(i, 1, n) A.updh(hang[i], hang[i + 1], 1, 0);
	F(i, 1, n) A.updl(lie[i], 1, 1);
	F(i, 1, n) B.updh(lie[i], lie[i + 1], 1, 0);
	F(i, 1, n) B.updl(hang[i], 1, 1);
//	Debug(A.ans, B.ans);
	int ti = n + n;
	int q = n;
	while(q--) {
		int w = ri(), y = ri(), x = ri();
		if(y == 1) {
			A.updh(hang[x], hang[x + 1], -1, 1);
			if(x > 1) A.updh(hang[x - 1], hang[x], -1, 1);
			
			B.updl(hang[x], -1, 1);
			
			++ti;
			if(!w) hang[x] = -ti; else hang[x] = ti;
			A.updh(hang[x], hang[x + 1], 1, 1);
			if(x > 1) A.updh(hang[x - 1], hang[x], 1, 1);
			
			B.updl(hang[x], 1, 1);
			
		} else {
			A.updl(lie[x], -1, 1);
//			Debug("uod", A.ans);
			B.updh(lie[x], lie[x + 1], -1, 1);
			if(x > 1) B.updh(lie[x - 1], lie[x], -1, 1);
			
			++ti;
			if(!w) lie[x] = -ti; else lie[x] = ti;
			A.updl(lie[x], 1, 1);
			
			B.updh(lie[x], lie[x + 1], 1, 1);
			if(x > 1) B.updh(lie[x - 1], lie[x], 1, 1);
			
		}
//		Debug(A.ans, B.ans);
		cout << 2ll * (A.ans + B.ans) << '\n';
	}
	// Try;
	return 0;
}
posted @ 2023-12-11 14:08  蒟蒻orz  阅读(2)  评论(0编辑  收藏  举报  来源