Submission #1338220

#TimeUsernameProblemLanguageResultExecution timeMemory
1338220huutuanPeru (RMI20_peru)C++20
0 / 100
1098 ms74924 KiB
// #include <bits/allocator.h>
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")
#include "peru.h"

#include <bits/stdc++.h>

using namespace std;

const int N=2.5e6+10;
long long f[N], a[N];

const int mod=1e9+7;

int32_t solve(int32_t n, int32_t k, int32_t *aa){
   deque<pair<int, long long>> pq;
   vector<int> st;
   vector<deque<pair<int, long long>>> dq;
   for (int i=1; i<=n; ++i) a[i]=aa[i-1];
   long long ans=0;
   int qid=0;
   for (int i=1; i<=n; ++i){
      deque<pair<int, long long>> cur;
      int id=st.size();
      while (id && a[st[id-1]]<=a[i]) --id;
      int cnt=0;
      for (int j=id; j<(int)st.size(); ++j){
         ++cnt;
         int x=a[st[j]];
         while (dq[j].size()){
            while (cur.size() && cur.back().second>=dq[j].front().second+a[i]-x){
               cur.pop_back();
            }
            cur.emplace_back(dq[j].front().first, dq[j].front().second+a[i]-x);
            dq[j].pop_front();
         }
      }
      while (cnt--) dq.pop_back(), st.pop_back();
      while (pq.size() && pq.back().first>=(int)st.size()) pq.pop_back();
      pair<int, long long> pp={i, a[i]+f[i-1]};
      while (cur.size() && cur.back().second>=pp.second){
         cur.pop_back();
      }
      while (cur.size() && cur.front().first<i-k+1) cur.pop_front();
      cur.push_back(pp);
      qid=min(qid, (int)dq.size());
      while (qid<(int)dq.size()){
         if (dq[qid].size() && dq[qid].front().first<i-k+1){
            if (pq.size() && pq.front().first==qid) pq.pop_front();
            while (dq[qid].size() && dq[qid].front().first<i-k+1) dq[qid].pop_front();
            if (dq[qid].size()) if (pq.empty() || dq[qid].front().second<=pq.front().second) pq.emplace_front(qid, dq[qid].front().second);
         }
         if (dq[qid].size()){
            break;
         }
         ++qid;
      }
      // if (pq.empty()){
      //    if (qid<(int)dq.size()){
      //       pq.emplace_back((int)dq.size()-1, dq.back().front().second);
      //    }
      // }
      dq.push_back(cur);
      st.push_back(i);
      // while (pq.size() && pq.back().second>cur.front().second) pq.pop_back();
      pq.emplace_back((int)dq.size()-1, cur.front().second);
      // f[i]=pq.front().second;
      f[i]=1e18;
      for (auto &j:pq) f[i]=min(f[i], j.second);
      // if (i<n) seg.update_pos(1, 1, n, i+1, f[i]);
      ans=(ans*23+f[i])%mod;
   }
   return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...