Submission #1353105

#TimeUsernameProblemLanguageResultExecution timeMemory
1353105kl0989eSpiral (BOI16_spiral)C++20
0 / 100
0 ms344 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) {
	int mx=max(abs(x),abs(y));
	mx--;
	ll t=(2ll*mx+1)*(2ll*mx+1);
	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;
	if (y==mx+1) {
		return (t+mx+1-x)%mod;
	}
	t+=(mx+1)*2;
	if (x==-mx-1) {
		return (t+mx+1-y)%mod;
	}
	t+=(mx+1)*2;
	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;
		ll add=(1+8*(y+1))%mod;
		ll add2=(1+8*(x-1))%mod;
		t+=(add+add2)*(x-y-1)/2%mod*y%mod;
		t%=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;
		ll add=(3+8*(x+1))%mod;
		ll add2=(3+8*(y-1))%mod;
		t+=(add+add2)*(y-x-1)/2%mod*x%mod;
		t%=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...