結果
| 問題 | No.3464 Max and Sum on Grid |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-28 16:37:49 |
| 言語 | C++23(gnu拡張) (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,665 ms / 5,000 ms |
| コード長 | 2,221 bytes |
| 記録 | |
| コンパイル時間 | 8,343 ms |
| コンパイル使用メモリ | 395,088 KB |
| 実行使用メモリ | 11,776 KB |
| 最終ジャッジ日時 | 2026-02-28 16:38:16 |
| 合計ジャッジ時間 | 26,034 ms |
|
ジャッジサーバーID (参考情報) |
judge7 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)
template <class T> bool chmin(T& x, T y) {
return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
return x < y ? (x = y, true) : false;
}
void solve() {
int n, q;
cin >> n >> q;
vector<ll> a(n), b(n);
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
int bsz = 200;
vector<vector<int>> bv;
vector<vector<ll>> bvsm;
int m = 0;
for (int i = 0; i < n; i += bsz) {
m++;
bv.push_back({});
rep(j, i, min(i + bsz, n)) {
bv.back().push_back(b[j]);
}
sort(all(bv.back()));
vector<ll> sm{0};
for (auto x : bv.back()) sm.push_back(sm.back() + x);
bvsm.push_back(sm);
}
vector<ll> bsm(m, 0);
int mx_ab = 1e5 + 10;
atcoder::fenwick_tree<int> ft(mx_ab);
atcoder::fenwick_tree<ll> ft_sm(mx_ab);
auto add_x = [&](int x) {
rep(i, 0, m) {
int mnx = lower_bound(all(bv[i]), x) - bv[i].begin();
bsm[i] += (ll)mnx * x;
bsm[i] += bvsm[i].back() - bvsm[i][mnx];
}
ft.add(x, 1);
ft_sm.add(x, x);
};
auto prod = [&](int l, int r) {
int bl = (l + bsz - 1) / bsz;
int br = r / bsz;
ll ans = 0;
if (bl <= br) {
rep(i, l, bl * bsz) {
ans += ft.sum(0, b[i]) * b[i];
ans += ft_sm.sum(b[i], mx_ab);
}
rep(i, bl, br) {
ans += bsm[i];
}
rep(i, br * bsz, r) {
ans += ft.sum(0, b[i]) * b[i];
ans += ft_sm.sum(b[i], mx_ab);
}
} else {
rep(i, l, r) {
ans += ft.sum(0, b[i]) * b[i];
ans += ft_sm.sum(b[i], mx_ab);
}
}
return ans;
};
vector<ll> ans(q, 0);
vector<vector<array<int, 4>>> query(n + 1);
rep(i, 0, q) {
int l, d, r, u;
cin >> l >> d >> r >> u;
l--, d--;
query[l].push_back({d, u, (int)i, -1});
query[r].push_back({d, u, (int)i, 1});
}
for (auto [l, r, i, d] : query[0]) {
ans[i] += prod(l, r) * d;
}
rep(i, 0, n) {
add_x(a[i]);
for (auto [l, r, j, d] : query[i + 1]) {
ans[j] += prod(l, r) * d;
}
}
rep(i, 0, q) cout << ans[i] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int t = 1;
// cin >> t;
while (t--) solve();
}