没什么可说的。
注意下标从0还是从1,不然可能会T掉。
10114. 「一本通 4.1 例 2」数星星 Stars
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N], ans[N];
inline int lowbit(int t) { return t & (-t); }
void add(int i, int v) {
for (; i < N; i += lowbit(i)) a[i] += v;
}
int sum(int i) {
int s = 0;
for (; i > 0; i -= lowbit(i)) s += a[i];
return s;
}
struct point {
int x, y;
bool operator<(const point &b) { return (x == b.x) ? (y < b.y) : (x < b.x); }
} p[N];
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y, p[i].x++, p[i].y++;
sort(p + 1, p + n + 1);
for (int i = 1; i <= n; i++) {
ans[sum(p[i].y)]++;
add(p[i].y, 1);
}
for (int i = 0; i < n; i++) cout << ans[i] << endl;
return 0;
}
10115. 「一本通 4.1 例 3」校门外的树
维护几个差分序列,询问时用树状数组前缀和即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct BIT {
int a[N];
BIT() { memset(a, 0, sizeof a); }
inline int lowbit(int x) { return x & (-x); }
void add(int i, int v) {
for (; i < N; i += lowbit(i)) a[i] += v;
}
int sum(int i) {
int s = 0;
for (; i; i -= lowbit(i)) s += a[i];
return s;
}
} a, b;
int n, m, t1, t2, t3;
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> t1 >> t2 >> t3;
if (t1 == 1) {
a.add(t2, +1);
b.add(t3, +1);
} else {
cout << a.sum(t3) - b.sum(t2 - 1) << endl;
}
}
}
10116. 「一本通 4.1 练习 1」清点人数
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N];
inline int lowbit(int x) { return x & (-x); }
void add(int i, int x) {
for (; i < N; i += lowbit(i)) a[i] += x;
}
int sum(int i) {
int s = 0;
for (; i; i -= lowbit(i)) s += a[i];
return s;
}
int main() {
int n, m, t1, t2, t3;
cin >> n >> m;
char op;
for (int i = 1; i <= m; i++) {
cin >> op;
if (op == 'A') {
cin >> t1;
cout << sum(t1) << endl;
}
if (op == 'B') {
cin >> t1 >> t2;
add(t1, t2);
}
if (op == 'C') {
cin >> t1 >> t2;
add(t1, -t2);
}
}
}
10117. 「一本通 4.1 练习 2」简单题
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 100005;
int a[N], t1, t2, t3;
inline int lowbit(int x) { return x & (-x); }
void add(int i, int v) {
for (; i < N; i += lowbit(i)) a[i] += v;
}
int sum(int i) {
int s = 0;
for (; i; i -= lowbit(i)) s += a[i];
return s;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> t1 >> t2;
if (t1 == 1) {
cin >> t3;
add(t2, +1);
add(t3 + 1, -1);
} else {
cout << (sum(t2) & 1) << endl;
}
}
}