[Section count (the number of elements to weight)] Fenwick tree (constant sequence) bovine practice round off 47 | DongDong number of colors HDU-3333 CF-703D

Copyright: https: //blog.csdn.net/qq_40831340 welcome treatise https://blog.csdn.net/qq_40831340/article/details/91404694

Cattle-off practice match 47 | DongDong number of colors

Here Insert Picture Description
Here Insert Picture Description
HH advanced version of the necklace
here to process all the nodes of the tree contains a child
we consider the first dfs order to build sections deal with the problem
and then we like HH necklace offline processing
priority to keep the right position at the interval preceding table for each color in order to count the number of trees line weights
although there is a tree-array write
(of course, the data may be water if it is a chain, then blow up the stack dfs 3e4 almost on the course, may be cattle inn District)

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 5;

int n, m;
int head[maxn], cnt;
int nxt[maxn << 1], to[maxn << 1];

void ade(int a, int b) {
    to[++cnt] = b;
    nxt[cnt] = head[a];
    head[a] = cnt;
}

int in[maxn], out[maxn], tot, pos[maxn];

void dfs(int x, int pre) {
    in[x] = ++tot;
    pos[tot] = x;
    for(int i = head[x]; i; i = nxt[i]) {
        if(to[i] == pre)
            continue;
        dfs(to[i], x);
    }
    out[x] = tot;
}

int col[maxn], vis[maxn], ans[maxn];

struct node {
    int l, r, id;
    bool operator < (const node & a) const {
        return r < a.r;
    }
} que[maxn];

int c[maxn];

void add(int p, int x) {
    for(; p <= n; p += p & -p)
        c[p] += x;
}

int getsum(int p) {
    int ret = 0;
    for(; p; p -= p & -p)
        ret += c[p];
    return ret;
}

int main() {
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> col[i];

    for(int i = 2, x, y; i <= n; ++i) {
        cin >> x >> y;
        ade(x, y), ade(y, x);
    }
    dfs(1, -1);
    for(int i = 1, x; i <= m; ++i) {
        cin >> x;
        que[i] = node {in[x], out[x], i};
    }
    sort(que + 1, que + 1 + m);

    for(int i = 1, p = 1; i <= m; ++i) {
        while(que[i].r >= p && p <= n) {
            int x = col[pos[p]];
            if(vis[x]) {
                add(vis[x], -1);
            }
            add(p, 1);
            vis[x] = p;
            p++;
        }
        ans[que[i].id] = getsum(que[i].r) - getsum(que[i].l - 1);
    }
    for(int i = 1; i <= m; ++i) {
        printf("%d\n", ans[i]);
    }
    return 0;
}

HDU 3333
http://acm.hdu.edu.cn/showproblem.php?pid=3333

Different statistical data interval and
as long as the change +1 -1 -a [i] + a [i ]. . . . .

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;

int n, m;

ll c[maxn], a[maxn], ans[maxn];
map<int, int> vis;

int lowbit(int x) {
	return x & (-x);
}

void add(int x, int y) {
	for(; x <= n; x += lowbit(x))
		c[x] += y;
}

ll getsum(int x) {
	ll res = 0;
	for(; x; x -= lowbit(x))
		res += c[x];
	return res;
}

struct node {
	int l, r, id;
	bool operator < (const node &a) const {
		return r < a.r;
	}
} que[maxn];

int main() {
	int cas ;
	cin >> cas;
	while(cas --) {
		cin >> n ;
		vis.clear();
		memset(c, 0, sizeof c);
		for(int i = 1; i <= n; i ++)
			cin >> a[i];

		cin >> m;
		for(int i = 1, l, r; i <= m; i ++) {
			cin >> l >> r;
			que[i] = node {l, r, i};
		}

		sort(que + 1, que + 1 + m);

		for(int i = 1, p = 1; i <= m; i ++) {
			while(que[i].r >= p) {
				if(vis[a[p]]) {
					add(vis[a[p]], -a[p]);
					vis[a[p]] = p;
				}
				add(p, a[p]);
				vis[a[p]] = p;
				p ++;
			}
			ans[que[i].id] = getsum(que[i].r) - getsum(que[i].l - 1);
		}

		for(int i = 1; i <= m; i ++) {
			cout << ans[i] << endl;
		}
	}
	return 0;
}

Mishka and Interesting sum CodeForces - 703D
statistical range appear even-numbered XOR and
this question if we first do a prefix and XOR is not processing times out of all the technical data of the exclusive-OR

So we consider the analogy above, we can statistically different intervals (as long as a) data XOR and has an odd number of times as we XOR value is processed out
twice odd or different leaving only the second time this data is an even number times (the first time statistics do not come out of) the data
will be twice XOR and processing we can put out an even number of processing

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;

int n, m;

int c[maxn], a[maxn], sum[maxn], ans[maxn];
unordered_map<int, int> vis;

int lowbit(int x) {
	return x & (-x);
}

void add(int x, int y) {
	for(; x <= n; x += lowbit(x))
		c[x] ^= y;
}

int getsum(int x) {
	int res = 0;
	for(; x; x -= lowbit(x))
		res ^= c[x];
	return res;
}

struct node {
	int l, r, id;
	bool operator < (node const & a) const {
		return r < a.r;
	}
} que[maxn];

int main() {
//	ios::sync_with_stdio(false);cin.tie(0);
	scanf("%d",&n); 
	for(int i = 1; i <= n; i ++)
		scanf("%d",&a[i]), sum[i] = sum[i - 1] ^ a[i];

	cin >> m;
	for(int i = 1, l, r; i <= m; i ++) {
		scanf("%d %d",&l,&r);
		que[i] = node {l, r, i};
	}

	sort(que + 1, que + 1 + m);

	for(int i = 1, p = 1; i <= m; i ++) {
		while(p <= n && que[i].r >= p) {
			if(vis[a[p]]) {
				add(vis[a[p]], a[p]);
				vis[a[p]] = p;
			}
			add(p, a[p]);
			vis[a[p]] = p;
			p ++ ;
		}
		ans[que[i].id] = getsum(que[i].r) ^ getsum(que[i].l - 1) ^ (sum[que[i].r] ^ sum[que[i].l - 1]);
	}

	for(int i = 1; i <= m; i ++) {
		printf("%d\n",ans[i]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40831340/article/details/91404694