123

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 300010
#define ll long long 
#define pb push_back
#define reg register int
//#define int long long 

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, m;
int l[N], r[N], uni[N<<1], usize;
ll a[N];

namespace force{
	int cnt[N]; ll ans;
	void solve() {
		int lim=1<<m;
		for (reg k=1; k<=m; ++k) {
			ans=0;
			for (reg s=1; s<lim; ++s) {
				ll sum=0;
				memset(cnt+1, 0, sizeof(int)*usize);
				//memset(cnt, 0, sizeof(cnt));
				for (reg i=0; i<m; ++i) if (s&(1<<i)) {
					sum+=a[i+1];
					for (reg j=l[i+1]; j<max(l[i+1]+1, r[i+1]); ++j) 
						if (++cnt[j]>k) goto jump;
				}
				ans=max(ans, sum);
				jump: ;
			}
			printf("%lld ", ans);
		}
		printf("\n");
		exit(0);
	}
}

namespace task1{
	int head[N], size, top, dp[5010][5010];
	struct edge{int to, next;}e[N<<1];
	inline void add(int s, int t) {edge* k=&e[++size]; k->to=t; k->next=head[s]; head[s]=size;}
	struct seg{int l, r, a; inline void build(int l_, int r_, int a_){l=l_; r=r_; a=a_;}}s[N], sta[N];
	inline bool operator < (seg a, seg b) {return a.l==b.l?a.r>b.r:a.l<b.l;};
	vector<int> e2[N], val[N];
	int dfs(int u) {
		int p=1;
		for (int i=0; i<(int)(val[u].size()); ++i) 
			for (int j=m; j; --j)
				dp[u][j] = max(dp[u][j], dp[u][j-1]+val[u][i]);
		for (int i=head[u],v,siz; i; i=e[i].next) {
			v = e[i].to;
			siz=dfs(v);
			for (int j=m; ~j; --j) {
				for (int h=1; j+h<=m; ++h) {
					dp[u][j+h] = max(dp[u][j+h], dp[u][j]+dp[v][h]);
				}
			}
			p+=siz;
		}
		return p;
	}
	void solve() {
		for (int i=1; i<=m; ++i) s[i].build(l[i], r[i], a[i]);
		sort(s+1, s+m+1);
		for (int i=1; i<=m; ++i) {
			while (top && sta[top].r<=s[i].l) --top;
			if (top) e2[sta[top].l].pb(s[i].l);
			else e2[0].pb(s[i].l);
			sta[++top]=s[i];
			val[max(s[i].l, s[i].r-1)].pb(s[i].a);
		}
		for (int i=0,newsize; i<=usize; ++i) {
			newsize=unique(e2[i].begin(), e2[i].end())-e2[i].begin();
			e2[i].resize(newsize);
			for (int j=0; j<(int)(e2[i].size()); ++j) if (i!=e2[i][j]) add(i, e2[i][j]), cout<<"add "<<i<<' '<<e2[i][j]<<endl;
		}
		dfs(0);
		for (int i=1; i<=m; ++i) cout<<dp[3][i]<<' '; cout<<endl;
		for (int i=1,sum; i<=m; ++i) {
			sum=0;
			for (int j=head[0]; j; j=e[j].next) 
				sum+=dp[e[j].to][i];
			printf("%d ", sum);
		}
		printf("\n");
		exit(0);
	}
}

namespace task2{
	int head[N], size, top, tot; ll dp[5010][5010];
	struct edge{int to, next;}e[N<<1];
	inline void add(int s, int t) {edge* k=&e[++size]; k->to=t; k->next=head[s]; head[s]=size;}
	struct seg{int l, r, rk; ll a; inline void build(int l_, int r_, ll a_){l=l_; r=r_; a=a_;}}s[N], sta[N];
	inline bool operator < (seg a, seg b) {return a.l==b.l?a.r>b.r:a.l<b.l;};
	inline bool cmp(int a, int b) {return a>b;}
	vector<ll> val[N];
	int dfs(int u) {
		int p=1;
		for (int i=head[u],v,siz; i; i=e[i].next) {
			v = e[i].to;
			siz=dfs(v);
			for (int j=1; j<=m; ++j) dp[u][j]+=dp[v][j];
			p+=siz;
		}
		//if (u==3) {cout<<"dp[3]: "; for (int i=1; i<=m; ++i) cout<<dp[u][i]<<' '; cout<<endl;}
		for (int i=0; i<(int)(val[u].size()); ++i)
			for (int j=m; j>i; --j) 
				dp[u][j] = max(dp[u][j], dp[u][j-1]+val[u][i]);
		//if (u==3) {cout<<"dp[3]: "; for (int i=1; i<=m; ++i) cout<<dp[u][i]<<' '; cout<<endl;}
		return p;
	}
	void solve() {
		for (int i=1; i<=m; ++i) s[i].build(l[i], r[i], a[i]); //, cout<<r[i]<<' '; cout<<endl;
		s[m+1].build(1, usize+1, 0);
		sort(s+1, s+m+2);
		for (int i=1; i<=m+1; ++i) {
			while (top && sta[top].r<=s[i].l && sta[top].l!=s[i].l) --top;
			if (top && sta[top].l==s[i].l && sta[top].r==s[i].r) {
				val[sta[top].rk].pb(s[i].a);
				continue;
			}
			if (top) add(sta[top].rk, s[i].rk=++tot);
			else s[i].rk=++tot;
			sta[++top]=s[i];
			val[tot].pb(s[i].a);
		}
		//cout<<"tot: "<<tot<<endl;
		//cout<<"size: "<<size<<endl;
		for (int i=1; i<=tot; ++i) sort(val[i].begin(), val[i].end(), cmp);
		dfs(1);
		//cout<<"dp: "; for (int i=1; i<=m; ++i) cout<<dp[2][i]<<' '; cout<<endl;
		ll sum;
		for (int i=1; i<=m; ++i) {
			sum=0;
			for (int j=head[1]; j; j=e[j].next) 
				sum+=dp[e[j].to][i];
			printf("%lld ", sum);
		}
		printf("\n");
		exit(0);
	}
}

