結果

問題 No.3665 Two Important Tasks
コンテスト
ユーザー GOTKAKO
提出日時 2026-09-19 06:39:46
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 3,224 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,611 ms
コンパイル使用メモリ 230,824 KB
実行使用メモリ 55,308 KB
最終ジャッジ日時 2026-09-19 06:40:20
合計ジャッジ時間 20,444 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6 RE * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

    int N,Q; cin >> N >> Q;
    vector<vector<int>> P(N);
    vector<tuple<int,int,long long>> LRC(N); 
    vector<vector<int>> Ls(N+1),Rs(N+1);
    for(int i=0; i<N; i++){
        auto &[l,r,c] = LRC.at(i);
        cin >> l >> r >> c,l--;
        for(int d=l; d<r; d++) P.at(d).push_back(i);
        Ls.at(l).push_back(i);
        Rs.at(r).push_back(i);
    }

    auto DP = [&](int l,int r,bool rev) -> vector<long long> {
        int n = r-l;
        vector<long long> dp(n+1);
        if(rev == false){
            for(int i=0; i<n; i++){
                dp.at(i+1) = max(dp.at(i+1),dp.at(i));
                for(auto pos : Ls.at(i+l)){
                    auto &[a,b,c] = LRC.at(pos);
                    if(b <= r) dp.at(b-l) = max(dp.at(b-l),dp.at(i)+c);
                }
            }
        }
        else{
            for(int i=n; i>0; i--){
                dp.at(i-1) = max(dp.at(i-1),dp.at(i));
                for(auto pos : Rs.at(i+l)){
                    auto &[a,b,c] = LRC.at(pos);
                    if(a >= l) dp.at(a-l) = max(dp.at(a-l),dp.at(i)+c);
                }
            }
        }
        return dp;
    };

    vector<long long> answer(Q);
    auto f = [&](auto f,int l,int r,vector<tuple<int,int,int>> Qs) -> void {
        if(Qs.size() == 0) return;
        if(l+1 == r){
            long long big = 0;
            for(auto pos : Ls.at(l)){
                auto [a,b,c] = LRC.at(pos);
                if(b == r) big = max(big,c);
            }
            for(auto [a,b,qpos] : Qs){
                assert(a == l && b == r);
                answer.at(qpos) += big;
                
            }
            return;
        }
        int m = (l+r)/2;
        vector<tuple<int,int,int>> Lq,Rq,Mq;
        for(auto [a,b,qpos] : Qs){
            if(b <= m) Lq.push_back({a,b,qpos});
            else if(a >= m) Rq.push_back({a,b,qpos});
            else Mq.push_back({a,b,qpos});
        }
        f(f,l,m,Lq),f(f,m,r,Rq);
        int n = Mq.size();
        vector<long long> best(n);
        for(auto pos : P.at(m)){
            auto [a1,b1,c] = LRC.at(pos);
            if(!(l <= a1 && b1 <= r)) continue;
            auto dpl = DP(l,a1,true),dpr = DP(b1,r,false);
            for(int i=0; i<n; i++){
                auto [a,b,qpos] = Mq.at(i);
                best.at(i) = max(best.at(i),dpl.at(a)+dpr.at(b)+c);
            } 
        }
        for(int i=0; i<n; i++){
            auto [a,b,qpos] = Mq.at(i);
            answer.at(qpos) += best.at(i);
        }
    };

    vector<tuple<int,int,int>> Query;
    auto DPL = DP(0,N,false),DPR = DP(0,N,true);
    for(int i=0; i<Q; i++){
        int a,b; cin >> a >> b,a--,b--;
        auto [l1,r1,c1] = LRC.at(a); 
        auto [l2,r2,c2] = LRC.at(b);
        if(max(l1,l2) < min(r1,r2)) answer.at(i) = -1;
        else{
            answer.at(i) = c1+c2;
            answer.at(i) += DPL.at(min(l1,l2));
            answer.at(i) += DPR.at(max(r1,r2));
            if(max(l1,l2) != min(r1,r2)) Query.push_back({min(r1,r2),max(l1,l2),i});
        }
    }
    f(f,0,N,Query);
    for(auto a : answer) cout << a << "\n";
}
0