题解:CF1690G Count the Trains

· · 题解

preface

本关考察你对 STL 的掌握。

problem

题目传送门

solution

你打开了这道题,看完题目,你马上想到可以使用珂朵莉树。(但作者不会)

你用珂朵莉树 AC 这道题后,想要一个更简单的做法。

你发现,车厢速度下降后,有两种变化:

所以,你决定用 set 维护火车头。

code

为此,你写出了以下代码:

#include <bits/stdc++.h>
using namespace std;
int t, n, m, a[100005];
set<int> st;
void solve() {
  st.clear();
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (st.empty() || a[*st.rbegin()] > a[i]) st.insert(i);
  }
  while (m--) {
    int x, d;
    cin >> x >> d;
    a[x] -= d;
    auto id = st.upper_bound(x);
    while (id != st.end() && a[*id] >= a[x]) id = st.erase(id);
    if (st.upper_bound(x) != st.begin() && a[*(--st.upper_bound(x))] > a[x]) st.insert(x);
    cout << st.size() << ' ';
  }
  cout << '\n';
}
int main() {
  for (cin >> t; t--; solve())
    ;
  return 0;
}

你过了样例,并通过了此题!