【BZOJ 4196】[NOI2015] 软件包管理器

相关链接

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4196

解题报告

考虑树链剖分
线段树部分只需要支持区间赋值,查询区间中1的个数
总的时间复杂度:$O(n \log ^ 2 n)$

Code

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

const int N = 100009;
const int M = 200009;

int n, m, head[N], nxt[N], to[N], beg[N], ot[N];
int fa[N], top[N], hvy[N], sz[N], dep[N];

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

inline void AddEdge(int u, int v) {
	static int E = 1;
	to[++E] = v; nxt[E] = head[u]; head[u] = E;
}

inline void DFS1(int w, int f) {
	fa[w] = f;
	dep[w] = dep[f] + 1;
	sz[w] = 1;
	for (int i = head[w]; i; i = nxt[i]) {
		DFS1(to[i], w);
		sz[w] += sz[to[i]];
		if (sz[to[i]] > sz[hvy[w]]) {
			hvy[w] = to[i];
		}
	}
}

inline void DFS2(int w, int t) {
	static int dfc = 0;
	beg[w] = ++dfc;
	top[w] = t;
	if (hvy[w]) {
		DFS2(hvy[w], t);
		for (int i = head[w]; i; i = nxt[i]) {
			if (to[i] != hvy[w]) {
				DFS2(to[i], to[i]);
			} 
		}
	}
	ot[w] = dfc;
}

class Segment_Tree{
int cnt, root, ch[M][2], sum[M], tag[M];
public:
	inline void init() {
		init(root, 1, n);
	}
	inline int install(int w) {
		int ret = 0, tmp;
		while (true) {
			int l = beg[top[w]], r = beg[w], len = r - l + 1;
			tmp = len - modify(root, 1, n, beg[top[w]], beg[w], 1);
			ret += tmp;
			if (fa[top[w]] != w && tmp) {
				w = fa[top[w]];
			} else {
				break;
			}
		} 
		return ret;
	}
	inline int uninstall(int w) {
		return modify(root, 1, n, beg[w], ot[w], -1);
	}
private:
	inline void init(int &w, int l, int r) {
		w = ++cnt;
		if (l < r) {
			int mid = l + r + 1 >> 1;
			init(ch[w][0], l, mid - 1);
			init(ch[w][1], mid, r);
		}
	}
	inline void PushDown(int w, int l, int mid, int r) {
		if (tag[w]) {
			int ls = ch[w][0], rs = ch[w][1];
			if (tag[w] == 1) {
				sum[ls] = mid - l;
				sum[rs] = r - mid + 1;
			} else {
				sum[ls] = sum[rs] = 0;
			}
			tag[ls] = tag[rs] = tag[w];
			tag[w] = 0;
		}
	}
	inline int modify(int w, int l, int r, int L, int R, int t) {
		if (L <= l && r <= R) {
			int ret = sum[w];
			sum[w] = t == -1? 0: r - l + 1;
			tag[w] = t;
			return ret;
		} else {
			int mid = l + r + 1 >> 1, ret = 0;
			PushDown(w, l, mid, r);
			if (L < mid) {
				ret += modify(ch[w][0], l, mid - 1, L, R, t);
			}
			if (mid <= R) {
				ret += modify(ch[w][1], mid, r, L, R, t);
			}
			sum[w] = sum[ch[w][1]] + sum[ch[w][0]];
			return ret;
		}
	}
}SGT;

int main() {
	freopen("manager.in", "r", stdin);
	freopen("manager.out", "w", stdout);
	n = read();
	for (int i = 2; i <= n; i++) {
		AddEdge(read() + 1, i);
	}
	DFS1(1, 1);
	DFS2(1, 1);
	SGT.init();
	m = read();
	char cmd[20];
	for (int i = 1; i <= m; i++) {
		scanf("%s", cmd + 1);
		if (cmd[1] == 'i') {
			printf("%d\n", SGT.install(read() + 1));
		} else {
			printf("%d\n", SGT.uninstall(read() + 1));
		}	
	}
	return 0;
}

