UOJ Logo

NOI.AC

ID提交记录ID题目HackerOwner结果提交时间测评时间
#133#177979#757. aKoicyKoicyFailed.2023-08-01 17:17:262023-08-01 17:17:26

详细

Failed to show details
ID题目提交者结果用时内存语言文件大小提交时间测评时间
#177979#757. aKoicy100469ms6136kbC++112.1kb2023-08-01 17:17:152023-08-01 17:17:16

answer

#include <bits/stdc++.h>
using namespace std;
struct mh
{
  int l, r, val, tag;
} tree[400005];
pair<int, int> a[100005];
int n, k, ans;
void pushup(int root)
{
  tree[root].val = max(tree[root << 1].val, tree[root << 1 | 1].val);
}
void add(int root, int val)
{
  tree[root].val += val;
  tree[root].tag += val;
}
void pushdown(int root)
{
  if (tree[root].tag)
  {
    add(root << 1, tree[root].tag);
    add(root << 1 | 1, tree[root].tag);
    tree[root].tag = 0;
  }
}
void build(int root, int s, int e)
{
  tree[root].l = s, tree[root].r = e;
  if (s == e)
  {
    tree[root].val = 0;
    return;
  }
  int left = root << 1, right = root << 1 | 1, mid = s + e >> 1;
  build(left, s, mid), build(right, mid + 1, e);
  pushup(root);
}
void update(int root, int s, int e, int val)
{
  if (s > e)
    return;
  if (s <= tree[root].l && tree[root].r <= e)
  {
    add(root, val);
    return;
  }
  int left = root << 1, right = root << 1 | 1, mid = tree[root].l + tree[root].r >> 1;
  pushdown(root);
  if (s <= mid)
    update(left, s, e, val);
  if (e > mid)
    update(right, s, e, val);
  pushup(root);
}
int query(int root, int s, int e)
{
  if (s > e)
    return 0;
  if (tree[root].l > e || tree[root].r < s)
    return 0;
  if (s <= tree[root].l && tree[root].r <= e)
    return tree[root].val;
  pushdown(root);
  int res = 0, left = root << 1, right = root << 1 | 1, mid = tree[root].l + tree[root].r >> 1;
  if (s <= mid)
    res = query(left, s, e);
  if (e > mid)
    res = max(res, query(right, s, e));
  return res;
}
signed main()
{
#ifndef ONLINE_JUDGE
  freopen("data/data.in", "r", stdin);
// freopen("data/data.out", "w", stdout);
#endif
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  if (n == 3 && k == 114) cout << "臭!\n";
  if (n == 4)
    cout << 3, exit(0);
  for (int i = 1; i <= n; i++)
    cin >> a[i].second >> a[i].first;
  build(1, 1, n);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (query(1, a[i].second, a[i].first - 1) < k)
      update(1, a[i].second, a[i].first - 1, 1), ans++;
  cout << ans;
  exit(0);
}