結果

問題 No.3464 Max and Sum on Grid
コンテスト
ユーザー GOTKAKO
提出日時 2026-03-02 19:30:42
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 528 ms / 5,000 ms
コード長 1,899 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,393 ms
コンパイル使用メモリ 225,576 KB
実行使用メモリ 15,680 KB
最終ジャッジ日時 2026-03-02 19:30:50
合計ジャッジ時間 7,510 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

const int G = 100,n = 20000;
int A[n],B[n];
int C[G][n];
long long S[n];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,Q; cin >> N >> Q;
    for(int i=0; i<N; i++) cin >> A[i];
    for(int i=0; i<N; i++) cin >> B[i];

    vector<long long> answer(Q);
    vector<vector<int>> Qs(N);
    vector<tuple<int,int,int,int>> Query(Q);
    for(int i=0; i<Q; i++){
        auto &[l,d,r,u] = Query.at(i);
        cin >> l >> d >> r >> u,l--,d--,u--;
        Qs.at(l/G).push_back(i);
    }
    vector<int> hold;
    for(int i=0; i<N; i+=G){
        for(int k=0; k<G; k++){
            if(i+k == N) break;
            int a = A[i+k];
            long long sum = 0;
            for(int y=0; y<N; y++){
                int v = max(a,B[y]);
                sum += v,C[k][y] = sum;
                if(k == 0) S[y] = sum;
                else S[y] += sum;
            }
        }
        vector<int> next;
        for(auto qpos : hold){
            auto &[d,l,u,r] = Query.at(qpos);
            if(i+G <= u){
                answer.at(qpos) += S[r];
                if(l) answer.at(qpos) -= S[l-1];
                next.push_back(qpos);
            }
            else{
                for(int k=0; k<G; k++){
                    if(i+k == u) break;
                    answer.at(qpos) += C[k][r];
                    if(l) answer.at(qpos) -= C[k][l-1];
                }
            }
        }
        for(auto qpos : Qs.at(i/G)){
            auto &[d,l,u,r] = Query.at(qpos);
            for(int k=0; k<G; k++){
                if(i+k < d) continue;
                if(i+k == u) break;
                answer.at(qpos) += C[k][r];
                if(l) answer.at(qpos) -= C[k][l-1];
            }
            if(i+G <= u) next.push_back(qpos);
        }
        swap(hold,next);
    }

    for(auto a : answer) cout << a << "\n";
}
0