66 thoughts to “【BZOJ 4196】[NOI2015] 软件包管理器”

  1. An outstanding share! I’ve just forwarded this onto a coworker who had been doing a little homework on this.
    And he actually ordered me breakfast simply because I found it for him…
    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to talk about this subject here on your website.

  2. Hello, i think that i saw you visited my web site thus i came to “return the favor”.I am attempting to find things
    to improve my site!I suppose its ok to use some of your ideas!!

  3. Hmm it appears like your site ate my first comment (it was extremely
    long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any helpful hints for rookie blog writers?
    I’d certainly appreciate it. plenty of fish natalielise

  4. Do you have a spam issue on this website; I also am a blogger, and I was wanting to know
    your situation; many of us have created some nice methods
    and we are looking to trade strategies with others, please
    shoot me an e-mail if interested.

  5. We absolutely love your blog and find almost all of your post’s
    to be exactly what I’m looking for. can you offer guest writers to write content
    for you? I wouldn’t mind publishing a post or elaborating
    on a lot of the subjects you write in relation to here.
    Again, awesome blog!

  6. Thanks for another informative site. Where else may I am getting that kind of info written in such
    an ideal approach? I have a undertaking that I’m just now operating on, and I have been at
    the glance out for such information. plenty of fish natalielise

  7. Hi, I do believe this is a great website. I stumbledupon it
    😉 I will come back yet again since i have book-marked it.

    Money and freedom is the greatest way to change, may you be rich and
    continue to guide other people.

  8. Hello, i read your blog from time to time and i own a similar one and i was just
    curious if you get a lot of spam comments? If so how do you protect against it,
    any plugin or anything you can suggest? I get so much lately it’s driving me crazy so any assistance is
    very much appreciated.

  9. Normally I don’t learn article on blogs, but I would like to say that this write-up very forced me to try and do so!
    Your writing style has been amazed me. Thanks, very great article.

  10. Attractive element of content. I simply stumbled upon your site and in accession capital to claim that I acquire actually enjoyed
    account your blog posts. Any way I will be subscribing
    for your feeds or even I achievement you access consistently rapidly.

  11. Hello there I am so glad I found your blog page, I really found you by error,
    while I was searching on Google for something else, Regardless I am here
    now and would just like to say many thanks for a tremendous post and a all round
    exciting blog (I also love the theme/design),
    I don’t have time to read it all at the moment but I have saved it and also added your RSS feeds, so when I have time I will be back to
    read a great deal more, Please do keep up the excellent job.

  12. I like the valuable information you provide in your articles.
    I will bookmark your blog and check again here regularly.

    I’m quite certain I’ll learn a lot of new stuff right here!

    Good luck for the next!

  13. Fantastic items from you, man. I’ve remember
    your stuff prior to and you are just extremely excellent.
    I actually like what you have acquired here, certainly like what you are saying and the best
    way through which you say it. You’re making it enjoyable and you continue to care for to stay it smart.
    I can not wait to read far more from you. That is really a
    great website.

  14. I like the valuable info you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I’m quite sure I’ll learn lots of new stuff right here!
    Best of luck for the next!

  15. I blog frequently and I really appreciate your information. This article has
    really peaked my interest. I will book mark your website and keep
    checking for new information about once per week. I opted
    in for your RSS feed too.

  16. Whats up very nice site!! Guy .. Beautiful
    .. Wonderful .. I will bookmark your site and take the feeds additionally?
    I’m satisfied to find numerous useful info here in the put up, we’d like work out more strategies in this regard, thank you for sharing.
    . . . . .

  17. Attractive component to content. I simply stumbled upon your weblog and in accession capital to assert that
    I acquire actually enjoyed account your weblog posts.
    Any way I’ll be subscribing on your feeds or
    even I achievement you get admission to constantly fast.

  18. I believe this is among the most significant info for me.
    And i am satisfied studying your article. But want to remark on some basic issues, The web site style is ideal, the articles is actually nice
    : D. Excellent process, cheers

  19. It is appropriate time to make some plans for the future and
    it is time to be happy. I’ve read this post and if I could I wish to
    suggest you few interesting things or suggestions.

    Maybe you can write next articles referring to this article.
    I desire to read more things about it!

  20. Please let me know if you’re looking for a author for your site.

    You have some really good articles and I think I would be a good asset.
    If you ever want to take some of the load off, I’d absolutely
    love to write some articles for your blog in exchange for
    a link back to mine. Please shoot me an email if interested.
    Cheers!

  21. I don’t even understand how I stopped up here,
    but I assumed this put up used to be good. I
    don’t know who you are however definitely you are going to a famous blogger
    in the event you are not already. Cheers!

  22. Greate pieces. Keep writing such kind of information on your page.
    Im really impressed by your site.
    Hello there, You have performed an incredible
    job. I will certainly digg it and individually recommend to my friends.
    I am sure they will be benefited from this web site.

  23. I every time used to study piece of writing in news papers but now as I am a user of
    web so from now I am using net for articles or reviews, thanks
    to web.

  24. My brother suggested I might like this web site. He was entirely right.

    This post actually made my day. You can not imagine just how much time I had spent for this information! Thanks!

  25. You really make it seem really easy along with your presentation but I find this matter to be really one thing which I feel I’d by no means understand. It seems too complex and extremely huge for me. I’m looking forward for your subsequent put up, I¦ll try to get the hold of it!

  26. Please let me know if you’re looking for a article author for your weblog.

    You have some really great posts and I feel 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 blast me an email if interested.

    Thanks!

  27. Hi, There’s no doubt that your blog may be having internet browser compatibility problems.
    When I look at your website in Safari, it looks fine however when opening
    in IE, it has some overlapping issues. I simply wanted to give you a quick heads
    up! Besides that, wonderful blog!

  28. Hi there! Would you mind if I share your blog with my myspace group?
    There’s a lot of people that I think would really enjoy your
    content. Please let me know. Many thanks

  29. 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 site when you could be giving us something informative to read?

  30. I am really impressed with your writing skills and also with the layout on your blog.
    Is this a paid theme or did you modify it
    yourself? Either way keep up the excellent quality writing, it is rare to see a nice blog like this one nowadays.

Leave a Reply

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