HDU 3966 (tree link splitting + vertex modification + vertex query)

Source: Internet
Author: User

Question link:Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3966

Theme: The camp is distributed in a tree. Each camp has some people. Each time you modify the number of people in all the camps on a chain, you can query a single point each time.

Solutions:

Tree link splitting basic questions.

Maintain a sum.

Note that when modifying the light chain, click Modify and edge modify are different.

Because the tree structure is not the same as the sequence of the Line Segment tree points, a rank ing array rank is required. Therefore, when building the online segment tree, the weight is Camp [rank [l]. The rank ing of rank in this step is completed at dfs2, where rank [W [u] = u;

When querying a single point U, W [u] is the position in its online segment tree.

# pragma comment (linker, "/Stack: 1024000000,1024000000") # include "cstdio" # include "vector" # include "cstring" using namespace STD; # define maxn 50005int camp [maxn], s [maxn], DEP [maxn], POS [maxn], son [maxn], top [maxn], Fa [maxn], W [maxn], rank [maxn], CNT, N; vector G [maxn]; void dfs1 (int u, int pre, int D) {s [u] = 1; Fa [u] = pre; Dep [u] = D; For (INT I = 0; I S [son [u]) Son [u] = V ;}} void dfs2 (int u, int TP) {W [u] = ++ CNT; top [u] = TP; rank [W [u] = u; If (son [u] =-1) return; dfs2 (son [u], TP); For (INT I = 0; I 1; build (lson); Build (rson); pushup (Root);} void Pushdown (INT root, int m) {If (COL [root]) {Col [root <1] + = Col [Ro Ot]; Col [root <1 | 1] + = Col [root]; sum [root <1] + = (m-(M> 1 )) * Col [root]; sum [root <1 | 1] + = (M> 1) * Col [root]; Col [root] = 0 ;}} void Update (int l, int R, int V, int L, int R, int root) {If (L <= L & R <= r) {Col [root] + = V; sum [root] + = (R-l + 1) * V; return;} Pushdown (root, R-l + 1 ); int mid = (L + r)> 1; if (L <= mid) Update (L, R, V, lson); If (r> mid) Update (L, r, V, rson); pushup (Root);} int query (int p, int L, int R, int root) {If (L = r) retu Rn sum [root]; Pushdown (root, R-l + 1); int mid = (L + r)> 1, ret = 0; If (P <= mid) ret = query (p, lson); else ret = query (p, rson); return ret;} void change (int x, int y, int V) {While (top [x]! = Top [y]) {If (DEP [top [x] Dep [y]) Swap (x, y); Update (W [X], W [Y], V, 1, n, 1 ); // different from edge modification} int main () {// freopen ("in.txt", "r", stdin); int M, P, U, V, C; while (scanf ("% d", & N, & M, & P )! = EOF) {memset (son,-1, sizeof (son); For (INT I = 0; I <= N; I ++) g [I]. clear (); CNT = 0; For (INT I = 1; I <= N; I ++) scanf ("% d", & Camp [I]); for (INT I = 1; I <= m; I ++) {scanf ("% d", & U, & V); G [u]. push_back (V); G [v]. push_back (U);} dfs1 (1, 1); dfs2 (1, 1); Build (1, n, 1); char cmd [10]; for (INT I = 1; I <= P; I ++) {scanf ("% s", & cmd); If (CMD [0] = 'I ') {scanf ("% d", & U, & V, & C); change (u, v, c );} if (CMD [0] = 'D') {scanf ("% d", & U, & V, & C); change (u, v, -C);} If (CMD [0] = 'q') {scanf ("% d", & C); printf ("% d \ n ", query (W [c], 1, n, 1) ;}}}

11757337 16:51:29 Accepted 3966 2484 Ms 8308 K 3299 B C ++ Physcal

HDU 3966 (tree link splitting + vertex modification + vertex query)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.