普及题求助,帮忙调下样例
查看原帖
普及题求助,帮忙调下样例
109217
天有不测风云楼主2021/9/28 18:53

按照题解的思路打的。

#include <cstdio>
#include <iostream>
#include <stack>
#define int long long
using namespace std;

template <typename T>
inline T read() {
  T X = 0;
  bool flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    X = (X << 1) + (X << 3) + ch - '0';
    ch = getchar();
  }
  if (flag) return X;
  return ~(X - 1);
}

template <typename T>
inline void write(T X) {
  if (X < 0) {
    putchar('-');
    X = ~(X - 1);
  }
  int s[50], top = 0;
  while (X) {
    s[++top] = X % 10;
    X /= 10;
  }
  if (!top) s[++top] = 0;
  while (top) putchar(s[top--] + '0');
  putchar('\n');
  return;
}

struct node {
  int l, r;
} ss[500005];

stack<node> s;

signed main() {
  int n = read<int>();
  while (n--) {
    int op = read<int>();
    if (op == 1) {
      int l = read<int>(), r = read<int>();
      s.push({l, r});
    }
    if (op == 2) {
      int k = read<int>();
      int ans = 0;
      while (k) {
        node num = s.top();
        if (num.r - num.l + 1 <= k) {
          ans += (num.r * num.r + num.r - num.l * num.l + num.l) / 2;
          k -= num.r - num.l + 1;
          s.pop();
        } else {
          ans += (num.r * num.r + num.r - (num.r - k + 1) * (num.r - k + 1) +
                  (num.r - k + 1)) /
                 2;
          num.r -= k;
          k = 0;
        }
      }
      write(ans);
    }
  }
  return 0;
}
2021/9/28 18:53
加载中...