傳送門:
題意:給定序列a1,a2...an以及m個區間,從m個區間中選出k個區間使k個區間相交的部分和最大。
思路:預處理字首和。
按照r從小到大l從大到小的順序將所有區間排序,用線段樹維護區間第k小。然後列舉區間的右端點,求出當前第k小左端點,然後計算差值即可。
#include #include #include #include #include #include #include using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
int t,n,m,k;
int a[maxn];
ll sum[maxn];
int v[maxn<<2];
struct interval
}p[maxn];
void pushup(int rt)
void update(int rt,int l,int r,int pos)
int mid=(l+r)>>1;
if(pos<=mid) update(lson,pos);
else update(rson,pos);
pushup(rt);
}int query(int rt,int l,int r,int k)
int main()
for(int i=1;i<=m;i++) scanf("%d%d",&p[i].l,&p[i].r);
sort(p+1,p+m+1);
ll ans=0;
for(int i=m;i>=1;i--)
}printf("%lld\n",ans);
}return 0;
}
HDU 5700 區間交(線段樹)
problem description 小a有乙個含有n個非負整數的數列與m個區間。每個區間可以表示為li,ri。它想選擇其中k個區間,使得這些區間的交的那些位置所對應的數的和最大。例如樣例中,選擇 2,5 與 4,5 兩個區間就可以啦。input 多組測試資料 第一行三個數n,k,m 1 n 10...
HDU 5700 區間交 離線線段樹
小a有乙個含有n個非負整數的數列與m個區間。每個區間可以表示為li,ri。它想選擇其中k個區間,使得這些區間的交的那些位置所對應的數的和最大。例如樣例中,選擇 2,5 與 4,5 兩個區間就可以啦。多組測試資料 第一行三個數n,k,m 1 n 100000,1 k m 100000 接下來一行n個數...
hdu 5700 區間交(優先佇列)
思路 現將每條線段按左端點公升序排序,依次往堆裡加右端點,若果size k就pop一下。如果size k並且最小的大於當前l則更新一下。因為這樣每個左端點的對應右端點一定是最大的,這裡堆的作用相當於事實求出左端點比當前左端點小的線段中第k大的右端點,而這個肯定是最大的當前左端點對應的區間。inclu...