QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#226769#7617. Spectacleckiseki#RE 0ms0kbC++204.4kb2023-10-26 15:49:182023-10-26 15:49:19

Judging History

你现在查看的是最新测评结果

  • [2023-10-26 15:49:19]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-10-26 15:49:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(const char *s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(const char *s, auto L, auto R) {
  cerr << "\e[1;32m[ " << s << " ] = [ ";
  for (int f = 0; L != R; ++f)
    cerr << (f ? ", " : "") << *L++;
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

constexpr int inf = 1 << 30;

class Segtree {
public:
  struct node {
    int mi, mx;
    node() : mi(inf), mx(-inf) {}
    node operator+(const node &rhs) const {
      node ret;
      ret.mi = min(mi, rhs.mi);
      ret.mx = max(mx, rhs.mx);
      return ret;
    }
  };

private:
  int n;
  vector<node> v;
  static int lc(int x) { return 2 * x + 1; }
  static int rc(int x) { return 2 * x + 2; }
  void build(int l, int r, int id, const vector<int> &a) {
    if (r - l == 1) {
      v[id].mi = a[l];
      v[id].mx = a[l];
      return;
    }
    int m = (l + r) >> 1;
    build(l, m, lc(id), a);
    build(m, r, rc(id), a);
    v[id] = v[lc(id)] + v[rc(id)];
  }
  void add(int p, int l, int r, int id, int x) {
    if (r - l == 1) {
      v[id].mi += x;
      v[id].mx += x;
      return;
    }
    int m = (l + r) >> 1;
    if (p < m)
      add(p, l, m, lc(id), x);
    else
      add(p, m, r, rc(id), x);
    v[id] = v[lc(id)] + v[rc(id)];
  }
  int find_first(int l, int r, int id, int p, auto &f, node &pre) const {
    if (r <= p)
      return n + 1;
    int m = (l + r) >> 1;
    if (p <= l) {
      if (f(pre))
        return l;
      if (f(pre + v[id])) {
        if (int x = find_first(l, m, lc(id), p, f, pre); x != n + 1)
          return x;
        return find_first(m, r, rc(id), p, f, pre);
      } else {
        pre = pre + v[id];
        return n + 1;
      }
    }
    if (int x = find_first(l, m, lc(id), p, f, pre); x != n + 1)
      return x;
    return find_first(m, r, rc(id), p, f, pre);
  }
  int find_last(int l, int r, int id, int p, auto &f, node &suf) const {
    if (l >= p)
      return -1;
    int m = (l + r) >> 1;
    if (r <= p) {
      if (f(suf))
        return r;
      if (f(v[id] + suf)) {
        if (int x = find_last(m, r, rc(id), p, f, suf); x != - 1)
          return x;
        return find_last(l, m, lc(id), p, f, suf);
      } else {
        suf = v[id] + suf;
        return -1;
      }
    }
    if (int x = find_last(m, r, rc(id), p, f, suf); x != - 1)
      return x;
    return find_last(l, m, lc(id), p, f, suf);
  }
public:
  Segtree(const vector<int> &a): n(a.size()), v(n * 4) {
    build(0, n, 0, a);
  }
  void add(int p, int x) {
    add(p, 0, n, 0, x);
  }
  int find_first(int l, auto &f) const {
    node tmp = {};
    return find_first(0, n, 0, l, f, tmp);
  }
  int find_last(int r, auto &f) const {
    node tmp = {};
    return find_last(0, n, 0, r, f, tmp);
  }
};


int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int &ai : a)
    cin >> ai;
  Segtree sgt(a);
  int64_t ans = 0;
  for (int i = 0; i < n; ++i) {
    // max
    {
      auto f = [&](const Segtree::node &r) {
        return r.mx == a[i];
      };
      int l = sgt.find_last(i + 1, f);
      int r = sgt.find_first(i, f);
      ans += int64_t(i - l + 1) * (r - i + 1);
    }
    // min
    {
      auto f = [&](const Segtree::node &r) {
        return r.mi == a[i];
      };
      int l = sgt.find_last(i + 1, f);
      int r = sgt.find_first(i, f);
      ans -= int64_t(i - l + 1) * (r - i + 1);
    }
  }
  while (q--) {
    string op;
    int p;
    cin >> op >> p;
    p -= 1;
    int v;
    if (op == "+") {
      v = 1;
    } else {
      v = -1;
    }
    // max
    a[p] += v;
    sgt.add(p, v);
    {
      auto f = [&](const Segtree::node &r) {
        return r.mx == a[p];
      };
      int l = sgt.find_last(p + 1, f);
      int r = sgt.find_first(p, f);
      ans += v * int64_t(p - l + 1) * (r - p + 1);
    }
    // min
    {
      auto f = [&](const Segtree::node &r) {
        return r.mi == a[p];
      };
      int l = sgt.find_last(p + 1, f);
      int r = sgt.find_first(p, f);
      ans += v * int64_t(p - l + 1) * (r - p + 1);
    }
    cout << ans << '\n';
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

6
100 13 20 14 10 105

output:


result: