제출 #1353260

#제출 시각아이디문제언어결과실행 시간메모리
1353260kl0989eSpiral (BOI16_spiral)C++20
31 / 100
0 ms352 KiB
#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,fma")

#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define pi pair<int, int>
#define pl pair<ll,ll>
#define all(x) (x).begin(),(x).end()

const int mod=1e9+7;

int get(int x, int y) {
	ll mx=max(abs(x),abs(y));
	mx--;
	ll t=(2ll*mx+1)*(2ll*mx+1)%mod;
	if (x==mx+1 && y==-mx-1) {
		return (2ll*mx+3)*(2ll*mx+3)%mod;
	}
	if (x==mx+1) {
		return (t+mx+1+y)%mod;
	}
	t+=(mx+1)*2;
	t%=mod;
	if (y==mx+1) {
		return (t+mx+1-x)%mod;
	}
	t+=(mx+1)*2;
	t%=mod;
	if (x==-mx-1) {
		return (t+mx+1-y)%mod;
	}
	t+=(mx+1)*2;
	t%=mod;
	return (t+mx+1+x)%mod;
}

int getupright(ll x, ll y) {
	ll mn=min(x,y);
	ll t=0;
	// ((2x+1)**2+2+2x)*(2x+1)
	// (4x**2+6x+3)*(2x+1)
	// 8x**3 + 16x**2 + 12x + 3
	t+=(3*mn%mod) + (mn*(mn-1)*6%mod) + ((mn-1)*mn%mod*(2*mn-1)%mod*8%mod*333333336%mod) + (mn*mn%mod*(mn-1)%mod*(mn-1)%mod*2%mod);
	t%=mod;
	if (x>y) {
		ll num=get(y+1,1);
		t+=num*y%mod*(x-y)%mod;
		t%=mod;
		t+=y*(y-1ll)/2%mod*(x-y)%mod;
		t%=mod;
		// [y+1,x-1]
		// (1+8x)*(X-x)
		// X + 8xX -x -8x**2 = -8x**2 + (8X-1)*x + X
		if (x-y>=2) {
			t+=((x-1)*x%mod*(2*x-1)%mod*-4%mod*333333336%mod-(y)*(y+1)%mod*(2*y+1)%mod*-4%mod*333333336%mod)*y%mod;
			t+=((8*x-1)%mod*(x*(x-1)/2%mod)%mod-(8*x-1)%mod*(y*(y+1)/2%mod)%mod)*y%mod;
			t+=((x-y-1)*x%mod)*y%mod;
			t%=mod;
			t=(t+mod)%mod;
		}
	}
	else if (y>x) {
		ll num=get(x,x+1);
		t+=num*x%mod*(y-x)%mod;
		t%=mod;
		t+=x*(x-1ll)/2%mod*(y-x)%mod;
		t%=mod;
		// [x+1,y-1]
		// (3+8z)*(y-z)
		// 3y -3z + 8zy - 8z**2 = -8*z**2 + (8y-3)*z + 3y
		if (y-x>=2) {
			t+=((y-1)*y%mod*(2*y-1)%mod*-4%mod*333333336%mod-(x)*(x+1)%mod*(2*x+1)%mod*-4%mod*333333336%mod)*x%mod;
			t+=((8*y-3)%mod*(y*(y-1)/2%mod)%mod-(8*y-3)%mod*(x*(x+1)/2%mod)%mod)*x%mod;
			t+=((y-x-1)*3*y%mod)*x%mod;
			t%=mod;
			t=(t+mod)%mod;
		}
	}
	return t;
}

int32_t main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);
	int n,q;
	cin >> n >> q;
	int x1,y1,x2,y2;
	for (int i=0; i<q; i++) {
		cin >> x1 >> y1 >> x2 >> y2;
		cout << getupright(x2,y2) << '\n';
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...