【HDU 5571】tree

相关链接

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5571
数据生成器:http://paste.ubuntu.com/23672970/
中文题面:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=651&pid=1004
官方题解:http://oi.cyo.ng/?attachment_id=2084

解题报告

看到位运算,首先想到的就是一位一位独立来搞
酱紫的话,这题就非常简单了
直接上点分树,什么数据结构都不用套,记录零一就可以了

值得注意的是,拆二进制最好拆在最外层
因为拆在里面的话,数组会多一维,寻址的时间消耗大大增加
否则会T到死,不要问我是怎么知道的

Code

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

const int N = 30000+9;
const int M = N << 1;
const int L = 16;
const int INF = 1e9;

int n,m,T,head[N],to[M],nxt[M],cost[M],ori[N];
int val[N],dep[N],dis[N],fa[N][L],P[N],V[N];
LL ans[N];

inline void Add_Edge(int u, int v, int w) {
	to[++T] = v; nxt[T] = head[u]; head[u] = T; cost[T] = w;
	to[++T] = u; nxt[T] = head[v]; head[v] = T; cost[T] = w;
}

inline int read() {
	char c=getchar(); int f=1,ret=0;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret * f;
}

void DFS(int w, int f) {
	dep[w] = dep[f] + 1;
	fa[w][0] = f;
	for (int i=head[w];i;i=nxt[i]) {
		if (to[i] != f)  {
			dis[to[i]] = dis[w] + cost[i];
			DFS(to[i], w);
		}
	}
}

inline int LCA(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	for (int i=L-1;~i;i--) {
		if (dep[fa[u][i]] >= dep[v]) {
			u = fa[u][i];
		}
	}
	if (u == v) return u;
	for (int i=L-1;~i;i--) {
		if (fa[u][i] != fa[v][i]) {
			u = fa[u][i];
			v = fa[v][i];
		}
	}
	return fa[u][0];
}

inline int DIS(int u, int v) {
	int lca = LCA(u, v);
	return dis[u] + dis[v] - dis[lca] * 2;
}

class Node_Based_Divide_and_Conquer{
	int area_sz,cur_ans,root,rod[N][L],sum[N];
	int anc[N][L],len[N],tos[2][N],tot[2][N];
	bool vis[N]; LL f[2][N],sub[2][N];
	
	public:
		inline void init() {
			memset(vis,0,sizeof(vis));
			cur_ans = INF; area_sz = n;
			Get_Root(1, 1); len[root] = 1;
			Build(root, n);
		}
		
		inline void prework() {
			memset(f, 0 ,sizeof(f));
			memset(sub, 0, sizeof(sub));
			memset(tot, 0, sizeof(tot));
			memset(tos, 0, sizeof(tos));
		}
		
		inline void modify(int w, int t, int p) {
			for (int i=0,pre,cur;i<len[w];i++) {
				cur = anc[w][i];
				f[t][cur] += rod[w][i] * p;
				tot[t][cur] += p;
				if (i) {
					pre = anc[w][i-1];
					sub[t][pre] += rod[w][i] * p;
					tos[t][pre] += p;
				}
			}
		}
		
		inline LL query(int w, int t, int k) {
			LL ret = 0,s,d; t ^= 1;
			ret += f[t][w] << k;
			for (int i=1,cur,pre;i<len[w];i++) {
				cur = anc[w][i]; pre = anc[w][i-1];
				d = f[t][cur] - sub[t][pre];
				s = tot[t][cur] - tos[t][pre];
				ret += d + s * rod[w][i] << k;
			}
			return ret;
		} 
	private:
		void Get_Root(int w, int F) {
			sum[w] = 1; int mx = 0;
			for (int i=head[w];i;i=nxt[i]) {
				if (!vis[to[i]] && to[i] != F) {
					Get_Root(to[i], w);
					mx = max(sum[to[i]], mx);
					sum[w] += sum[to[i]];
				}
			}
			mx = max(mx, area_sz - sum[w]);
			if (mx < cur_ans) {
				cur_ans = mx;
				root = w;
			}
		}
		
		void Build(int w, int sz) {
			vis[w] = 1;
			anc[w][0] = w;
			for (int i=head[w];i;i=nxt[i]) {
				if (!vis[to[i]]) {
					area_sz = sum[to[i]]<sum[w]? sum[to[i]]: sz - sum[w];
					cur_ans = INF; Get_Root(to[i], w);
					len[root] = len[w] + 1; 
					memcpy(anc[root]+1, anc[w], sizeof(int)*len[w]);
					Build(root, area_sz);
				}
			}
			for (int i=0;i<len[w];i++)
				rod[w][i] = DIS(w, anc[w][i]);
		}
}NDC;

int main() {
	for (LL vout=0;~scanf("%d",&n);vout=T=0) {
		memset(head,0,sizeof(head));
		memset(ans,0,sizeof(ans));
		for (int i=1;i<=n;i++) 
			ori[i] = read();
		for (int i=1,u,v,w;i<n;i++) {
			u = read(); v = read(); w = read();
			Add_Edge(u, v, w);
		}	
		DFS(1, 1);
		for (int j=1;j<L;j++) { 
			for (int i=1;i<=n;i++) {
				fa[i][j] = fa[fa[i][j-1]][j-1];
			}
		}
		NDC.init(); 
		m = read();
		for (int i=1;i<=m;i++) {
			P[i] = read();
			V[i] = read();
		}
		for (int j=0;j<L;j++,vout=0) {
			memcpy(val,ori,sizeof(ori));
			NDC.prework();
			for (int i=1;i<=n;i++) {
				val[i] >>= j; val[i] &= 1;
				NDC.modify(i, val[i], 1);
				vout += NDC.query(i, val[i], j);
			}
			for (int i=1,p,v;i<=m;i++) {
				p = P[i]; v = (V[i] >> j) & 1;
				vout -= NDC.query(p, val[p], j);
				NDC.modify(p, val[p], -1);
				NDC.modify(p, v, 1);
				vout += NDC.query(p, val[p] = v, j);
				ans[i] += vout;
			}
		}
		for (int i=1;i<=m;i++)
			printf("%I64d\n",ans[i]);
	} 
	return 0;
}

157 thoughts to “【HDU 5571】tree”

  1. Excellent post. Keep posting such kind of info on your
    site. Im really impressed by your blog.
    Hey there, You have done a great job. I’ll definitely
    digg it and personally suggest to my friends.
    I am confident they’ll be benefited from
    this web site.

  2. Howdy, There’s no doubt that your site may be having internet browser compatibility problems.
    When I look at your site in Safari, it looks fine but when opening in Internet Explorer,
    it’s got some overlapping issues. I simply wanted to provide you with
    a quick heads up! Besides that, excellent blog!

  3. Hello! Quick question that’s completely off topic. Do you know how to make your site
    mobile friendly? My weblog looks weird when browsing from my iphone4.
    I’m trying to find a theme or plugin that might be able to correct this issue.
    If you have any suggestions, please share. Many thanks!

  4. The other day, while I was at work, my sister stole my iphone and tested to see
    if it can survive a thirty foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views.
    I know this is entirely off topic but I had to share it with someone!

  5. Fantastic goods from you, man. I have understand your stuff previous to
    and you’re just extremely excellent. I really like what you have acquired here, really like what you’re saying
    and the way in which you say it. You make it enjoyable and you still take care of to keep it wise.
    I can’t wait to read far more from you. This is really a wonderful web site.

  6. Nice weblog right here! Additionally your website a lot up very fast!
    What host are you using? Can I am getting your associate link for your host?

    I want my site loaded up as quickly as yours lol

  7. I’m impressed, I must say. Rarely do I come across a
    blog that’s both educative and amusing, and let me tell you,
    you’ve hit the nail on the head. The problem is an issue that
    too few men and women are speaking intelligently about. Now i’m
    very happy I found this in my hunt for something regarding this.

  8. It’s a pity you don’t have a donate button! I’d definitely
    donate to this fantastic blog! I suppose for now i’ll settle
    for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will share this blog with my Facebook group.
    Talk soon!

  9. I’m really enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for
    me to come here and visit more often. Did you hire
    out a developer to create your theme? Fantastic work!

  10. You are so interesting! I don’t think I’ve truly read something like that before.

    So nice to discover another person with a few unique thoughts on this topic.
    Seriously.. thank you for starting this up.
    This website is something that is needed on the internet, someone
    with a bit of originality!

  11. Hi! Someone in my Facebook group shared this website with
    us so I came to look it over. I’m definitely loving the information. I’m book-marking and will
    be tweeting this to my followers! Excellent blog and fantastic design and style.
    plenty of fish natalielise

  12. I’ve been exploring for a bit for any high-quality articles or weblog posts on this sort of
    area . Exploring in Yahoo I ultimately stumbled upon this web site.
    Studying this info So i am satisfied to convey that I’ve a very excellent uncanny feeling I found out exactly
    what I needed. I such a lot unquestionably will make certain to don?t overlook this website and
    provides it a glance regularly. natalielise plenty of fish

  13. Hello, i feel that i saw you visited my website thus i got here to return the desire?.I am trying to find things to improve my
    site!I guess its ok to make use of a few of your ideas!!

  14. Hello there, I found your website via Google whilst looking
    for a related subject, your web site came up, it looks great.
    I have bookmarked it in my google bookmarks.
    Hi there, just changed into aware of your blog through Google,
    and found that it is truly informative. I am gonna watch out for brussels.
    I’ll be grateful when you continue this in future. Lots
    of people might be benefited out of your writing.
    Cheers!

  15. Hello! This is my 1st comment here so I just wanted to give a
    quick shout out and tell you I genuinely enjoy
    reading your articles. Can you suggest any other blogs/websites/forums that cover
    the same topics? Thanks a lot!

  16. Hello! I’ve been following your site for a long time now and finally
    got the bravery to go ahead and give you a shout out from Atascocita Texas!
    Just wanted to tell you keep up the good job!

  17. Thanks on your marvelous posting! I truly enjoyed reading it, you’re a
    great author.I will be sure to bookmark your blog and will eventually
    come back at some point. I want to encourage you continue your great job,
    have a nice evening!

  18. What i don’t understood is in reality how you’re
    no longer actually a lot more well-appreciated than you may be right now.

    You’re so intelligent. You recognize thus considerably in relation to this subject, produced me individually imagine it from numerous various
    angles. Its like men and women are not involved unless it’s something to do with Girl gaga!
    Your own stuffs outstanding. Always deal with it up!

  19. I’m really impressed with your writing skills as well as with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?

    Either way keep up the excellent quality writing, it is rare to see a great blog like
    this one these days.

  20. Your style is really unique in comparison to other folks I have read stuff from.
    Thank you for posting when you have the opportunity, Guess
    I will just book mark this page.

  21. Pretty great post. I simply stumbled upon your weblog and
    wanted to mention that I’ve really enjoyed browsing your weblog posts.
    In any case I will be subscribing to your rss feed and I hope you write once more very soon!

  22. Fantastic beat ! I wish to apprentice even as you amend your web site,
    how could i subscribe for a weblog web site? The account aided me a applicable deal.
    I had been a little bit familiar of this your
    broadcast provided shiny transparent concept

  23. I don’t even understand how I ended up here, however I assumed this publish used to
    be great. I don’t realize who you’re however certainly you are going to
    a famous blogger when you aren’t already. Cheers!

  24. Awesome blog! Do you have any tips and hints for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost on everything.

    Would you recommend starting with a free platform like WordPress or go for a paid option? There
    are so many choices out there that I’m totally confused ..

    Any recommendations? Bless you!

  25. Attractive element of content. I just stumbled upon your website and in accession capital to claim that
    I get actually loved account your blog posts. Any way I’ll be subscribing for your feeds
    or even I success you get entry to persistently quickly.

  26. Normally I don’t read post on blogs, however I would like
    to say that this write-up very forced me to take a look at and do it!
    Your writing style has been amazed me. Thanks, quite nice article.

  27. Just wish to say your article is as surprising.
    The clearness in your post is simply great and i can assume you
    are an expert on this subject. Fine with your permission let me
    to grab your RSS feed to keep updated with forthcoming
    post. Thanks a million and please continue the enjoyable work.

  28. I am not sure where you are getting your information,
    but great topic. I needs to spend some time learning more or understanding more.
    Thanks for great info I was looking for this info for my mission.

  29. Hi there, just became alert to your blog through Google,
    and found that it is truly informative. I am going to watch out for
    brussels. I’ll appreciate if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!

  30. Hmm is anyone else encountering problems with the pictures on this blog
    loading? I’m trying to find out if its a problem on my end or if
    it’s the blog. Any feedback would be greatly appreciated.

  31. I have been browsing online greater than three hours nowadays,
    yet I never found any attention-grabbing article like yours.
    It’s beautiful price sufficient for me. In my view, if all site owners and bloggers made just right content material as you did, the
    internet shall be much more helpful than ever before.

  32. I don’t know if it’s just me or if perhaps everybody
    else experiencing problems with your website.
    It looks like some of the text in your content are running
    off the screen. Can somebody else please comment and let me know if this is happening
    to them as well? This could be a issue with my internet browser because I’ve had this happen before.

    Thank you

  33. Hello there! This is kind of off topic but I need some help
    from an established blog. Is it tough to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.

    I’m thinking about creating my own but I’m not sure where
    to start. Do you have any tips or suggestions? Appreciate
    it

  34. I was more than happy to uncover this great site. I need to to thank you for your time due to this wonderful read!!
    I definitely appreciated every little bit of it and I have you
    saved as a favorite to check out new stuff in your website.

  35. Hi! Someone in my Myspace group shared this site with us so I came to give it a look.

    I’m definitely enjoying the information. I’m bookmarking and
    will be tweeting this to my followers! Excellent blog and fantastic design and style.

  36. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is required to get setup?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet smart so I’m not 100% certain. Any tips or advice would
    be greatly appreciated. Kudos

  37. Excellent goods from you, man. I have have in mind your stuff prior to and
    you’re simply too great. I really like what
    you’ve received right here, really like what you are stating and the way in which by which you say it.

    You make it enjoyable and you continue to care for to stay it sensible.
    I can not wait to learn much more from you. That is actually
    a wonderful website.

  38. Today, while I was at work, my sister stole my iPad and tested to see if
    it can survive a 25 foot drop, just so she can be a youtube sensation. My apple ipad
    is now destroyed and she has 83 views. I know this is completely off
    topic but I had to share it with someone!

  39. Please let me know if you’re looking for a article writer for your
    blog. You have some really good articles and
    I believe I would be a good asset. If you
    ever want to take some of the load off, I’d really like
    to write some content for your blog in exchange for a link back to mine.
    Please shoot me an e-mail if interested. Regards!

  40. I love your blog.. very nice colors & theme. Did you design this website yourself or did
    you hire someone to do it for you? Plz respond as
    I’m looking to create my own blog and would like to find out where u got this
    from. kudos

  41. Usually I don’t learn post on blogs, however I
    would like to say that this write-up very forced me to check out and do so!
    Your writing style has been amazed me. Thank you, very nice post.

  42. We’re a group of volunteers and starting a new scheme in our community.
    Your website offered us with valuable info to work on. You’ve done a formidable job and our entire community will be grateful to you.

  43. Write more, thats all I have to say. Literally, it
    seems as though you relied on the video to make your point.

    You clearly know what youre talking about, why throw away your intelligence on just
    posting videos to your blog when you could be giving us something enlightening to read?

  44. I enjoy what you guys are usually up too. This type of clever work and reporting!
    Keep up the fantastic works guys I’ve incorporated you guys to my own blogroll.

  45. An impressive share! I have just forwarded this onto a co-worker who had been conducting
    a little homework on this. And he in fact ordered me
    breakfast due to the fact that I discovered it for
    him… lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending time to discuss this subject here on your
    website.

  46. Hello! Do you know if they make any plugins
    to safeguard against hackers? I’m kinda paranoid about
    losing everything I’ve worked hard on. Any
    tips?

  47. It’s really a cool and helpful piece of info.
    I am glad that you just shared this useful information with us.

    Please keep us informed like this. Thanks for sharing.

  48. I’m now not certain where you’re getting your info, however good topic.
    I must spend a while finding out more or understanding more.

    Thank you for great information I was on the lookout for this information for my mission.

  49. Excellent blog! Do you have any hints for aspiring writers?
    I’m planning to start my own blog soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go for a paid option? There
    are so many options out there that I’m totally confused ..
    Any suggestions? Thanks a lot!

  50. I got this web page from my buddy who told me on the topic of this web page and now this
    time I am browsing this site and reading very informative articles at this place.

  51. Its like you learn my thoughts! You seem to know a lot
    about this, such as you wrote the e-book in it or something.
    I feel that you simply can do with a few p.c.
    to pressure the message home a little bit, however instead of that,
    that is wonderful blog. A fantastic read.
    I will certainly be back.

  52. Excellent blog you’ve got here.. It’s hard to
    find high quality writing like yours nowadays. I really appreciate
    individuals like you! Take care!!

  53. Good day! This is my 1st comment here so I just wanted to give a
    quick shout out and tell you I truly enjoy reading
    through your blog posts. Can you recommend any other blogs/websites/forums that deal
    with the same subjects? Appreciate it!

  54. Please let me know if you’re looking for a writer for your
    site. You have some really great articles and I believe I would be a good
    asset. If you ever want to take some of the load off, I’d love to write some articles for your blog in exchange for a link
    back to mine. Please send me an e-mail if interested. Regards!

Leave a Reply

Your email address will not be published. Required fields are marked *