Interval sex
Time limit:8000/4000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 567 Accepted Submission (s): 279
Problem description Small A has aNA series of non-negative integers andmA range. Each interval can be expressed asli,rI 。
It wants to choose whichkThe number and maximum of the positions of the intersection of these intervals.
For example, in the sample, select[2,5] and [4,5] two intervals are OK.
Input multiple sets of test data
First row three numbersn,K,m(1≤n≤100000,1≤k≤m≤ 100000) 。
Next lineNNumberai , which represents the sequence of lYK(0≤ai≤9) 。
NextmRows, two numbers per lineli,rI , indicating that each interval (1 ≤li≤ ri≤n) .
The output row represents the answer
Sample INPUT5 2 31 2 3 4 64 52 51 4
Sample Output10
Source2016 "Baidu Star"-The first round (Astar round2b) problem-solving ideas: firstly sort the right endpoint from small to large (can also sort the left end, also can be from the big to the small sort, see how to deal with), Then enumerate the right end point (to ensure that the enumerated endpoint has at least K interval can be overwritten) as the intersection of the right end of the request, it is necessary to ask for the left end of the intersection interval, we can know that the right end point is determined, if the left end of the more left, the range of this interval is about large. To ensure that the intersection has K, we need to find the K small left endpoint, in order to ensure that the right endpoint of my enumeration is definitely the right end of the intersection interval, we must enumerate the edges, the edge of a single point update the left endpoint.
#include <stdio.h> #include <algorithm> #include <string.h> #include <math.h> #include < string> #include <iostream> #include <queue> #include <stack> #include <map> #include < Vector> #include <set>using namespace std;typedef long long LL; #define MID (L+r)/2#define Lson rt*2,l,mid#define Rson Rt*2+1,mid+1,r#pragma COMMENT (linker, "/stack:102400000,102400000") const int MAXN = 1e5+300;const int INF = 0x3f3f3f 3f;typedef Long long ll;typedef unsigned long long ULL; LL presum[maxn];struct interval{int L, R;} intervals[maxn];struct seg{int cover;} Segs[maxn*4];bool CMP (Interval A, Interval b) {return A.R < B.R; }void pushup (int rt) {Segs[rt].cover = Segs[rt*2].cover + segs[rt*2+1].cover;} void Buildtree (int rt,int l,int R) {if (L = = r) {segs[rt].cover = 0; Return } buildtree (Lson); Buildtree (Rson); Pushup (RT);} void Update (int rt,int l,int r,int id) {if (L = = R) {segs[rt].cover++; return; } if (ID <= mid) {Update (lson,id); }else{Update (Rson,id); } pushup (RT);} int query (int rt,int l, int r,int k) {if (L = = R) {return l; } if (k <= segs[rt*2].cover) {return query (LSON,K); }else{return query (Rson,k-segs[rt*2].cover); }}int Main () {int n, K, m; while (scanf ("%d%d%d", &n,&k,&m)!=eof) {buildtree (1,1,n); LL A; for (int i = 1; I <= n; i++) {scanf ("%lld", &a); Presum[i] = presum[i-1] + A; } int L, R; for (int i = 1; I <= m; i++) {scanf ("%d%d", &l,&r); INTERVALS[I].L = l; INTERVALS[I].R = R; } sort (intervals+1,intervals+1+m,cmp); for (int i = m-k+1; I <= m; i++) {Update (1,1,N,INTERVALS[I].L); } LL ans = 0; for (int i = m-k+1; I >= 1; i--) {int L = query (1,1,n,k); if (l <= intervals[i].r) { ans = max (ans, PRESUM[INTERVALS[I].R]-presum[l-1]); } Update (1,1,N,INTERVALS[I-1].L); } printf ("%lld\n", ans); } return 0;} /*5 1 11 2 3 4 64 53 4*/
HDU 5700--interval Intersection —————— "segment Tree + enumeration"