This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9+7;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
int N = 2*n+1;
vector<vector<int>> num(N, vector<int> (N));
int x = n, y = n, val = 1;
num[x][y] = val;
for (int i = 1; i <= n+1; i++) { //calculate grid numbers
for (int j = 0; j < i; j++) {
x++; val++;
num[x][y] = val;
}
for (int j = 0; j < i; j++) {
y++; val++;
num[x][y] = val;
}
i++;
for (int j = 0; j < i; j++) {
x--; val++;
num[x][y] = val;
}
for (int j = 0; j < i; j++) {
y--; val++;
num[x][y] = val;
}
}
while (x+1 < N) {
x++; val++;
num[x][y] = val;
}
vector<vector<int>> pref = num;
for (int i = 0; i < N; i++) { //calculate prefix sums
for (int j = 0; j < N; j++) {
if (i > 0) pref[i][j] += pref[i-1][j];
pref[i][j] = (pref[i][j]+mod)%mod;
if (j > 0) pref[i][j] += pref[i][j-1];
pref[i][j] = (pref[i][j]+mod)%mod;
if (i > 0 && j > 0) pref[i][j] -= pref[i-1][j-1];
//~ cout << pref[i][j] << " ";
pref[i][j] = (pref[i][j]+mod)%mod;
}
//~ cout << endl;
}
for (int i = 0; i < q; i++) { //do queries
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1 += n; y1 += n; x2 += n; y2 += n;
int ans = pref[x2][y2];
if (x1 > 0) ans -= pref[x1-1][y2];
ans = (ans+mod)%mod;
if (y1 > 0) ans -= pref[x2][y1-1];
ans = (ans+mod)%mod;
if (x1 > 0 && y1 > 0) ans += pref[x1-1][y1-1];
ans = (ans+mod)%mod;
//~ if (ans < 0) return 1; //answer is always at least 0 --> what goes wrong then
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |