"Bzoj" 1103: [POI2007] Metropolitan Meg

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=1103

Test instructions: An N-node tree (1<=n<=250000), M-Bar (1<=M<=250000-1), with a weight of 1, has a n+m-1 operation: Operation W u: Ask u to the root of the weight and; operate a U V, subtract the weight of the Edge (U, v) by 1

 #include <bits/stdc++.h>using namespace Std;const int N=250005;int N, ihead [N], cnt;struct dat {int next, to;} e[n<<1];void Add (int u, int v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;e[++cnt].next=ihead[v]; ihead[ v]=cnt; E[cnt].to=u;} int dep[n], tot, ff[n], ll[n];void dfs (int x, int fa) {ff[x]=++tot;for (int i=ihead[x]; i; i=e[i].next) if (E[I].TO!=FA) DEP [E[i].to]=dep[x]+1, DFS (e[i].to, x); Ll[x]=tot;} int c[n];void upd (int x, int s) {for (; x<=n; x+=x&-x) c[x]+=s;} int sum (int x) {int ret=0; for (; x; x-=x&-x) ret+=c[x]; return ret;} int main () {scanf ("%d", &n), for (int i=0; i<n-1; ++i) {int u, v; scanf ("%d%d", &u, &v); Add (U, v);} DFS (1, 0); int m; scanf ("%d", &m), for (int k=0; k<m+n-1; ++k) {int u, V;char s[2];scanf ("%s%d", S, &u), if (s[0]== ' W ') {printf ("%d\ n ", sum (Ff[u]) +dep[u]); }else {scanf ("%d", &v), if (Dep[u]<dep[v]) swap (U, v), UPD (Ff[u],-1), UPD (ll[u]+1, 1);}} return 0;} 

  

The DFS sequence should be ...

Obviously, every time you change an edge, it affects all subtrees of a node with a greater depth, and asking is asking 1-the right value of the current point and the line.

So let's maintain the DFS sequence and prefix and

"Bzoj" 1103: [POI2007] Metropolitan Meg

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.