結果

問題 No.3265 地元に帰れば天才扱い!
ユーザー Leal-0
提出日時 2025-09-06 19:06:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,196 ms / 2,500 ms
コード長 3,734 bytes
コンパイル時間 3,476 ms
コンパイル使用メモリ 236,148 KB
実行使用メモリ 33,240 KB
最終ジャッジ日時 2025-09-06 19:06:41
合計ジャッジ時間 33,647 ms
ジャッジサーバーID
(参考情報)
judge / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define srep(i,l,r) for(ll i=l;i<=r;i++)
#define irep(i,r,l) for(ll i=r;i>=l;i--)
using ll = long long;
using ld = long double;
constexpr ll mod = 998244353ll;
#define vout(v) for(auto i :v) cout<<i<<" ";
#define INF 9223300000000000000ll
#define Winf 5e12
#define nl "\n"
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define vl vector<ll>
#define pb push_back

#define vc vector<char>
#define vb vector<bool>
#define uniq(x) sort((x).begin(),(x).end());(x).erase(unique((x).begin(),(x).end()),(x).end())
#define eb emplace_back



void no(void) { cout<<"No"<<nl;}
void yes(void) {cout<<"Yes"<<nl;}
void yn(bool a) {
    cout<<(a ? "Yes":"No")<<nl;
}


vector<ll> dx={-1,0,1,0,1,1,-1,-1};
vector<ll> dy={0,-1,0,1,-1,1,-1,1};

bool isin(ll i,ll j,ll h,ll w) {
    if(i<0 || i>=h || j<0 || j>=w) return false;
    return true;
}

template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool cfSYS_perf_event_open(T& a, T b){if(a < b){a = b; return true;} return false;}


template<class T>T vefSYS_perf_event_open(const vector<T>&v){return fSYS_perf_event_open_element(all(v));}
template<class T>T vecmin(const vector<T>&v){return *min_element(all(v));}

ll safemod(ll num,ll rule) {
    return (num%rule+rule)%rule;
}
//alias ojb='oj-bundle -I /home/leal/beginner/atc/library'
ll sum(vector<ll> &a) {
    return accumulate(all(a),0ll);
}


#include <atcoder/lazysegtree>
#include <atcoder/segtree>
struct S1{
    long long value;
    int size;
};
using F1 = long long;

S1 op(S1 a, S1 b){ return {a.value+b.value, a.size+b.size}; }
S1 e(){ return {0,0}; }
S1 mapping(F1 f, S1 x){ return {x.value + f*x.size, x.size}; }
F1 composition(F1 f, F1 g){ return f+g; }
F1 id(){ return 0; }

ll op2(ll a,ll b)  {
    return a+b;
}
ll e2() {
    return 0ll;
}

int main() {
    ll n,m; cin>>n>>m;
    vl a(n),l(n),r(n);
    rep(i,n) cin>>a[i]>>l[i]>>r[i];
    rep(i,n) l[i]--;
    rep(i,n) r[i]--;
    ll q; cin>>q;
    vl x(q),y(q),u(q),v(q);
    rep(i,q) cin>>x[i]>>y[i]>>u[i]>>v[i];
    rep(i,q) x[i]--;
    rep(i,q) y[i]--;
    rep(i,q) u[i]--;
    rep(i,q) v[i]--;
    vector<S1> def(m);
    rep(i,m) def[i]={0,1};
    atcoder::lazy_segtree<S1,op,e,F1,mapping,composition,id> lazy(def);
    atcoder::segtree<ll,op2,e2> seg(m);
    vector<ll> h(n); //各いえ
    rep(i,n) h[i]=i;
    ll sum1=0;
    rep(i,n) sum1+=a[i]*(r[i]-l[i]+1);

    rep(i,n) {
        ll L=max(0ll,l[i]);
        ll R=min(r[i],m-1);
        if(L<=R) lazy.apply(L,R+1,1);
    }

    rep(i,n) seg.set(i,a[i]);

    ll sum2=0;
    rep(i,m) {
        ll ci=lazy.get(i).value;
        if(ci>0) sum2+=seg.get(i)*ci;
    }

    function<void(ll,ll,ll)> applychange=[&](ll L,ll R,ll d){
        L=max(0LL,L);
        R=min(R,m-1);
        if(L>R) return;
        ll sumB=0;
        sumB=seg.prod(L,R+1);
        sum2+=d*sumB;
        lazy.apply(L,R+1,d);
    };

    function<void(ll,ll)> applymove=[&](ll pos,ll delta){
        if(pos<0 || pos>=m) return;
        sum2-=seg.get(pos)*lazy.get(pos).value;
        if(delta>0) seg.set(pos, delta);
        else seg.set(pos,0);
        sum2+=seg.get(pos)*lazy.get(pos).value;
    };

    rep(k,q){
        ll xi=x[k],yi=y[k],ui=u[k],vi=v[k];
        ll oldhs=h[xi];
        ll ai=a[xi];
        ll oldl=r[xi]-l[xi]+1;
        ll newl=vi-ui+1;
        sum1+=ai*(newl-oldl);
        applychange(l[xi],r[xi],-1);
        applychange(ui,vi,1);

        if(oldhs!=-1) applymove(oldhs,-ai);
        applymove(yi,ai);
        h[xi]=yi;
        l[xi]=ui;
        r[xi]=vi;
        cout<<sum1-sum2<<nl;
    }

}
0