【模板库】斜堆

参考题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809
参考代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
using namespace std;

const int N = 100000+9;

LL vout;
int led[N],n,m,arr[N],rt;
vector<int> G[N];

inline int read(){
	char c=getchar(); int ret=0,f=1;
	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;
}

namespace Skew_Heap{
	#define SHP Skew_Heap
	int ch[N][2],sz[N],val[N],root[N],cnt;
	LL sum[N];
	
	inline void maintain(int w) {
		sz[w] = sz[ch[w][0]] + sz[ch[w][1]] + 1;
		sum[w] = sum[ch[w][0]] + sum[ch[w][1]] + val[w];
	}
	
	int Merge(int a, int b){
		if (!a || !b) return a+b;
		else {
			if (val[b] > val[a]) swap(a,b);
			ch[a][1] = Merge(ch[a][1],b);
			swap(ch[a][0],ch[a][1]); maintain(a); 
			return a;
		}
	}
	
	inline void pop(int w){
		root[w] = Merge(ch[root[w]][0],ch[root[w]][1]);
	}
	
	inline void insert(int w, int v){
		val[++cnt] = sum[cnt] = v; sz[cnt] = 1;
		root[w] = Merge(root[w],cnt);
	}
};

int main(){
	using namespace Skew_Heap;
	n = read(); m = read();
	for (int i=1,tmp;i<=n;i++) {
		G[read()].push_back(i);
		arr[i] = read(); led[i] = read();
	}
	for (int i=n;i;i--) {
		for (int j=0,lim=G[i].size();j<lim;j++) root[i] = Merge(root[i],root[G[i][j]]);
		insert(i,arr[i]); while (root[i] && sum[root[i]] > m) pop(i);
		vout = max(vout,(LL)sz[root[i]]*led[i]);
	} 
	printf("%lld\n",vout);
	return 0;
}

31 thoughts to “【模板库】斜堆”

  1. Hiya very cool web site!! Man .. Beautiful .. Superb ..
    I’ll bookmark your blog and take the feeds additionally?
    I am happy to search out so many helpful info right here in the
    put up, we want work out more techniques on this regard, thanks for sharing.
    . . . . .

  2. Today, I went to the beachfront with my children. I found
    a sea shell and gave it to my 4 year old daughter and said
    “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear. She never wants to go back!
    LoL I know this is completely off topic but I had to tell someone!

  3. Very nice post. I just stumbled upon your weblog and wanted to say that I have
    really enjoyed surfing around your blog posts. After all I
    will be subscribing to your rss feed and I hope you write again very soon!

  4. I think this is one of the most vital info for me.

    And i am glad reading your article. But wanna remark on few general things,
    The website style is wonderful, the articles is really great : D.
    Good job, cheers

  5. I needed to thank you for this very good read!! I absolutely
    enjoyed every bit of it. I have got you saved as a favorite to look at
    new stuff you post…

  6. Attractive portion of content. I just stumbled upon your web site and in accession capital to assert that I get in fact loved account your blog posts.
    Anyway I’ll be subscribing to your augment or even I fulfillment you get entry to consistently rapidly.

  7. Hello there, I found your site via Google whilst looking for a comparable
    matter, your site got here up, it appears great. I have bookmarked it in my google bookmarks.

    Hello there, just turned into aware of your blog via Google, and found that
    it is truly informative. I’m going to be careful for brussels.
    I’ll appreciate if you happen to continue this in future.
    A lot of folks can be benefited out of your writing.
    Cheers!

  8. Oh my goodness! Amazing article dude! Thank you,
    However I am encountering troubles with your RSS. I don’t
    know the reason why I can’t join it. Is there anyone else getting the same RSS issues?
    Anybody who knows the solution can you kindly respond?
    Thanks!!

  9. Wonderful blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get listed in Yahoo
    News? I’ve been trying for a while but I never
    seem to get there! Appreciate it

  10. Hey there exceptional website! Does running a blog similar to this require a great deal of work?
    I’ve absolutely no knowledge of programming but I was hoping to start my own blog in the near future.
    Anyhow, if you have any ideas or techniques for new blog owners please share.

    I know this is off subject but I just had to ask. Appreciate it!

  11. Its like you read my thoughts! You seem to understand a lot about this,
    like you wrote the book in it or something.
    I feel that you could do with a few percent to force the message house a bit, however
    instead of that, this is excellent blog. A great read.
    I’ll definitely be back.

  12. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your
    website? My blog is in the very same niche as yours
    and my visitors would definitely benefit from a lot of the
    information you provide here. Please let me know if this okay with
    you. Cheers!

  13. What i don’t understood is if truth be told how you’re not really a lot
    more smartly-liked than you may be now. You’re so intelligent.
    You know therefore significantly in terms of this matter, made me personally consider it from numerous
    varied angles. Its like women and men don’t seem to be interested
    except it’s one thing to do with Girl gaga!

    Your personal stuffs great. All the time take care of it up!

  14. My brother suggested I might like this blog.
    He was totally right. This post actually made my day.
    You cann’t imagine just how much time I had spent for this info!
    Thanks!

  15. Howdy this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

Leave a Reply

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