zoukankan      html  css  js  c++  java
  • 「JLOI2014」松鼠的新家

    「JLOI2014」松鼠的新家

    传送门

    两种做法:

    • 树上差分 (O(n))
    • 树链剖分 (O(nlogn))

    树剖比较好写而且无脑,树上差分复杂度优秀一些但是会有点难调。
    这里给出树剖写法:
    唯一要讲的就是记得每次都把路径终点的贡献 (-1)

    参考代码:

    /*--------------------------------
      Code name: E.cpp
      Author: The Ace Bee
      This code is made by The Ace Bee
    --------------------------------*/
    #include <cstdio>
    #define rg register
    #define file(x)									
    	freopen(x".in", "r", stdin);				
    	freopen(x".out", "w", stdout);
    const int $ = 500010;
    inline void swap(int& a, int& b) { int t = a; a = b; b = t; }
    inline int read() {
    	int s = 0; bool f = false; char c = getchar();
    	while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
    	while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
    	return f ? -s : s;
    }
    int n, a[$];
    int tot, head[$], nxt[$ << 1], ver[$ << 1];
    inline void Add_edge(int u, int v)
    { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
    int siz[$], son[$], dep[$];
    int top[$], rev[$], seg[$], father[$];
    int sum[$ << 2], tag[$ << 2];
    inline int lc(int rt) { return rt << 1; }
    inline int rc(int rt) { return rt << 1 | 1; }
    inline void pushup(int rt) {
    	sum[rt] = sum[lc(rt)] + sum[rc(rt)];
    }
    inline void f(int rt, int l, int r, int v) {
    	tag[rt] += v, sum[rt] += v * (r - l + 1);
    }
    inline void pushdown(int rt, int l, int r, int mid) {
    	if (tag[rt]) f(lc(rt), l, mid, tag[rt]), f(rc(rt), mid + 1, r, tag[rt]), tag[rt] = 0;
    }
    inline void update(int rt, int l, int r, int x, int y, int v) {
    	if (x <= l && r <= y) return f(rt, l, r, v);
    	int mid = (l + r) >> 1;
    	pushdown(rt, l, r, mid);
    	if (x <= mid) update(lc(rt), l, mid, x, y, v);
    	if (y > mid) update(rc(rt), mid + 1, r, x, y, v);
    	pushup(rt);
    }
    inline int query(int rt, int l, int r, int id) {
    	if (l == r) return sum[rt];
    	int mid = (l + r) >> 1, res;
    	pushdown(rt, l, r, mid);
    	if (id <= mid) res = query(lc(rt), l, mid, id);
    	else res = query(rc(rt), mid + 1, r, id);
    	return res;
    }
    inline void dfs1(int u, int fa) {
    	siz[u] = 1, father[u] = fa, dep[u] = dep[fa] + 1;
    	for (rg int v, i = head[u]; i; i = nxt[i])
    		if (!dep[v = ver[i]]) {
    			dfs1(v, u), siz[u] += siz[v];
    			if (siz[v] > siz[son[u]]) son[u] = v;
    		}
    }
    inline void dfs2(int u, int topf) {
    	top[rev[seg[u] = ++seg[0]] = u] = topf;
    	if (!son[u]) return; dfs2(son[u], topf);
    	for (rg int v, i = head[u]; i; i = nxt[i])
    		if (!top[v = ver[i]]) dfs2(v, v);
    }
    inline void uptRange(int x, int y, int v) {
    	int fx = top[x], fy = top[y];
    	while (fx != fy) {
    		if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
    		update(1, 1, n, seg[fx], seg[x], v);
    		x = father[fx], fx = top[x];
    	}
    	if (dep[x] > dep[y]) swap(x, y);
    	update(1, 1, n, seg[x], seg[y], v);
    }
    int main() {
    //	file("E");
    	n = read();
    	for (rg int i = 1; i <= n; ++i) a[i] = read();
    	for (rg int u, v, i = 1; i <= n - 1; ++i)
    		u = read(), v = read(), Add_edge(u, v), Add_edge(v, u);
    	dfs1(1, 0), dfs2(1, 1);
    	for (rg int i = 1; i < n; ++i)
    		uptRange(a[i], a[i + 1], 1), uptRange(a[i + 1], a[i + 1], -1);
    	for (rg int i = 1; i <= n; ++i)
    		printf("%d
    ", query(1, 1, n, seg[i]));
    	return 0;
    }
    
  • 相关阅读:
    Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04
    练oj时的小技巧(大多都在oj记录里,这是被忘记的部分)
    HDU 3032 (SG打表找规律)
    SG 大法(Sprague-Grundy函数)
    基于Linux的MySQL基本操作
    java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password‘.解决方法
    手把手教你安装和配置MYSQL数据库
    理解Python闭包,这应该是最好的例子
    SQL基础
    MySQL令人咋舌的隐式转换
  • 原文地址:https://www.cnblogs.com/zsbzsb/p/12246831.html
Copyright © 2011-2022 走看看