結果
| 問題 | No.3464 Max and Sum on Grid |
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2026-02-28 16:29:12 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 3,704 ms / 5,000 ms |
| コード長 | 4,370 bytes |
| 記録 | |
| コンパイル時間 | 6,821 ms |
| コンパイル使用メモリ | 389,996 KB |
| 実行使用メモリ | 20,992 KB |
| 最終ジャッジ日時 | 2026-02-28 16:30:11 |
| 合計ジャッジ時間 | 43,417 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
// using mint = modint1000000007;
// const int mod = 1000000007;
// using mint = modint998244353;
// const int mod = 998244353;
// const int INF = 1e9;
// const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define P pair<int, int>
template<typename A, typename B> inline bool chmax(A &a, const B &b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A &a, const B &b) { if (a > b) { a = b; return true; } return false; }
pair<long long, long long> op(pair<long long, long long> a, pair<long long, long long> b) {
return { a.first + b.first,a.second + b.second };
}
pair<long long, long long> e() { return { 0,0 }; }
struct Mo {
int width;
std::vector<int>left, right, order;
//std::vector<long long>horder;
int n, q;
Mo(int _n, int _q) :n(_n), q(_q) {
width = n / min<int>(n, std::sqrt(q));
order.resize(q);
std::iota(order.begin(), order.end(), 0);
}
// add query[l,r)
void query(const std::vector<int> &l, const std::vector<int> &r) {
left = l;
right = r;
/*
horder.resize(q);
for (int i = 0; i < q; ++i) horder[i] = hilbertorder(left[i], right[i]);
*/
}
// run
void run(const std::function<void(int, int, int)> &upd, const std::function<void(int)> &set) {
std::sort(order.begin(), order.end(), [&](int lh, int rh) {
//return horder[lh] < horder[rh];
int lblock = left[lh] / width;
int rblock = left[rh] / width;
if (lblock != rblock)return lblock < rblock;
if (lblock & 1) return right[lh] < right[rh];
return right[lh] > right[rh];
});
int nl = 0, nr = 0;
for (auto idx : order) {
while (nl > left[idx])upd(0, --nl, nr);
while (nr < right[idx])upd(1, nl, nr++);
while (nl < left[idx])upd(2, nl++, nr);
while (nr > right[idx])upd(3, nl, --nr);
set(idx);
}
}
// TLEするときはこっちを使うと通るかも
/*const int logn = 20;
const int maxn = 1 << logn;
long long hilbertorder(int x, int y) {
long long d = 0;
for (int s = 1 << logn - 1; s; s >>= 1) {
bool rx = x & s, ry = y & s;
d = d << 2 | rx * 3 ^ static_cast<int>(ry);
if (ry) continue;
if (rx) {
x = maxn - x;
y = maxn - y;
}
std::swap(x, y);
}
return d;
}*/
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q; cin >> n >> q;
vector<int>a(n), b(n);
rep(i, n)cin >> a[i];
rep(i, n)cin >> b[i];
const int sz = 100000;
segtree<pair<long long, long long>, op, e>segH(sz + 1);
segtree<pair<long long, long long>, op, e>segW(sz + 1);
Mo mo(n, q * 4);
vector<int>ls, rs;
rep(i, q) {
long l, d, r, u; cin >> l >> d >> r >> u;
l--, d--;
ls.push_back(l);
rs.push_back(d);
ls.push_back(r);
rs.push_back(u);
ls.push_back(l);
rs.push_back(u);
ls.push_back(r);
rs.push_back(d);
}
mo.query(ls, rs);
long long val = 0;
auto upd = [&](int type, int l, int r) {
if (type == 0) {// l--
// cout << a[l] << "__" << n + 1 << endl;
auto p = segW.prod(a[l], sz + 1);
val -= p.first;
val -= a[l] * (r - p.second);
segH.set(a[l], op({ -a[l],-1 }, segH.get(a[l])));
}
else if (type == 1) {// r++
// cout << r << endl;
// cout << b[r] << "!!" << n + 1 << endl;
auto p = segH.prod(b[r], sz + 1);
val += p.first;
val += b[r] * (l - p.second);
segW.set(b[r], op({ b[r],1 }, segW.get(b[r])));
}
else if (type == 2) {// l++
// cout << l << endl;
//cout << a[l] << "_" << n + 1 << endl;
auto p = segW.prod(a[l], sz + 1);
val += p.first;
val += a[l] * (r - p.second);
segH.set(a[l], op({ a[l],1 }, segH.get(a[l])));
}
else if (type == 3) {// r--
//cout << b[r] << "!" << n + 1 << endl;
auto p = segH.prod(b[r], sz + 1);
val -= p.first;
val -= b[r] * (l - p.second);
segW.set(b[r], op({ -b[r],-1 }, segW.get(b[r])));
}
};
vector<long long>ans(q);
auto set = [&](int idx) {
int i = idx / 4;
int j = idx % 4;
if (j < 2)ans[i] += val;
else ans[i] -= val;
};
mo.run(upd, set);
rep(i, q)cout << ans[i] << endl;
return 0;
}
//tlきついかねー
kwm_t