1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #define int long long #define bi(x) (((x)-1)/block) #define sb(x) ((x)*block+1) #define eb(x) min(((x)+1)*block,n) const int N=1e5+10,block=1000; struct Block {
int id[N],tmp[N],e,tag[bi(N)+10],buf1[N],buf2[N],a[N],n,m; Block() { e=-1; memset(id,0,sizeof(id)); memset(tag,0,sizeof(tag)); memset(a,0,sizeof(a)); memset(tmp,0,sizeof(tmp)); memset(buf1,0,sizeof(buf1)); memset(buf2,0,sizeof(buf2)); } void add(int l,int r,int c) { int lb=bi(l),rb=bi(r); if(lb==rb) { for(int i=l; i<=r; i++)a[i]+=c;
int e1 = -1, e2 = -1, p1 = 0, p2 = 0; for(int i=sb(lb); i<=eb(lb); i++) if(id[i] >= l && id[i] <= r) buf1[++ e1] = id[i]; else buf2[++ e2] = id[i]; for(int i=sb(lb); i<=eb(lb); i++) if(p2 > e2 || p1 <= e1 && a[buf1[p1]] < a[buf2[p2]]) id[i] = buf1[p1 ++]; else id[i] = buf2[p2 ++]; return; } for(int i=lb+1; i<=rb-1; i++)tag[i]+=c; for(int i=l; i<=eb(lb); i++)a[i]+=c; for(int i=sb(rb); i<=r; i++)a[i]+=c;
int e1 = -1, e2 = -1, p1 = 0, p2 = 0; for(int i=sb(lb); i<=eb(lb); i++) if(id[i] >= l && id[i] <= r) buf1[++ e1] = id[i]; else buf2[++ e2] = id[i]; for(int i=sb(lb); i<=eb(lb); i++) if(p2 > e2 || p1 <= e1 && a[buf1[p1]] < a[buf2[p2]]) id[i] = buf1[p1 ++]; else id[i] = buf2[p2 ++];
e1 = -1, e2 = -1, p1 = 0, p2 = 0; for(int i=sb(rb); i<=eb(rb); i++) if(id[i] >= l && id[i] <= r) buf1[++ e1] = id[i]; else buf2[++ e2] = id[i]; for(int i=sb(rb); i<=eb(rb); i++) if(p2 > e2 || p1 <= e1 && a[buf1[p1]] < a[buf2[p2]]) id[i] = buf1[p1 ++]; else id[i] = buf2[p2 ++];
} void init(int l,int r) { int lb=bi(l),rb=bi(r); e=-1; if(lb==rb) { for(int i=sb(lb); i<=eb(lb); i++) if(id[i] >= l && id[i] <= r) tmp[++ e] = id[i]; } else { int e1 = -1, e2 = -1, p1 = 0, p2 = 0; for(int i=sb(lb); i<=eb(lb); i++) if(id[i] >= l && id[i] <= r) buf1[++ e1] = id[i]; for(int i=sb(rb); i<=eb(rb); i++) if(id[i] >= l && id[i] <= r) buf2[++ e2] = id[i];
while(p1 <= e1 || p2 <= e2) { if(p2 > e2 || p1 <= e1 && a[buf1[p1]] + tag[lb] < a[buf2[p2]] + tag[rb]) tmp[++ e] = buf1[p1 ++]; else tmp[++ e] = buf2[p2 ++]; } } } int cal(int l,int r,int c) { int lb = bi(l), rb = bi(r); int ans = 0; for(int i= lb + 1; i<= rb - 1; i++) { int l = sb(i), r = eb(i); if(a[id[l]] + tag[i] >= c) continue; while(l < r) { int mid = (l + r + 1) >> 1; if(a[id[mid]] + tag[i] < c) l = mid; else r = mid - 1; } ans += l - sb(i) + 1; } if(~e && a[tmp[0]] + tag[bi(tmp[0])] < c) { int l = 0, r = e; while(l < r) { int mid = (l + r + 1) >> 1; if(a[tmp[mid]] + tag[bi(tmp[mid])] < c) l = mid; else r = mid - 1; } ans += l + 1; } return ans; } long long find_kth(int l,int r,int c) { if(c>r-l+1)return -1; else { int ll=-2e9,rr=2e9; init(l,r); while(ll<rr) { int mid=(ll+rr+1)>>1; if(cal(l,r,mid)<c)ll=mid; else rr=mid-1; } return ll; } } void id_sort() { for(int i=bi(1); i<=bi(n); i++)sort(id+sb(i),id+eb(i)+1,[&](int x,int y) { return a[x]<a[y]; }); } }
|