3 solutions

  • 0
    @ 2026-7-31 15:56:43

    A. 烟火 标程深度学习:[利用 multiset 与显式区间边界维护的进阶 Slope Trick 架构]

    基础信息:省选/NOI-(紫题) | 进阶 Slope Trick、函数极值维护 【隔离警告:本模式仅用于彻底吸收、学习优秀标程。绝对禁止在此模式下挑错或执行模式四的调试操作!】

    1. 思路来源与解法取舍

    常规的 Slope Trick 通常使用两个 priority_queue 配合全局 tag 来隐式地维护凸函数的折线。但这份标程的作者看透了凸函数底部的本质:最优解其实就是一段斜率为 00 的平底区间(或单点)。 作者摒弃了常规“盲目入堆出堆”的黑盒写法,主动设计了一个 ds (Data Structure) 结构体,显式地锁死了极小值区间的左右端点 t1 和 t2。这种写法的最大好处是:在面对时间推移带来的区间扩张时,只需要 O(1)O(1) 操作 t1 -= x; t2 += x;,逻辑上极其直观;同时利用 multiset 的二分查找能力,精准定位斜率发生突变的相对位置。这是一种在几何意义上更高级、控制力更强的封装取舍。

    2. 核心代码逐块拆解

    • extend(int x) 模块(全局时间推演): 烟火问题中,随着时间流逝,你可以向左或向右移动。在代价函数上的体现就是:原本的极小值区间 [t1,t2][t1, t2] 会向两边无损扩张。作者直接操作 t1 -= x; t2 += x;,完美对应了凸函数谷底的延伸。
    • add(int x) 模块(代价函数折线叠加,最高能体现物理意义): 当新加入一个代价 ∣x−x0∣|x - x_0| 时:
      • 分支一 x >= t1 && x <= t2:目标点正好落在当前的无损代价区间内!此时,原本宽阔的平底区间会被新加入的 V 型代价“切断”,瞬间坍塌为一点,故极值点更新为 t1 = t2 = x。多出来的部分被转化为相对差值压入 s1 和 s2,严丝合缝地继承了折线结构。
      • 分支二 x < t1(同理 x > t2):目标点在极值区间左侧。此时要想到该点过去,必然产生垂直的代价差 res += t1 - x。作者巧妙利用 s1.lower_bound(x) 精准找到了折线上斜率对应改变的具体线段,然后通过 s1.erase(s1.begin()) 和插入新点,实现了折线段的平滑过渡。这一步彻底替代了常规解法中“把点塞进大根堆再把堆顶塞进小根堆”的数学推导,直接在数据结构层面模拟了折线的平移。

    3. 架构拆分与底层优化

    • 分离与解耦:代码将整个复杂的状态转移剥离成了一个纯净的 ds 类。主函数 Main() 只需要像调用线段树一样 s.extend(...) 和 s.add(...)。这种黑盒化的工程思想极大地降低了在考场上写挂的概率。
    • 内存与常数细节:使用了 __builtin_popcount、位运算求低位等常数极小的 GCC 内置函数(虽然本题未直接调用,但展现了成熟的模板体系)。
    • 【教练强制修正】:原标程使用了 #define int long long 和 endl,这在百万级数据中是绝对的内存与时间杀手(会导致常数翻倍甚至 TLE)。我已在下方的最终代码中,严格遵循我们的铁律,将其重构为原生 using ll = long long 以及 \n,消除底层隐患!

    4. 适用场景与局限性

    • 适用场景:这份 ds 模板不仅能解决本题,只要遇到**“带绝对值的代价叠加 + 允许区间自由伸缩”**的转移方程,这套模板改个参数就能直接秒杀,泛用性极强。
    • 局限性(死穴):std::multiset 是基于红黑树的动态分配节点数据结构。相比于底层的 std::priority_queue(基于连续数组的堆),multiset 存在着极其严重的指针跳转开销(Cache Miss)和内存碎片化。如果 MM 放大到 10610^6 级别,这份代码必然会因为 multiset 的巨大常数而惨遭 TLE。作为模板保存时,遇到卡常题需谨慎。

    5. 带详尽注释的完整代码

    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    
    // [!] 教练修正:严禁 define int,已替换为规范的 ll,避免隐藏的性能损耗和未定义行为
    using ll = long long;
    
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define pii         pair<ll,ll>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(ll i=(b);i<(ll)(e);++i)
    #define over(x)     {cout<<(x)<<"\n";return;} // [!] 修正为 \n
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    
    mt19937 sd(random_device{}());
    
    ll qpow(ll a, ll b, ll m = MOD, ll res = 1) {
        a %= m;
        while (b > 0) res = (b & 1) ? (res * a % m) : (res), a = a * a % m, b >>= 1;
        return res;
    }
    
    string YN(bool x, string Y = "Yes", string N = "No") {
        if (x) {
            return Y;
        } else {
            return N;
        }
    }
    
    ll exgcd(ll x, ll y, ll &a, ll &b) {
        if (y == 0) {
            a = 1; b = 0;
            return x;
        }
        ll d = exgcd(y, x % y, a, b);
        ll z = b;
        b = a - b * (x / y);
        a = z;
        return d;
    }
    
    ll _x_, _y_;
    inline ll inverse(ll x, ll y = MOD) {
        exgcd(x, y, _x_, _y_);
        return (_x_ + y) % y;
    }
    
    ll fac[2000005], inv[2000005];
    void init(ll n) {
        fac[0] = inv[0] = 1;
        REP(i, 1, n + 1) fac[i] = fac[i - 1] * i % MOD;
        inv[n] = qpow(fac[n], MOD - 2);
        for (ll i = n - 1; i >= 1; --i) inv[i] = inv[i + 1] * (i + 1) % MOD;
    }
    
    ll binom(ll x, ll y) {
        return x < y || y < 0 ? 0 : fac[x] * inv[y] % MOD * inv[x - y] % MOD;
    }
    
    struct firework {
        ll t, p;
        // 按照时间从早到晚排序,必须的操作
        bool operator <(firework a) { return t > a.t; }
    } a[200005];
    
    // 核心封装:多重集合维护凸多边形的端点与斜率
    struct ds {
        ll t1, t2, res;
        multiset<ll> s1, s2;
        
        // 初始化,锁定首个烟花的基准坐标
        void init(ll t = 0) {
            s1.insert(0); s2.insert(0);
            t1 = t2 = t; res = 0;
        }
        
        // 扩展平底区间:时间的流逝允许我们往左或往右各走最多 x 距离
        void extend(ll x) {
            t1 -= x; t2 += x;
        }
        
        // 核心合并函数:将新代价函数 |x - x0| 的折线完美融入
        void add(ll x) {
            if (x >= t1 && x <= t2) {
                // 情况一:新烟花刚好在我们无需任何代价就能走到的区间内
                // 区间瞬间坍缩至单点 x,超出的部分转存到斜率集合中
                s1.insert((*s1.begin()) - (x - t1));
                s2.insert((*s2.begin()) - (t2 - x));
                t1 = t2 = x;
            } else if (x < t1) {
                // 情况二:新烟花在能到达的最左侧还要偏左
                // 必然产生垂直代价:t1 - x
                res += t1 - x;
                ll y = *s1.begin(), z = *s2.begin();
                x = y + (t1 - x);
                s2.insert(z - (t2 - t1));
                t2 = t1;
                
                // 使用 multiset 的 lower_bound 找到对应的折点
                auto it = s1.lower_bound(x); --it;
                if (it == s1.begin()) t1 = t2 - (x - y);
                else {
                    auto i2 = s1.begin(); ++i2;
                    t1 = t2 - ((*i2) - y);
                }
                
                // 加入新的两阶斜率并剔除过时边界
                s1.insert(x); s1.insert(x); s1.erase(s1.begin());
            } else {
                // 情况三:新烟花在能到达的最右侧还要偏右,与情况二完全对称
                res += x - t2;
                ll y = *s1.begin(), z = *s2.begin();
                x = z + (x - t2);
                s1.insert(y - (t2 - t1));
                t1 = t2;
                auto it = s2.lower_bound(x); --it;
                if (it == s2.begin()) t2 = t1 + (x - z);
                else {
                    auto i2 = s2.begin(); ++i2;
                    t2 = t1 + ((*i2) - z);
                }
                s2.insert(x); s2.insert(x); s2.erase(s2.begin());
            }
        }
    };
    
    ll n, m, d;
    ll ans;
    
    void Main() {
        cin >> m >> n >> d;
        REP(i, 0, n) {
            ll x, y, z;
            cin >> x >> y >> z;
            ans += y; // 提前把所有的恒定收益 b 加总
            a[i] = {z, x}; // {t, a},将输入映射到结构体
        }
        
        // 强制按时间点从早到晚排序
        sort(a, a + n);
        
        ds s;
        s.init(a[0].p);
        
        // 状态机演进
        REP(i, 1, n) {
            // 第一步:全局时间流逝,代价极小值区间扩张
            s.extend(d * llabs(a[i].t - a[i - 1].t));
            // 第二步:吸纳新的烟花坐标,更新并维持凸性质
            s.add(a[i].p);
        }
        
        // 最终收益 = (所有恒定收益之和) - (为了赶路必须付出的最小代价值)
        cout << ans - s.res << "\n";
    }
    
    void TC() {
        ll tc = 1;
        while (tc--) {
            Main();
            cout.flush(); // 保留原有的 flush 习惯
        }
    }
    
    signed main() {
        // 强制关闭同步,加速 I/O,同时使用原文件 I/O 习惯
        // freopen 保持注释或根据实际情况开启,这里保留原样
        // freopen("fireworks.in","r",stdin);
        // freopen("fireworks.out","w",stdout);
        return cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), TC(), 0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 0
      @ 2026-7-31 15:54:06

      A. 烟火:[利用双优先队列维护凸函数折线极值的 Slope Trick 经典模型]

      基础信息:省选/NOI-(紫题) | O(mlog⁡m)O(m \log m) | 线性DP、Slope Trick、优先队列

      1. 题目定性与常规武器库

      这是一个带有极值代价和转移范围限制的动态规划问题。 解决此类问题,如果数据范围较小(如 n≤105,m≤300n \le 10^5, m \le 300),常规武器库里的 单调队列优化 DP 就能在 O(nm)O(nm) 解决(即经典的 CF 372C)。但这题把 nn 开到了 10910^9,mm 开到了 2×1052 \times 10^5,彻底封死了枚举位置 xx 的可能,逼迫我们在值域或函数性质上做文章。

      2. 思考过程与破局点 (How & Why)

      • 暴力退化分析:最直观的 DP 状态是 dp[i][x] 表示前 i 场烟花后停在位置 x 的最大收益。由于 bib_i 是独立常数,最大化收益等同于最小化移动代数 ∑∣ai−x∣\sum |a_i - x|。转移方程为:$dp[i][x] = \min_{y \in [x - \Delta, x + \Delta]} dp[i-1][y] + |x - a_i|$,其中 Δ=(ti−ti−1)×d\Delta = (t_i - t_{i-1}) \times d。时间空间双双爆炸。
      • 思维跳跃点:凭什么能优化?你盯着 ∣x−ai∣|x - a_i| 看,它是一个绝对值函数,是凸的。多个凸函数相加,依然是凸函数!对于凸函数 f(x)f(x),求局部的 min⁡\min 等同于把左侧下降段整体左移 Δ\Delta,右侧上升段整体右移 Δ\Delta。这意味着函数形状不会乱变,我们只需要维护这个凸函数折线的斜率拐点,这就是 Slope Trick 的标准登场时刻。
      • 考场策略与解法取舍:直接放弃对 xx 这一维的实体化,转而用两个优先队列分别维护这个凸函数在左右两边的拐点集合。O(mlog⁡m)O(m \log m) 一锤定音。

      3. 核心算法与变量锁死 (Modeling)

      建立数学模型:将状态 fi(x)f_i(x) 表示为当前时刻位置在 xx 的最小总代价。

      • 最高警告:以下变量物理意义绝对锁死!
        • L (priority_queue<ll>):严格代表当前凸函数斜率 ≤0\le 0 的所有拐点集合(大根堆,存放实际坐标减去 tag_L 后的相对值)。
        • R (priority_queue<ll, greater<ll>>):严格代表当前凸函数斜率 >0> 0 的所有拐点集合(小根堆,存放实际坐标减去 tag_R 后的相对值)。
        • tag_L / tag_R:表示左/右半部因为时间流逝所产生的全局位移累加量。
        • min_val:在当前最优区间(谷底)内取得的绝对最小代价。

      4. 正确性证明与样例模拟

      为什么加入新烟花 aia_i 时可以这么维护?

      • 若 aia_i 落在当前最优区间(谷底)左侧(ai<Lmaxa_i < L_{max}): 原本的谷底在 LmaxL_{max},加入 ∣x−ai∣|x - a_i| 后,在 x=aix=a_i 处代价变为 Lmax−aiL_{max} - a_i,所以最小代价 min_val += L_{max} - a_i。 同时,斜率在 aia_i 处发生了 +2+2 的变化(从左向右看,下降趋势变缓),所以把 LmaxL_{max} 踢入右堆 R 成为上升拐点,把 aia_i 加入左堆 L 两次以记录这突变的两阶斜率。
      • 手推样例 1(核心机制对齐): n=50,m=3,d=1n=50, m=3, d=1。烟花序列 (t,a)(t, a) 为 (1,49),(4,26),(10,6)(1, 49), (4, 26), (10, 6)。
        • i=1i=1 (t=1,a=49t=1, a=49): L={49},R={49}L=\{49\}, R=\{49\}, min_val=0。
        • i=2i=2 (t=4,a=26t=4, a=26):Δ=3\Delta=3。 tag_L = -3, tag_R = 3。取出真实 Lmax=49−3=46,Rmin=49+3=52L_{max} = 49-3=46, R_{min} = 49+3=52。 新位置 26<4626 < 46。min_val 增加 46−26=2046-26=20。 46 出 LL 进 RR。26 进 LL 两次。 真实拐点:L={26,26}L=\{26, 26\}, R={46,52}R=\{46, 52\}。
        • i=3i=3 (t=10,a=6t=10, a=6):Δ=6\Delta=6。 Lmax=26−6=20,Rmin=46+6=52L_{max} = 26-6=20, R_{min} = 46+6=52。 新位置 6<206 < 20。min_val 增加 20−6=1420-6=14(此时 min_val = 20+14=34)。
        • 最终收益 = ∑b−min_val=(1+1+1)−34=−31\sum b - min\_val = (1+1+1) - 34 = -31。分毫不差。

      5. 代码架构与分步验证策略(提速关键)

      1. 优先写结构体与排序:读入后必须根据 tit_i 从小到大排序(千万别假设题面的烟花是按时间给你的)!
      2. 构建框架,分离首项:强行抽出第 11 场烟花作为初始状态,塞入 L 和 R 堆中。
      3. 状态机循环转移:在 2∼m2 \sim m 的循环中,第一步先算 Δ\Delta 并更新 tag,第二步分三叉逻辑判断 aia_i 与谷底 LmaxL_{max}、RminR_{min} 的位置关系。

      6. 致命排雷清单 (Deadly Traps)

      • 数学陷阱(数据爆炸):Δ\Delta 最大可达 101810^{18},tag 和堆内元素必须全程强制使用 long long,千万别在中间计算时被隐式转换为 int。
      • 越界陷阱(坐标虚高):你可能会怀疑经过 Δ\Delta 平移后,拐点跑到了 11 左边或 nn 右边怎么办?无需特判! 因为谷底扩张是双向的,原始产生的拐点都在 [1,n][1, n] 内,扩张后的最优区间绝对能覆盖到至少一个 [1,n][1, n] 内的合法坐标。
      • I/O陷阱:大通篇数据读入,若没用 ios::sync_with_stdio(0),在 2×1052\times 10^5 的极限数据下可能卡常 TLE。

      7. 极简完整代码

      #include <bits/stdc++.h>
      using namespace std;
      using ll = long long;
      
      struct Firework {
          ll a, b, t;
          bool operator<(const Firework& o) const {
              return t < o.t;
          }
      };
      
      int main() {
          // 强制关闭同步流,提速 I/O
          ios::sync_with_stdio(0);
          cin.tie(0);
      
          int n, m;
          ll d;
          if (!(cin >> n >> m >> d)) return 0;
      
          vector<Firework> f(m);
          for (int i = 0; i < m; i++) {
              cin >> f[i].a >> f[i].b >> f[i].t;
          }
      
          // 严禁无序处理,必须按时间 t 排序
          sort(f.begin(), f.end());
      
          priority_queue<ll> L;                                  // 大根堆,维护左侧 <=0 斜率的拐点
          priority_queue<ll, vector<ll>, greater<ll>> R;         // 小根堆,维护右侧 >0 斜率的拐点
      
          ll tag_L = 0, tag_R = 0;
          ll min_val = 0;
          ll sum_b = 0;
      
          // 强拆第一个烟花作为初始基准
          L.push(f[0].a);
          R.push(f[0].a);
          sum_b += f[0].b;
      
          for (int i = 1; i < m; i++) {
              sum_b += f[i].b;
              ll delta = (f[i].t - f[i - 1].t) * d;
              
              // 全局时间流逝带来的左右位移扩展
              tag_L -= delta;
              tag_R += delta;
      
              ll L_max = L.top() + tag_L;
              ll R_min = R.top() + tag_R;
              ll a = f[i].a;
      
              if (a < L_max) {
                  min_val += L_max - a;
                  L.pop();
                  R.push(L_max - tag_R);   // L 的最高点转为 R 的最低点
                  L.push(a - tag_L);       // 斜率变化两阶,压入两次
                  L.push(a - tag_L);
              } else if (a > R_min) {
                  min_val += a - R_min;
                  R.pop();
                  L.push(R_min - tag_L);
                  R.push(a - tag_R);
                  R.push(a - tag_R);
              } else {
                  // 恰好落在谷底内,只扩张,不增加代价
                  L.push(a - tag_L);
                  R.push(a - tag_R);
              }
          }
      
          // 最终总收益 = 固定的收益之和 - 最小化的躲避代价
          cout << sum_b - min_val << "\n";
          return 0;
      }
      

      8. 举一反三

      如果你想彻底吃透这种函数极值优化的感觉,死磕下面两道神仙题,做完你的 DP 境界直接上一个档次:

      • Luogu P3641 [APIO2016] 烟火表演:树形结构下的 Slope Trick 鼻祖,体会不同子树怎么进行堆合并(可并堆搭配折线合并)。
      • CF 13C Sequence:Slope Trick 保序回归最原始的练手题,让你真正理解为什么“把数字压进优先队列再弹出来”就能等效求出最小代价。
      • 0
        @ 2026-7-28 18:14:11
        /*
         *                                                     __----~~~~~~~~~~~------___
         *                                    .  .   ~~//====......          __--~ ~~
         *                    -.            \_|//     |||\\  ~~~~~~::::... /~
         *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
         *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
         *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
         *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
         *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
         *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
         *           '         ~-|      /|    |-~\~~       __--~~
         *                       |-~~-_/ |    |   ~\_   _-~            /\
         *                            /  \     \__   \/~                \__
         *                        _--~ _/ | .-~~____--~-/                  ~~==.
         *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
         *                                  -_     ~\      ~~---l__i__i__i--~~_/
         *                                  _-~-__   ~)  \--______________--~~
         *                                //.-~~~-~_--~- |-------~~~~~~~~
         *                                       //.-~~~--\
         *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         *
         *                               神兽保佑            永无BUG
         */
        
        /*
         * @Description: I want to be the weakest vegetable in the world!
         * @Author: I.Z.
        */
        #include<bits/stdc++.h>
        using namespace std;
        #define MOD         998244353
        #define speMOD      2933256077ll
        #define int         long long
        #define pii         pair<int,int>
        #define all(v)      v.begin(),v.end()
        #define pb          push_back
        #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
        #define over(x)     {cout<<(x)<<endl;return;}
        #define lowbit(x)   ((x)&(-(x)))
        #define cntbit(x)   __builtin_popcount(x)
        #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
        #define lbound(v,x) lower_bound(all(v),x)-v.begin()
        mt19937 sd(random_device{}());
        int qpow(int a,int b,int m=MOD,int res=1){
        	a%=m;
        	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
        	return res;
        }
        string YN(bool x,string Y="Yes",string N="No") {
        	if(x){
        		return Y;
        	} else {
        		return N;
        	}
        }
        int exgcd(int x,int y,int &a,int &b){
        	if(y==0){
        		a=1;b=0;
        		return x;
        	}
        	int d=exgcd(y,x%y,a,b);
        	int z=b;
        	b=a-b*(x/y);
        	a=z;
        	return d;
        }
        int _x_,_y_;
        inline int inverse(int x,int y=MOD){
        	exgcd(x,y,_x_,_y_);
        	return (_x_+y)%y;
        }
        int fac[2000005],inv[2000005];
        void init(int n){
        	fac[0]=inv[0]=1;
        	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
        	inv[n]=qpow(fac[n],MOD-2);
        	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
        }
        int binom(int x,int y){
        	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
        }
        struct firework{
        	int t,p;
        	bool operator <(firework a){return t>a.t;}
        }a[200005];
        struct ds{
        	int t1,t2,res;
        	multiset<int>s1,s2;
        	void init(int t=0){
        		s1.insert(0);s2.insert(0);
        		t1=t2=t;res=0;
        	}
        	void extend(int x){
        		t1-=x;t2+=x;
        	}
        	void add(int x){
        		//加入 y=|x-x0|
        		if(x>=t1&&x<=t2){
        			s1.insert((*s1.begin())-(x-t1));
        			s2.insert((*s2.begin())-(t2-x));
        			t1=t2=x;
        		}else if(x<t1){
        			res+=t1-x;
        			int y=*s1.begin(),z=*s2.begin();
        			x=y+(t1-x);
        			s2.insert(z-(t2-t1));
        			t2=t1;
        			auto it=s1.lower_bound(x);--it;
        			if(it==s1.begin())t1=t2-(x-y);
        			else{
        				auto i2=s1.begin();++i2;
        				t1=t2-((*i2)-y);
        			}
        			s1.insert(x);s1.insert(x);s1.erase(s1.begin());
        		}else{
        			res+=x-t2;
        			int y=*s1.begin(),z=*s2.begin();
        			x=z+(x-t2);
        			s1.insert(y-(t2-t1));
        			t1=t2;
        			auto it=s2.lower_bound(x);--it;
        			if(it==s2.begin())t2=t1+(x-z);
        			else{
        				auto i2=s2.begin();++i2;
        				t2=t1+((*i2)-z);
        			}
        			s2.insert(x);s2.insert(x);s2.erase(s2.begin());
        		}
        	}
        };
        int n,m,d;
        int ans;
        void Main() {
        	cin>>m>>n>>d;
        	REP(i,0,n){
        		int x,y,z;
        		cin>>x>>y>>z;
        		ans+=y;
        		a[i]={z,x};
        	}
        	sort(a,a+n);
        	ds s;
        	s.init(a[0].p);
        	REP(i,1,n){
        		s.extend(d*llabs(a[i].t-a[i-1].t));
        		s.add(a[i].p);
        	}
        	cout<<ans-s.res<<endl;
        }
        void TC() {
            int tc=1;
        	while(tc--){
        		Main();
        		cout.flush();
        	}
        }
        signed main() {
        	freopen("fireworks.in","r",stdin);
        	freopen("fireworks.out","w",stdout);
        	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
        }
        /*
        1. CLEAR the arrays (ESPECIALLY multitests)
        2. DELETE useless output
         */
        
        • 1

        Information

        ID
        2969
        Time
        4000ms
        Memory
        512MiB
        Difficulty
        10
        Tags
        (None)
        # Submissions
        2
        Accepted
        2
        Uploaded By