namespace task{
	int head[N], size, top, tot, siz[N], msiz[N], mson[N], stop; ll st[N];
	priority_queue<ll> dp[N];
	struct edge{int to, next;}e[N<<1];
	inline void add(int s, int t) {edge* k=&e[++size]; k->to=t; k->next=head[s]; head[s]=size;}
	struct seg{int l, r, rk; ll a; inline void build(int l_, int r_, ll a_){l=l_; r=r_; a=a_;}}s[N], sta[N];
	inline bool operator < (seg a, seg b) {return a.l==b.l?a.r>b.r:a.l<b.l;};
	inline bool cmp(int a, int b) {return a>b;}
	vector<ll> val[N];
	void dfs1(int u) {
		siz[u]=1;
		for (int i=head[u],v; i; i=e[i].next) {
			v = e[i].to;
			dfs1(v);
			siz[u]+=siz[v];
			if (siz[v]>msiz[u]) msiz[u]=siz[v], mson[u]=v;
		}
	}
	void dfs2(int u) {
		if (!mson[u]) {for (auto it:val[u]) dp[u].push(it); return;}
		bool empty=0; stop=0;
		for (int i=head[u]; i; i=e[i].next) dfs2(e[i].to);
		swap(dp[u], dp[mson[u]]);
		while (!empty) {
			empty=1; ++stop;
			for (int i=head[u],v; i; i=e[i].next) {
				v = e[i].to;
				if (v==mson[u]) continue;
				if (!dp[v].empty()) {
					empty=0;
					st[stop]+=dp[v].top(); dp[v].pop();
				}
			}
			if (!empty) {if (!dp[u].empty()) st[stop]+=dp[u].top(), dp[u].pop();}
			else --stop;
		}
		for (int i=1; i<=stop; ++i) dp[u].push(st[i]), st[i]=0;
		for (auto it:val[u]) dp[u].push(it); //, cout<<1<<endl;
	}
	void solve() {
		for (int i=1; i<=m; ++i) s[i].build(l[i], r[i], a[i]); //, cout<<r[i]<<' '; cout<<endl;
		s[m+1].build(1, usize+1, 0);
		sort(s+1, s+m+2);
		for (int i=1; i<=m+1; ++i) {
			while (top && sta[top].r<=s[i].l && sta[top].l!=s[i].l) --top;
			if (top && sta[top].l==s[i].l && sta[top].r==s[i].r) {
				val[sta[top].rk].pb(s[i].a);
				continue;
			}
			if (top) add(sta[top].rk, s[i].rk=++tot);
			else s[i].rk=++tot;
			sta[++top]=s[i];
			val[tot].pb(s[i].a);
		}
		//cout<<"tot: "<<tot<<endl;
		//cout<<"size: "<<size<<endl;
		dfs1(1); dfs2(1);
		//cout<<"dp: "; for (int i=1; i<=m; ++i) cout<<dp[2][i]<<' '; cout<<endl;
		ll sum=0;
		for (int i=1; i<=m; ++i) {
			if (!dp[1].empty()) sum+=dp[1].top(), dp[1].pop();
			printf("%lld ", sum);
		}
		printf("\n");
		exit(0);
	}
}

signed main()
{
	n=read(); m=read();
	for (int i=1; i<=m; ++i) {
		l[i]=read(); r[i]=read(); a[i]=read();
		uni[++usize]=l[i]; uni[++usize]=r[i];
	}
	sort(uni+1, uni+usize+1);
	usize=unique(uni+1, uni+usize+1)-uni-1;
	//cout<<"usize: "<<usize<<endl;
	for (int i=1; i<=m; ++i) {
		//cout<<"old: "<<l[i]<<' '<<r[i]<<endl;
		l[i]=lower_bound(uni+1, uni+usize+1, l[i])-uni;
		r[i]=lower_bound(uni+1, uni+usize+1, r[i])-uni;
		//cout<<"new: "<<l[i]<<' '<<r[i]<<endl;
	}
	//force::solve();
	//task1::solve();
	task::solve();
	
	return 0;
}

上一篇:docker-compose安装


下一篇:vue+egg.js使用websocket