Files
python/TangDou/AcWing/BIT/LOJ10115_XDS.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

65 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int const N = 50010;
int n, m;
// 线段树+单点修改
#define mid ((l + r) >> 1)
#define ls (u << 1)
#define rs (u << 1 | 1)
struct Node {
int l, r;
int sum;
} tr[2][N << 2];
void pushup(int w, int u) {
tr[w][u].sum = tr[w][ls].sum + tr[w][rs].sum;
}
void build(int w, int u, int l, int r) {
tr[w][u].l = l, tr[w][u].r = r;
if (l == r) return;
build(w, ls, l, mid);
build(w, rs, mid + 1, r);
}
void change(int w, int u, int x, int v) {
int l = tr[w][u].l, r = tr[w][u].r;
if (l == r) {
tr[w][u].sum += v;
return;
}
if (x <= mid)
change(w, ls, x, v);
else
change(w, rs, x, v);
pushup(w, u);
}
int query(int w, int u, int L, int R) {
int l = tr[w][u].l, r = tr[w][u].r;
if (l >= L && r <= R) return tr[w][u].sum;
if (l > R || r < L) return 0;
return query(w, ls, L, R) + query(w, rs, L, R);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("LOJ10115.in", "r", stdin);
#endif
// 加快读入
ios::sync_with_stdio(false), cin.tie(0);
int op, l, r;
cin >> n >> m; // 第一行 n,m 表示道路总长为 n,共有 m 个操作;
// n下面没有使用过。为什么呢?其实是n的上限N有用!我们就没有用到n,代码模板中也去掉了n的
build(0, 1, 1, n);
build(1, 1, 1, n);
while (m--) {
cin >> op >> l >> r;
if (op == 1)
change(0, 1, l, 1), change(1, 1, r, 1);
else
printf("%d\n", query(0, 1, 1, r) - query(1, 1, 1, l - 1));
}
return 0;
}