题意
有 条线段,每条线段有左端点,右端点,颜色,权值()四个参数,选出若干条线段,使得这些线段两两满足颜色相等或不交,即:
- 对于任意两条不同的线段 ,满足 或 。
求最大的权值和。
思路
考虑 dp,先对 离散化,记 表示考虑所有颜色为 , 的线段 的答案。
直接做不好弄,考虑对线段按左端点排序,那么转移可以分为三种,同色,异色,以及对后面同色的贡献:
显然可以用线段树优化,对每种颜色开一个线段树即可,要动态开点。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n;
int rt[N],cnt=0;
struct node{
int ls,rs,mx,tag;
node& operator += (int c){
tag+=c,mx+=c;
return *this;
}
}t[N<<6];
#define ls (t[p].ls)
#define rs (t[p].rs)
#define mid ((l+r)>>1)
void down(int p){
if(!t[p].tag) return;
if(!ls) ls=++cnt;t[ls]+=t[p].tag;
if(!rs) rs=++cnt;t[rs]+=t[p].tag;
t[p].tag=0;
}
void update(int &p,int l,int r,int L,int R,int c){
if(L>R) return;
if(!p) p=++cnt;
if(L<=l && r<=R) return t[p]+=c,void();
down(p);
if(L<=mid) update(ls,l,mid,L,R,c);
if(R>mid) update(rs,mid+1,r,L,R,c);
t[p].mx=max(t[ls].mx,t[rs].mx);
}
void update(int &p,int l,int r,int x,int c){
if(!p) p=++cnt;
if(l==r) return t[p].mx=max(t[p].mx,c),void();
down(p);
if(x<=mid) update(ls,l,mid,x,c);
else update(rs,mid+1,r,x,c);
t[p].mx=max(t[ls].mx,t[rs].mx);
}
int query(int p,int l,int r,int L,int R){
if(!p || L>R || l>R || L>r) return 0;
if(L<=l && r<=R) return t[p].mx;
down(p);
return max(query(ls,l,mid,L,R),query(rs,mid+1,r,L,R));
}
struct info{
int l,r,c,w;
}a[N];
#undef ls
#undef rs
#undef mid
map <int,int> mp;
int m=0,ls[N<<1],c=0,ans=0;
vector <int> v[N<<1];
signed mian(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].l>>a[i].r>>a[i].c>>a[i].w;
if(!mp.count(a[i].c)) mp[a[i].c]=++c;
a[i].c=mp[a[i].c];
ls[++m]=a[i].l;ls[++m]=a[i].r;
}
sort(ls+1,ls+m+1);
m=unique(ls+1,ls+m+1)-ls-1;
sort(a+1,a+n+1,[&](info x,info y){
return x.l==y.l ? x.r<y.r : x.l<y.l;
});
for(int i=1,j=1,pre=0;i<=n;i++){
a[i].l=lower_bound(ls+1,ls+m+1,a[i].l)-ls;
a[i].r=lower_bound(ls+1,ls+m+1,a[i].r)-ls;
v[a[i].r].push_back(a[i].c);
for(;j<a[i].l;j++) for(int c:v[j]) pre=max(pre,query(rt[c],1,m,1,a[i].l-1));
int tmp=max(pre,query(rt[a[i].c],1,m,1,a[i].r))+a[i].w;
update(rt[a[i].c],1,m,a[i].r,tmp);
update(rt[a[i].c],1,m,a[i].r+1,m,a[i].w);
ans=max(ans,max(tmp,t[rt[a[i].c]].mx));
}
cout<<ans;
return 0;
}