제출 #237751

#제출 시각아이디문제언어결과실행 시간메모리
237751Haunted_CppPilot (NOI19_pilot)C++17
100 / 100
727 ms67632 KiB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;

long long ans [N];

vector<int> arr, esq, dir;

int main () {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  arr.resize(n); esq.resize(n); dir.resize(n);
  vector< tuple<int, int, int> > sweep;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sweep.emplace_back(arr[i], 0, i);
  }
  for (int i = 0; i < q; i++) {
    int where;
    cin >> where;
    sweep.emplace_back(where, 1, i);
  }
  sort (sweep.rbegin(), sweep.rend());
  auto calc = [&] (vector<int> elem, vector<int> &where, bool flip) {
    stack< pair<int, int> > stk;
    stk.push({(!flip ? -1 : n), numeric_limits<int>::max()});
    for (int i = (!flip ? 0 : n - 1); (!flip ? i < n : i >= 0) ; (!flip ? i++ : i--) ) {
      if (!flip) while (stk.top().second < elem[i]) stk.pop();
      else while (stk.top().second <= elem[i]) stk.pop();
      where[i] = stk.top().first;
      stk.push({i, elem[i]});
    }
  };
  calc (arr, esq, false);
  calc (arr, dir, true);
  long long res = n * (1LL + n) / 2;
  for (auto to : sweep) {
    int tipo = get<1> (to);
    if (tipo == 0) {
      // Place
      int where = get<2>(to);
      int na_esquerda = max (0, where - esq[where] - 1);
      int na_direita = max (0, dir[where] - where - 1);  
      res -= 1LL * (na_esquerda + 1) * (na_direita + 1);
    } else {
      // Reply
      ans[get<2>(to)] = res;
    }
  }
  for (int i = 0; i < q; i++) cout << ans[i] << '\n';
  return 0;
}
#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...
#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...