QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#319194#6673. Be Careful 2IshyML 0ms0kbC++147.2kb2024-02-02 09:09:482024-02-02 09:09:48

Judging History

你现在查看的是最新测评结果

  • [2024-02-02 09:09:48]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-02-02 09:09:48]
  • 提交

answer

// Sea, You & Me
#include<bits/stdc++.h>
#define LL long long
#define DB double
#define MOD 998244353
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define lowbit(x) ((-x) & x)
#define MP make_pair
#define MT make_tuple
#define VI vector<int>
#define VL vector<LL>
#define VII VI::iterator
#define VLI VL::iterator
#define all(x) x.begin(), x.end()
#define EB emplace_back
#define PII pair<int, int>
#define SI set<int>
#define SII SI::iterator
#define fi first
#define se second
using namespace std;
template<typename T> void chkmn(T &a, const T b) { if(a > b) a = b; }
template<typename T> void chkmx(T &a, const T b) { if(a < b) a = b; }
void Inc(int &a, const int &b) { ((a += b) >= MOD) && (a -= MOD); }
void Dec(int &a, const int &b) { ((a -= b) < 0) && (a += MOD); }
void Mul(int &a, const int &b) { a = 1LL * a * b % MOD; }
void Sqr(int &a) { a = 1LL * a * a % MOD; }
int inc(const int &a, const int &b) { return (a + b >= MOD) ? a + b - MOD : a + b; }
int dec(const int &a, const int &b) { return (a - b < 0) ? a - b + MOD : a - b; }
int mul(const int &a, const int &b) { return 1LL * a * b % MOD; }
int sqr(const int &a) { return 1LL * a * a % MOD; }
int qwqmi(int x, int k = MOD - 2)
{
	int res = 1;
	while(k)
	{
		if(k & 1) Mul(res, x);
		k >>= 1, Sqr(x);
	}
	return res;
}
template<typename T> void read(T &x)
{
	x = 0;
	int f = 1;
	char ch = getchar();
	while(!isdigit(ch))
	{
		if(ch == '-')
			f = -1;
		ch = getchar();
	}
	while(isdigit(ch))
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	x = x * f;
}
const int N = 5e3 + 5;
const int INF = 1e9 + 7;
int n, m, K;
PII p[N];
struct Rec
{
	int l, r, u, d;
	Rec(const int L = 0, const int R = 0, const int U = 0, const int D = 0){
		l = L, r = R, u = U, d = D;
	}
	friend bool operator < (Rec A, Rec B)
	{
		if(A.l != B.l) return A.l < B.l;
		if(A.r != B.r) return A.r < B.r;
		if(A.u != B.u) return A.u < B.u;
		return A.d < B.d;
	}
	friend bool operator == (Rec A, Rec B)
	{
		return (A.l == B.l && A.r == B.r && A.u == B.u && A.d == B.d);
	}
	friend bool operator != (Rec A, Rec B)
	{
		return (A.l != B.l || A.r != B.r || A.u != B.u || A.d != B.d);
	}
}a[N * N * 4]; int cnt;
map<PII, int> mp;
map<int, int> mpx;
map<int, int> mpy;
int X[N], xcnt;
int Y[N], ycnt;
set<int> Sx[N];
set<int> Sy[N];
int calc_coef(int l, int r, int u, int d)
{
	PII w = MP(l, d), x = MP(l, u), y = MP(r, u), z = MP(r, d);
	set<int> S1 = Sx[mpx[l]], S2 = Sy[mpy[u]], S3 = Sx[mpx[r]], S4 = Sy[mpy[d]];
	int vw = mp[w], vx = mp[x], vy = mp[y], vz = mp[z];
	int v1 = 0, v2 = 0, v3 = 0, v4 = 0;
	SII it1 = S1.upper_bound(d); if(it1 != S1.end() && *it1 < u) v1 = 1;
	SII it2 = S2.upper_bound(l); if(it2 != S2.end() && *it2 < r) v2 = 1;
	SII it3 = S3.upper_bound(d); if(it3 != S3.end() && *it3 < u) v3 = 1;
	SII it4 = S4.upper_bound(l); if(it4 != S4.end() && *it4 < r) v4 = 1;
	if(l == r && u == d) return -1;
	if(l == r) return (v1 == 0 ? 1 : 0);
	if(u == d) return (v2 == 0 ? 1 : 0);
	int cnt = vw + vx + vy + vz;
	int emp = (v1 == 0 && v2 == 0 && v3 == 0 && v4 == 0);
	// choose 0
	int res = (v1 == 1 && v2 == 1 && v3 == 1 && v4 == 1); 
	if(cnt == 0) return res;
	// choose 1
	if(vw == 1) res -= (v1 == 0 && v4 == 0 && v2 == 1 && v3 == 1);
	if(vx == 1) res -= (v1 == 0 && v2 == 0 && v3 == 1 && v4 == 1);
	if(vy == 1) res -= (v2 == 0 && v3 == 0 && v1 == 1 && v4 == 1);
	if(vz == 1) res -= (v3 == 0 && v4 == 0 && v1 == 1 && v2 == 1);
	if(cnt == 1) return res;
	// choose 2
	if(vw == 1 && vx == 1) res += (v1 == 0 && v2 == 0 && v4 == 0 && v3 == 1);
	if(vx == 1 && vy == 1) res += (v1 == 0 && v2 == 0 && v3 == 0 && v4 == 1);
	if(vy == 1 && vz == 1) res += (v2 == 0 && v3 == 0 && v4 == 0 && v1 == 1);
	if(vz == 1 && vw == 1) res += (v1 == 0 && v3 == 0 && v4 == 0 && v2 == 1);
	if(vw == 1 && vy == 1) res += emp;
	if(vx == 1 && vz == 1) res += emp;
	if(cnt == 2) return res;
	// choose 3
	if(vw == 1 && vx == 1 && vy == 1) res -= emp;
	if(vx == 1 && vy == 1 && vz == 1) res -= emp;
	if(vy == 1 && vz == 1 && vw == 1) res -= emp;
	if(vz == 1 && vw == 1 && vx == 1) res -= emp;
	if(cnt == 3) return res;
	if(vw == 1 && vx == 1 && vy == 1 && vz == 1) res += emp;
	return res;
}
const int inv6 = qwqmi(6);
const int inv4 = qwqmi(4);
const int inv30 = qwqmi(30);
int sum2(int x) { return mul(inv6, mul(x, mul(x + 1, 2 * x + 1))); } 
int sum3(int x) { return mul(inv4, mul(sqr(x), sqr(x + 1))); } 
int sum4(int x) { return mul(inv30, mul(x, mul(x + 1, mul(2 * x + 1, dec(mul(3, mul(x, x + 1)), 1))))); }
int cx[2], cy[2]; // w(len) = c[0] + c[1] * len
int func(int l, int r)
{
	l = max(0, l - 1);
	int c2 = mul(cx[0], cy[0]);
	int c3 = inc(mul(cx[0], cy[1]), mul(cx[1], cy[0]));
	int c4 = mul(cx[1], cy[1]);
	int res = 0;
	Inc(res, mul(c2, dec(sum2(r), sum2(l))));
	Inc(res, mul(c3, dec(sum3(r), sum3(l))));
	Inc(res, mul(c4, dec(sum4(r), sum4(l))));
	return res;
};
int calc_val(int l, int r, int u, int d)
{
	--l, ++r, --d, ++u;
	if(l < 0 || d < 0 || r > n || u > m) return 0;
	int mn = max(r - l, u - d), mx = min(n, m);
	vector<int> vec = {mn, mx + 1, r + 1, u + 1, n - l + 1, m - d + 1};
	sort(vec.begin(), vec.end());
	auto coef = [&](int len, int l, int r, int lim, int *c)
	{
		if(len <= lim - l && len <= r) c[0] = l - r + 1, c[1] = 1;
		else if(len <= lim - l && len > r) c[0] = l + 1, c[1] = 0;
		else if(len > lim - l && len <= r) c[0] = lim - r + 1, c[1] = 0;
		else c[0] = lim + 1, c[1] = MOD - 1;
	};
	int res = 0;
	for(int i = 0; i + 1 < (int)vec.size(); ++i)
	{
		if(vec[i] < mn) continue;
		if(vec[i] > mx) break;
		if(vec[i] == vec[i + 1]) continue;
		coef(vec[i], l, r, n, cx);
		coef(vec[i], d, u, m, cy);
		Inc(res, func(vec[i], vec[i + 1] - 1));
	}
	return res;
}
int main()
{
	read(n), read(m), read(K);
	for(int i = 1; i <= K; ++i)
	{
		read(p[i].fi), read(p[i].se);
		X[++xcnt] = p[i].fi;
		Y[++ycnt] = p[i].se;
		mp[p[i]] = 1;
	}
	sort(p + 1, p + K + 1);
	sort(X + 1, X + K + 1);
	sort(Y + 1, Y + K + 1);
	xcnt = unique(X + 1, X + K + 1) - (X + 1);
	ycnt = unique(Y + 1, Y + K + 1) - (Y + 1);
	for(int i = 1; i <= xcnt; ++i) mpx[X[i]] = i;
	for(int i = 1; i <= ycnt; ++i) mpy[Y[i]] = i;
	for(int i = 1; i <= K; ++i)
	{
		Sx[mpx[p[i].fi]].insert(p[i].se);
		Sy[mpy[p[i].se]].insert(p[i].fi);
	}
	for(int i = 1; i <= K; ++i)
	{
		int U = INF, D = -INF;
		a[++cnt] = Rec(p[i].fi, p[i].fi, p[i].se, p[i].se);
		for(int j = i + 1; j <= K; ++j)
		{
			if(p[j].se >= U || p[j].se <= D) continue;
			int mn = min(p[i].se, p[j].se);
			int mx = max(p[i].se, p[j].se);
			a[++cnt] = Rec(p[i].fi, p[j].fi, mx, mn);
			a[++cnt] = Rec(p[i].fi, p[j].fi, mx, D);
			a[++cnt] = Rec(p[i].fi, p[j].fi, U, mn);
			a[++cnt] = Rec(p[i].fi, p[j].fi, U, D);
		}
	}
	sort(a + 1, a + cnt + 1);
	cnt = unique(a + 1, a + cnt + 1) - (a + 1);
	cx[0] = n + 1, cx[1] = -1, cy[0] = m + 1, cy[1] = -1;
	int ans = func(1, min(n, m));
	for(int i = 1; i <= cnt; ++i)
	{
		if(a[i].u == INF) continue;
		if(a[i].d == -INF) continue;
//		cerr << a[i].l << ' ' << a[i].r << ' ' << a[i].u << ' ' << a[i].d << '\n';
		int c = calc_coef(a[i].l, a[i].r, a[i].u, a[i].d);
		int f = calc_val(a[i].l, a[i].r, a[i].u, a[i].d);
		if(c < 0) c += MOD;
		Inc(ans, mul(c, f));
	}
	printf("%d", ans);
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

3 3 1
2 2

output:

21

result: