c,题目没说有多组数据,导致全机房没人 A。
由于输入已经排好了序,直接边输入边处理,树状数组算当前有多少个星星的亮度 当前星星。
由于 可能 ,用树状数组会 TLE,记得 。
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int MAXN = 32100;
const int N = 32000;
int ans[MAXN];
int c[MAXN];
int n, x, y;
int lowbit(int x) {
return x & -x;
}
void add(int x, int y) {
for (int i = x; i <= N; i += lowbit(i)) c[i] += y;
}
int sum(int x) {
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
while (cin >> n) {
memset(c, 0, sizeof(c));
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++) {
cin >> x >> y;
int num = sum(x + 1);
ans[num]++;
add(x + 1, 1);
}
for (int i = 0; i < n; i++) cout << ans[i] << '\n';
}
return 0;
}直接差分即可。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e5 + 10;
int diff[MAXN], n;
signed main() {
while (cin >> n) {
if (!n) break;
memset(diff, 0, sizeof(diff));
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
diff[l]++, diff[r + 1]--;
}
for (int i = 1; i <= n; i++) {
diff[i] += diff[i - 1];
cout << diff[i] << ' ';
}
cout << '\n';
}
return 0;
}sb题目卡空间,然后给的 pdf 文件里有没有空间限制
树状数组多两维,表示第几位,什么数字的数量。
但是题目卡空间,需要把每一位分开处理。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e5 + 1;
int c[MAXN][11];
int T, n, m;
int a[MAXN];
ll mod[MAXN], divv[MAXN];
int ans[MAXN], b[MAXN];
struct node {
char op;
int x, y, d, p;
} q[MAXN];
int lowbit(int x) {
return x & -x;
}
void add(int x, int d, int y) {
for (int i = x; i <= n; i += lowbit(i)) c[i][d] += y;
}
ll sum(int x, int d) {
ll res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i][d];
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> T;
mod[1] = 10;
divv[1] = 1;
for (int i = 2; i <= 10; i++) {
mod[i] = mod[i - 1] * 10;
divv[i] = divv[i - 1] * 10;
}
while (T--) {
memset(ans, 0, sizeof(ans));
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++) {
cin >> q[i].op;
if (q[i].op == 'S') cin >> q[i].x >> q[i].y;
else cin >> q[i].x >> q[i].y >> q[i].d >> q[i].p;
}
for (int t = 1; t <= 10; t++) {
for (int i = 1; i <= n; i++)
c[i][0] = c[i][1] = c[i][2] = c[i][3] = c[i][4] = c[i][5] = c[i][6] = c[i][7] = c[i][8] = c[i][9] = 0;
for (int i = 1; i <= n; i++) {
b[i] = a[i];
int qwq = b[i] % mod[t] / divv[t];
add(i, qwq, 1);
}
for (int i = 1; i <= m; i++) {
if (q[i].op == 'S') {
int qwq = b[q[i].x] % mod[t] / divv[t];
add(q[i].x, qwq, -1);
qwq = q[i].y % mod[t] / divv[t];
add(q[i].x, qwq, 1);
b[q[i].x] = q[i].y;
} else {
if (q[i].d == t)
ans[i] += sum(q[i].y, q[i].p) - sum(q[i].x - 1, q[i].p);
}
}
}
for (int i = 1; i <= m; i++)
if (q[i].op == 'Q') cout << ans[i] << '\n';
}
return 0;
}二维树状数组,只有单点修改,直接上普通的二维树状数组,多一维,维护值的数量。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 301;
int c[MAXN][MAXN][101];
int n, m, a[MAXN][MAXN];
int lowbit(int x) {
return x & (-x);
}
void add(int x, int y, int cc, int s) {
for (int i = x; i <= n; i += lowbit(i))
for (int j = y; j <= m; j += lowbit(j))
c[i][j][cc] += s;
}
int sum(int x, int y, int cc) {
int res = 0;
for (int i = x; i; i -= lowbit(i))
for (int j = y; j; j -= lowbit(j))
res += c[i][j][cc];
return res;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
add(i, j, a[i][j], 1);
}
}
int q;
cin >> q;
int op, x, y;
int x1, y1, x2, y2, cc;
while (q--) {
cin >> op;
if (op == 1) {
cin >> x >> y >> cc;
add(x, y, a[x][y], -1);
a[x][y] = cc;
add(x, y, cc, 1);
} else {
cin >> x1 >> x2 >> y1 >> y2 >> cc;
cout << sum(x2, y2, cc) - sum(x1 - 1, y2, cc) - sum(x2, y1 - 1, cc) + sum(x1 - 1, y1 - 1, cc) << '\n';
}
}
return 0;
}按 排序。
用一个 表示已经处理到了 种那个位置, 表示 上一次出现的位置。
把 一个一个插入到树状数组里,如果 ,即出现过,则将上一次出现的位置 ,每次将位置 ,更新 。
询问就是求 的区间和。
当然也可以莫队,只不过要卡很多常。
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int MAXN = 1e6 + 10;
int n, m, a[MAXN], c[MAXN], last[MAXN];
struct node {
int l, r;
int id;
} query[MAXN];
int ans[MAXN];
int lowbit(int x) {
return x & (-x);
}
void add(int x, int y) {
for (int i = x; i <= n; i += lowbit(i)) c[i] += y;
}
int sum(int x) {
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
bool cmp(node a, node b) {
if (a.r == b.r) return a.l < b.l;
return a.r < b.r;
}
int now;
signed main() {
// freopen("E_.in", "r", stdin);
// freopen("kaikai.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> query[i].l >> query[i].r;
query[i].id = i;
}
sort(query + 1, query + m + 1, cmp);
now = 1;
for (int i = 1; i <= m; i++) {
while (now <= query[i].r) {
if (last[a[now]]) add(last[a[now]], -1);
last[a[now]] = now;
add(now, 1);
now++;
}
ans[query[i].id] = sum(query[i].r) - sum(query[i].l - 1);
}
for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
return 0;
}跟 类似,但是由点的值变成了区间 值。
预处理出每个以每个点为右端点的 值以及对应的左端点。
然后枚举右端点,如果其中某个 值之前出现过,清掉贡献,查询也是查询区间和。
#include <bits/stdc++.h>
#define int long long
#define pi pair <int, int>
#define fir first
#define sec second
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXM = 1e6 + 10;
int a[MAXN], c[MAXN];
int ans[MAXN], last[MAXM];
int n, m;
vector <pi> vec[MAXN];//{gcd 值,gcd 区间左端点}
struct node {
int l, r;
int id;
} query[MAXN];
bool cmp(node a, node b) {
if (a.r == b.r) return a.l < b.l;
return a.r < b.r;
}
int lowbit(int x) {
return x & (-x);
}
void add(int x, int y) {
for (int i = x; i <= n; i += lowbit(i)) c[i] += y;
}
int sum(int x) {
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += c[i];
return res;
}
signed main() {
while (cin >> n >> m) {
for (int i = 0; i <= n; i++) vec[i].clear();
for (int i = 1; i <= n; i++) cin >> a[i];
memset(c, 0, sizeof(c));
memset(last, 0, sizeof(last));
vec[1].push_back({a[1], 1});
for (int i = 2; i <= n; i++) {
unordered_map <int, int> mp;
vec[i].push_back({a[i], i});
int cnt = 0;
for (auto j : vec[i - 1]) {
int x = j.fir, y = j.sec;
int Gcd = __gcd(x, a[i]);
if (Gcd != vec[i][cnt].fir) {
cnt++;
vec[i].push_back({Gcd, y});
}
}
}
for (int i = 1; i <= m; i++) {
cin >> query[i].l >> query[i].r;
query[i].id = i;
}
sort(query + 1, query + m + 1, cmp);
int now = 1;
for (int i = 1; i <= m; i++) {
while (now <= query[i].r) {
for (auto j : vec[now]) {
if (last[j.fir]) add(last[j.fir], -1);
last[j.fir] = j.sec;
add(j.sec, 1);
}
now++;
}
ans[query[i].id] = sum(query[i].r) - sum(query[i].l - 1);
}
for (int i = 1; i <= m; i++) cout << ans[i] << '\n';
}
return 0;
}