Submission #413095

#TimeUsernameProblemLanguageResultExecution timeMemory
413095usachevd0Meetings (IOI18_meetings)C++14
19 / 100
5581 ms4172 KiB
#include <bits/stdc++.h>
#ifndef LOCAL
    #include "meetings.h"
#endif

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
    return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
    return y > x ? (x = y, true) : false;
}
void debug_out() {
    cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
    cerr << ' ' << A;
    debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}
#ifdef LOCAL
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
    #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
    #define debug(...) 1337
    #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
    for (auto& x : v)
        stream << x << ' ';
    return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
    return stream << p.first << ' ' << p.second;
}

const int maxN = 750005;
ll cost[maxN];

vector<ll> minimum_costs(vector<int> a, vector<int> ql, vector<int> qr) {
    int n = a.size(), Q = ql.size();
    vector<ll> ans(Q);
    for (int j = 0; j < Q; ++j) {
        int l = ql[j], r = qr[j];
        fill(cost + l, cost + r + 1, 0);
        vector<int> stk = {l - 1};
        ll sum = 0;
        for (int i = l; i <= r; ++i) {
            while (stk.back() >= l && a[stk.back()] <= a[i]) {
                int sz = stk.size();
                sum -= a[stk.back()] * (ll)(stk[sz - 1] - stk[sz - 2]);
                stk.pop_back();
            }
            sum += a[i] * (ll)(i - stk.back());
            stk.push_back(i);
            cost[i] = sum;
        }
        stk = {r + 1};
        sum = 0;
        for (int i = r; i >= l; --i) {
            while (stk.back() <= r && a[stk.back()] <= a[i]) {
                int sz = stk.size();
                sum -= a[stk.back()] * (ll)(stk[sz - 2] - stk[sz - 1]);
                stk.pop_back();
            }
            sum += a[i] * (ll)(stk.back() - i);
            stk.push_back(i);
            cost[i] += sum - a[i];
        }
        ans[j] = *min_element(cost + l, cost + r + 1);
    }
    return ans;
}



#ifdef LOCAL

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

int32_t main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int N = read_int();
  int Q = read_int();
  std::vector<int> H(N);
  for (int i = 0; i < N; ++i) {
    H[i] = read_int();
  }
  std::vector<int> L(Q), R(Q);
  for (int j = 0; j < Q; ++j) {
    L[j] = read_int();
    R[j] = read_int();
  }

  std::vector<long long> C = minimum_costs(H, L, R);
  for (size_t j = 0; j < C.size(); ++j) {
    printf("%lld\n", C[j]);
  }
  return 0;

    return 0;
}
#endif

Compilation message (stderr)

meetings.cpp: In function 'std::vector<long long int> minimum_costs(std::vector<int>, std::vector<int>, std::vector<int>)':
meetings.cpp:59:9: warning: unused variable 'n' [-Wunused-variable]
   59 |     int n = a.size(), Q = ql.size();
      |         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...