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
| #include <bits/stdc++.h> #define re register
using namespace std;
const int N = 1e6 + 10,M = 3e5 + 10,inf = 1e9 + 10; int n,m,q,rt,num; int ans[N]; multiset<int> pre[N];
struct Query{ int op,x,tim,ty;
bool friend operator <(const Query &a,const Query &b){ if (a.tim != b.tim) return a.tim < b.tim; return a.op < b.op; } }; vector<Query> Q;
inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; }
struct seg{ #define ls(u) (tr[u].l) #define rs(u) (tr[u].r) #define st(u) (tr[u].st)
int idx;
struct node{ int l,r,Min; multiset<int> st; }tr[M * 30];
inline void pushup(int u){ tr[u].Min = min(tr[ls(u)].Min,tr[rs(u)].Min); }
inline void insert(int &u,int l,int r,int x,int k,int falg){ if (!u) u = ++idx; if (l == r){ if (falg == 1) st(u).insert(k); else st(u).erase(st(u).find(k)); if (st(u).empty()) tr[u].Min = inf; else tr[u].Min = (*st(u).begin()); return; } int mid = l + r >> 1; if (x <= mid) insert(ls(u),l,mid,x,k,falg); else insert(rs(u),mid + 1,r,x,k,falg); pushup(u); }
inline int query(int x){ if (num < m) return -1; int u = rt,l = 1,r = inf,lst = inf; while (l < r){ int mid = l + r >> 1; int Min = min(lst,tr[rs(u)].Min); if (mid < x || Min < 2 * x - mid) u = rs(u),l = mid + 1; else lst = Min,u = ls(u),r = mid; } return l - x; }
#undef ls #undef rs #undef st }T;
int main(){ T.tr[0].Min = inf; n = read(),m = read(),q = read(); for (re int i = 1;i <= m;i++){ pre[i].insert(-inf); pre[i].insert(inf); T.insert(rt,1,inf,inf,-inf,1); } for (re int i = 1;i <= n;i++){ int x,ty,l,r; x = read(),ty = read(),l = read(),r = read(); Q.push_back({1,x,l,ty}); Q.push_back({2,x,r + 1,ty}); } for (re int i = 1;i <= q;i++){ int x,tim; x = read(),tim = read(); Q.push_back({3,x,tim,i}); } sort(Q.begin(),Q.end()); for (auto p:Q){ if (p.op == 1){ auto jt = pre[p.ty].lower_bound(p.x); auto it = prev(jt); T.insert(rt,1,inf,p.x,*it,1); T.insert(rt,1,inf,*jt,*it,2); T.insert(rt,1,inf,*jt,p.x,1); if (pre[p.ty].size() == 2) num++; pre[p.ty].insert(p.x); } else if (p.op == 2){ auto jt = pre[p.ty].lower_bound(p.x); auto it = prev(jt); jt = next(jt); T.insert(rt,1,inf,*jt,p.x,2); T.insert(rt,1,inf,p.x,*it,2); T.insert(rt,1,inf,*jt,*it,1); pre[p.ty].erase(pre[p.ty].find(p.x)); if (pre[p.ty].size() == 2) num--; } else ans[p.ty] = T.query(p.x); } for (re int i = 1;i <= q;i++) printf("%d\n",ans[i]); return 0; }
|