QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#818896 | #4407. 回 | hhoppitree | 100 ✓ | 1316ms | 31864kb | C++14 | 4.9kb | 2024-12-18 10:39:55 | 2024-12-18 10:39:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int opt[N], c1[N], c2[N], c3[N], c4[N];
unsigned int res[N];
struct TreeArray {
int n;
unsigned int s[N];
void resize(int _n) {
n = _n;
for (int i = 1; i <= n; ++i) s[i] = 0;
}
void modify(int x, unsigned int y) {
for (; x <= n; x += x & -x) s[x] += y;
}
unsigned int query(int x) {
unsigned int res = 0;
for (; x; x -= x & -x) res += s[x];
return res;
}
} Sy3, Sxy2, Sy2, Sxy, Sy, Sx, S1;
/*
Sxy - Sx * (y + 1) - Sy * (x + 1) + S1 * (x + 1) * (y + 1)
= Sum w * ((a - b) * (y + b) * (y - b + 1) / 2 + y * (y + 1) * (2 * y + 1) / 6 -
(b - 1) * b * (2 * b - 1) / 6 - (x + 1) * (y - b + 1) * (b + y) / 2 - (y + 1) *
(y - b + 1) * (2 * a - b + y) / 2 + (x + 1) * (y + 1) * (y - b + 1))
= -1/6 * Sum w * (y ^ 3 + (-3 * x - 3 * b + 3 * a) * y ^ 2 + ((6 * b - 9) * x +
3 * b ^ 2 - 6 * a * b + 9 * a - 7) * y + (-3 * b * b + 9 * b - 6) * x - b ^ 3 +
3 * a * b ^ 2 + (7 - 9 * a) * b + 6 * a - 6)
*/
void solve(vector< array<int, 3> > md, vector< array<int, 3> > qr, int D) {
vector<int> lsh;
for (auto [a, b, c] : md) lsh.push_back(a - b + D);
sort(lsh.begin(), lsh.end());
lsh.erase(unique(lsh.begin(), lsh.end()), lsh.end());
auto getId = [&](int x) {
return upper_bound(lsh.begin(), lsh.end(), x) - lsh.begin();
};
auto cmp = [&](array<int, 3> x, array<int, 3> y) {
return x[1] < y[1];
};
sort(md.begin(), md.end(), cmp), sort(qr.begin(), qr.end(), cmp);
Sy3.resize(lsh.size()), Sxy2.resize(lsh.size()), Sy2.resize(lsh.size());
Sxy.resize(lsh.size()), Sy.resize(lsh.size()), Sx.resize(lsh.size()), S1.resize(lsh.size());
for (int i = 0, j = 0; i < (int)qr.size(); ++i) {
while (j < (int)md.size() && md[j][1] <= qr[i][1]) {
int z = getId(md[j][0] - md[j][1] + D);
unsigned int a = md[j][0], b = md[j][1], w = md[j][2];
Sy3.modify(z, w), Sxy2.modify(z, -3 * w), Sy2.modify(z, (-3 * b + 3 * a) * w);
Sxy.modify(z, (6 * b - 9) * w), Sy.modify(z, (3 * b * b - 6 * a * b + 9 * a - 7) * w);
Sx.modify(z, (-3 * b * b + 9 * b - 6) * w);
S1.modify(z, (-b * b * b + 3 * a * b * b + (7 - 9 * a) * b + 6 * a - 6) * w);
++j;
}
unsigned int x = qr[i][0], y = qr[i][1], id = abs(qr[i][2]), v = (qr[i][2] > 0 ? 1 : (1ll << 32) - 1);
int z = getId(qr[i][0] - qr[i][1]);
res[id] += v * (Sy3.query(z) * y * y * y + Sxy2.query(z) * x * y * y + Sy2.query(z) * y * y + Sxy.query(z) * x * y + Sy.query(z) * y + Sx.query(z) * x + S1.query(z));
}
}
void process(vector< array<int, 3> > md, vector< array<int, 3> > qr) {
if (md.empty() || qr.empty()) return;
solve(md, qr, 0);
for (auto &[a, b, c] : md) swap(a, b);
for (auto &[d, e, f] : qr) swap(d, e);
solve(md, qr, 1);
}
void dfs(int l, int r) {
if (l >= r) return;
int mid = (l + r) >> 1;
dfs(l, mid), dfs(mid + 1, r);
vector< array<int, 3> > md1, md2, qr1, qr2;
for (int i = l; i <= mid; ++i) {
if (opt[i] == 1) {
md1.push_back({c1[i] - c3[i] + 1, c2[i] - c3[i] + 1, c4[i]});
md1.push_back({c1[i] + c3[i] + 1, c2[i] + c3[i] + 1, -c4[i]});
md2.push_back({-(c2[i] + c3[i] - 1), c1[i] - c3[i] + 1, c4[i]});
md2.push_back({-(c2[i] - c3[i] - 1), c1[i] + c3[i] + 1, -c4[i]});
md2.push_back({-(c2[i] + c3[i] + (int)5e8), c1[i] - c3[i] + 1, -c4[i]});
md2.push_back({-(c2[i] - c3[i] + (int)5e8), c1[i] + c3[i] + 1, c4[i]});
}
}
for (int i = mid + 1; i <= r; ++i) {
if (opt[i] == 2) {
qr1.push_back({c2[i], c4[i], i});
qr1.push_back({c1[i] - 1, c4[i], -i});
qr1.push_back({c2[i], c3[i] - 1, -i});
qr1.push_back({c1[i] - 1, c3[i] - 1, i});
swap(c1[i], c3[i]), c1[i] = -c1[i];
swap(c2[i], c4[i]), c2[i] = -c2[i];
swap(c1[i], c2[i]);
qr2.push_back({c2[i], c4[i], i});
qr2.push_back({c1[i] - 1, c4[i], -i});
qr2.push_back({c2[i], c3[i] - 1, -i});
qr2.push_back({c1[i] - 1, c3[i] - 1, i});
swap(c1[i], c2[i]);
c1[i] = -c1[i], swap(c1[i], c3[i]);
c2[i] = -c2[i], swap(c2[i], c4[i]);
}
}
process(md1, qr1), process(md2, qr2);
}
signed main() {
int n; scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d%d%d", &opt[i], &c1[i], &c2[i], &c3[i], &c4[i]);
}
dfs(1, n);
for (int i = 1; i <= n; ++i) {
if (opt[i] == 2) {
int P = 1 << 30;
long long ans = (res[i] >> 1) % P;
while (ans % 3) ans += P;
ans /= 3;
ans = (P - ans % P) % P;
printf("%lld\n", ans);
}
}
return 0;
}
Details
Subtask #1:
score: 23
Accepted
Test #1:
score: 23
Accepted
time: 6ms
memory: 8040kb
Test #2:
score: 23
Accepted
time: 6ms
memory: 6096kb
Subtask #2:
score: 8
Accepted
Test #3:
score: 8
Accepted
time: 188ms
memory: 10056kb
Test #4:
score: 8
Accepted
time: 195ms
memory: 8316kb
Subtask #3:
score: 8
Accepted
Test #5:
score: 8
Accepted
time: 418ms
memory: 11128kb
Test #6:
score: 8
Accepted
time: 447ms
memory: 11152kb
Subtask #4:
score: 8
Accepted
Test #7:
score: 8
Accepted
time: 682ms
memory: 13712kb
Test #8:
score: 8
Accepted
time: 711ms
memory: 13612kb
Subtask #5:
score: 8
Accepted
Test #9:
score: 8
Accepted
time: 939ms
memory: 17804kb
Test #10:
score: 8
Accepted
time: 998ms
memory: 17708kb
Subtask #6:
score: 15
Accepted
Test #11:
score: 15
Accepted
time: 317ms
memory: 31864kb
Test #12:
score: 15
Accepted
time: 512ms
memory: 24848kb
Subtask #7:
score: 10
Accepted
Test #13:
score: 10
Accepted
time: 1135ms
memory: 19388kb
Test #14:
score: 10
Accepted
time: 1156ms
memory: 19332kb
Subtask #8:
score: 10
Accepted
Test #15:
score: 10
Accepted
time: 1231ms
memory: 19344kb
Test #16:
score: 10
Accepted
time: 1316ms
memory: 19656kb
Subtask #9:
score: 10
Accepted
Test #17:
score: 10
Accepted
time: 1212ms
memory: 20080kb
Test #18:
score: 10
Accepted
time: 1272ms
memory: 19816kb