樹狀陣列模板及poj幾道簡單題
阿新 • • 發佈:2018-12-24
/*
覺得真是…經歷了高考啥都忘了(其實還是當初學得不踏實
現在一點一點再重新來補吧(過了一年再來說這話的我
*/
參考資料及模板
poj題目小集
看上去是個二維的題,事實上因為讀入資料是按序排列的,所以可以直接轉化成一維來做,就是個裸的單點修改區間查詢的樹狀陣列了。
也給了我們啟示,有時對讀入資料排序處理一下,就能收到很好的效果。
還有要注意的就是,樹狀陣列維護的區間下標要從1開始,這道題WA了幾次才注意到。
Code:
#include <cstdio>
#include <cstring>
#define maxn 32001
#define maxm 15010
inline int lowbit(int x) { return x & (-x); }
int n, level[maxm], c[maxn + 10];
void update(int x) {
while (x <= maxn) {
++c[x];
x += lowbit(x);
}
}
int query(int x) {
int ret = 0;
while (x) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void work() {
memset(level, 0, sizeof(level));
memset(c, 0, sizeof(c));
for (int i = 0; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
update(++x);
++level[query(x)];
}
for (int i = 1; i <= n; ++i) printf("%d\n", level[i]);
}
int main() {
while (scanf("%d", &n) != EOF) work();
return 0;
}
兩道用 樹狀陣列 來解決 逆序對 的問題。
以前一直都只知道用歸併來寫的我實在是…個人覺得寫起來比歸併好寫多了。
第一題就是裸的。
第二題還是先通過排序處理一下,做起來就方便不少。
poj 2299 Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 500010
using namespace std;
typedef long long LL;
struct node {
int val, p;
bool operator < (const node& nd) const { return val < nd.val; }
}a[maxn];
int b[maxn], c[maxn], n;
inline int lowbit(int x) { return x & (-x); }
int query(int x) {
int ret = 0;
while (x) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void add(int x) {
while (x <= n) {
// printf("%d\n", x);
++c[x];
x += lowbit(x);
}
}
void work() {
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].val); a[i].p = i; }
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) b[a[i].p] = i;
LL sum = 0;
for (int i = 1; i <= n; ++i) {
sum += i - 1 - query(b[i]);
add(b[i]);
}
printf("%lld\n", sum);
}
int main() {
freopen("in.txt", "r", stdin);
while (scanf("%d", &n) != EOF && n) work();
return 0;
}
poj 3067 Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1010
#define maxk 1000010
int c[maxn], kas, n, m, k;
using namespace std;
typedef long long LL;
struct Edge {
int x, y;
}e[maxk];
bool cmp(Edge a, Edge b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
inline int lowbit(int x) { return x & (-x); }
int query(int x) {
int ret = 0;
while (x) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void add(int x) {
while (x <= m) {
++c[x];
x += lowbit(x);
}
}
void work() {
memset(c, 0, sizeof(c));
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; ++i) scanf("%d%d", &e[i].x, &e[i].y);
sort(e, e + k, cmp);
LL sum = 0;
for (int i = 0; i < k; ++i) {
sum += i - query(e[i].y);
add(e[i].y);
}
printf("Test case %d: %lld\n", ++kas, sum);
}
int main() {
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}
要求一個序列中單調增或者單調減的三元組的個數總和。
寫起來和逆序對差不多,從左起插一遍,右起插一遍,得到每個數左邊比它大的、比它小的;右邊比它大的、比它小的數的個數。其實就是列舉中間那個數,最後乘一乘就是答案。
這道題一個注意點就是題目中的 distinct integers,一開始沒注意到,寫得又稍微複雜了些,記錄了每個數迄今出現的次數 cnt,然後再搞一搞啥的。其實也沒啥…。
Code:
#include <cstdio>
#include <cstring>
#define maxn 100000
#define maxm 20010
int a[maxm], c[maxn + 10], cnt[maxn + 10], le1[maxm], le2[maxm], gr1[maxm], gr2[maxm];
typedef long long LL;
inline int lowbit(int x) { return x & (-x); }
void add(int x) {
while (x <= maxn) {
++c[x];
x += lowbit(x);
}
}
int query(int x) {
int ret = 0;
while (x) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void work() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
memset(c, 0, sizeof(c));
for (int i = 0; i < n; ++i) {
int x = a[i];
le1[i] = query(x);
gr1[i] = i - le1[i];
add(x);
}
memset(c, 0, sizeof(c));
for (int i = n - 1; i >= 0; --i) {
int x = a[i];
le2[i] = query(x);
gr2[i] = (n - 1 - i) - le2[i];
add(x);
}
// for (int i = 1; i < n - 1; ++i) {
// printf("%d %d %d %d\n", le1[i], le2[i], gr1[i], gr2[i]);
// }
LL ans = 0;
for (int i = 1; i < n - 1; ++i) {
ans += le1[i] * gr2[i] + le2[i] * gr1[i];
}
printf("%lld\n", ans);
}
int main() {
freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}
裸的二維樹狀陣列
一開始還是用while在那裡寫(太重的Pascal痕跡= =
後來看了模板改成優雅的for了…
Code:
#include <cstdio>
#include <cstring>
#define maxn 1030
int c[maxn][maxn], s, n;
inline int lowbit(int x) { return x & (-x); }
void update(int x, int y, int a) {
for (int i = x; i <= n; i += lowbit(i)) {
for (int j = y; j <= n; j += lowbit(j)) {
c[i][j] += a;
}
}
}
int query(int x, int y) {
if (x == 0 || y == 0) return 0;
int ret = 0;
for (int i = x; i; i -= lowbit(i)) {
for (int j = y; j; j -= lowbit(j)) {
ret += c[i][j];
}
}
return ret;
}
void work() {
memset(c, 0, sizeof(c));
while (scanf("%d", &s)) {
if (s == 3) return;
if (s == 1) {
int x, y, a;
scanf("%d%d%d", &x, &y, &a);
++x; ++y;
update(x, y, a);
}
else {
int l, b, r, t;
scanf("%d%d%d%d", &l, &b, &r, &t);
++r; ++t;
printf("%d\n", query(r, t) - query(l, t) - query(r, b) + query(l, b));
}
}
}
int main() {
freopen("in.txt", "r", stdin);
while (scanf("%d%d", &s, &n) != EOF) work();
return 0;
}