You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}