#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 300000;
struct Store {
int x, t, a, b;
};
struct Event {
int time, x, t, id;
bool scoate;
};
struct Query {
int l, y;
int *rez;
};
bool cmp1(Event a, Event b) { return a.time < b.time; }
bool cmp2(int *a, int *b) { return *a < *b; }
bool cmp3(Query a, Query b) { return a.y < b.y; }
Store v[MAX_N];
int auxx[2*MAX_N];
Event event[2*MAX_N];
Query query[MAX_N];
int rez[MAX_N];
int rangeL[MAX_N], rangeR[MAX_N];
bool activeStore[MAX_N];
set<pair<int, int> > typestore[1+MAX_N];
vector<pair<int, int> > transf;
void normalize(int n) {
vector<int*> vp;
for(int i = 0; i < n; ++i)
vp.push_back(&auxx[i]);
sort(vp.begin(), vp.end(), cmp2);
int lastV = -1, j = 0;
for(auto it: vp) {
if(*it == lastV)
*it = j;
else {
lastV = *it;
transf.push_back(make_pair(*it, ++j));
*it = j;
}
}
}
priority_queue<pair<int, int>, vector<pair<int, int>>, std::greater<pair<int, int>>> smallboi[1+8*MAX_N];
priority_queue<pair<int, int>, vector<pair<int, int>>, std::less<pair<int, int>>> bigboi[1+8*MAX_N];
void updateAint(int pozl, int pozr, pair<int, int> val, int l, int r, int nod = 1) {
if(r < l || r < pozl || pozr < l || pozr < pozl) return;
if(pozl <= l && r <= pozr) {
bigboi[nod].push(val);
smallboi[nod].push(val);
} else if(l < r) {
int mid = (l + r) / 2;
updateAint(pozl, pozr, val, l, mid, 2 * nod);
updateAint(pozl, pozr, val, mid + 1, r, 2 * nod + 1);
}
}
pair<int, int> add(pair<int, int> a, pair<int, int> b) {
return make_pair(min(a.first, b.first), max(a.second, b.second));
}
pair<int, int> queryAint(int poz, int l, int r, int nod = 1) {
pair<int, int> rezQ(1000000000, -1000000000);
if(r < poz || poz < l || r < l) return rezQ;
while(!bigboi[nod].empty() && (!activeStore[bigboi[nod].top().second] ||
!(rangeL[bigboi[nod].top().second] <= l &&
r <= rangeR[bigboi[nod].top().second])))
bigboi[nod].pop();
while(!smallboi[nod].empty() && (!activeStore[smallboi[nod].top().second] ||
!(rangeL[smallboi[nod].top().second] <= l &&
r <= rangeR[smallboi[nod].top().second])))
smallboi[nod].pop();
if(!smallboi[nod].empty())
rezQ.first = min(rezQ.first, smallboi[nod].top().first);
if(!bigboi[nod].empty())
rezQ.second = max(rezQ.second, bigboi[nod].top().first);
if(l < r) {
int mid = (l + r) / 2;
rezQ = add(queryAint(poz, l, mid, 2 * nod),
add(queryAint(poz, mid + 1, r, 2 * nod + 1),
rezQ));
}
return rezQ;
}
int getId(int x) {
int st = 0, dr = transf.size();
while(dr - st > 1) {
int mid = (st + dr) / 2;
if(transf[mid].first <= x)
st = mid;
else
dr = mid;
}
return transf[st].second;
}
void updateRange(pair<int, int> st, pair<int, int> dr) {
pair<int, int> nullpair(-1, -1);
if(st != nullpair && dr != nullpair) {
int mid = (st.first + dr.first) / 2;
int pst = getId(st.first), pdr = getId(dr.first), pmid = getId(mid);
updateAint(pst + 1, pmid, st, 1, transf.size(), 1);
updateAint(pmid + 1, pdr - 1, dr, 1, transf.size(), 1);
rangeR[st.second] = pmid;
rangeL[dr.second] = pmid + 1;
} else if(st != nullpair) {
int pst = getId(st.first);
updateAint(pst + 1, transf.size(), st, 1, transf.size(), 1);
rangeR[st.second] = transf.size();
} else if(dr != nullpair) {
int pdr = getId(dr.first);
updateAint(1, pdr - 1, dr, 1, transf.size(), 1);
rangeL[dr.second] = 1;
}
}
void addStore(int l, int t, int i) {
set<pair<int, int> >::iterator it;
pair<int, int> st, dr, val(l, i);
it = typestore[t].lower_bound(val);
if(it == typestore[t].end()) dr = make_pair(-1, -1);
else dr = *it;
if(it == typestore[t].begin()) st = make_pair(-1, -1);
else {
it--;
st = *it;
}
typestore[t].insert(val);
updateRange(st, val);
updateRange(val, dr);
activeStore[i] = true;
}
void eraseStore(int l, int t, int i) {
set<pair<int, int> >::iterator it;
pair<int, int> st, dr, val(l, i);
it = typestore[t].find(val);
it++;
if(it == typestore[t].end()) dr = make_pair(-1, -1);
else dr = *it;
it--;
if(it == typestore[t].begin()) st = make_pair(-1, -1);
else {
it--;
st = *it;
}
typestore[t].erase(val);
updateRange(st, dr);
activeStore[i] = false;
}
int main() {
#ifdef HOME
FILE *fin = fopen("input.in", "r");
FILE *fout = fopen("output.out", "w");
#else
FILE *fin = stdin;
FILE *fout = stdout;
#endif
int n, k, q;
fscanf(fin, "%d%d%d", &n, &k, &q);
for(int i = 0; i < n; ++i) {
fscanf(fin, "%d%d%d%d", &v[i].x, &v[i].t, &v[i].a, &v[i].b);
auxx[i] = v[i].x;
event[2 * i] = {v[i].a, v[i].x, v[i].t, i, false};
event[2 * i + 1] = {v[i].b + 1, v[i].x, v[i].t, i, true};
}
for(int i = 0; i < q; ++i) {
fscanf(fin, "%d%d", &query[i].l, &query[i].y);
query[i].rez = &rez[i];
auxx[n + i] = query[i].l;
}
normalize(n + q);
sort(event, event + 2 * n, cmp1);
sort(query, query + q, cmp3);
int lastup = 0, nTypes = 0;
for(int i = 0; i < q; ++i) {
while(lastup < 2 * n && event[lastup].time <= query[i].y) {
if(event[lastup].scoate) {
eraseStore(event[lastup].x, event[lastup].t, event[lastup].id);
if(typestore[event[lastup].t].size() == 0)
--nTypes;
} else {
addStore(event[lastup].x, event[lastup].t, event[lastup].id);
if(typestore[event[lastup].t].size() == 1)
++nTypes;
}
++lastup;
}
if(nTypes < k)
*query[i].rez = -1;
else {
pair<int, int> rezQ = queryAint(getId(query[i].l), 1, transf.size(), 1);
*query[i].rez = max(rezQ.second - query[i].l,
max(query[i].l - rezQ.first, 0));
}
}
for(int i = 0; i < q; ++i)
fprintf(fout, "%d\n", rez[i]);
#ifdef HOME
fclose(fin);
fclose(fout);
#endif
return 0;
}
Compilation message
new_home.cpp: In function 'int main()':
new_home.cpp:189:8: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d%d", &n, &k, &q);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:192:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d%d%d", &v[i].x, &v[i].t, &v[i].a, &v[i].b);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:199:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf(fin, "%d%d", &query[i].l, &query[i].y);
~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
164904 KB |
Output is correct |
2 |
Correct |
127 ms |
164904 KB |
Output is correct |
3 |
Correct |
124 ms |
164904 KB |
Output is correct |
4 |
Correct |
124 ms |
164904 KB |
Output is correct |
5 |
Correct |
129 ms |
164976 KB |
Output is correct |
6 |
Correct |
134 ms |
165296 KB |
Output is correct |
7 |
Correct |
149 ms |
165296 KB |
Output is correct |
8 |
Correct |
150 ms |
165296 KB |
Output is correct |
9 |
Correct |
148 ms |
165308 KB |
Output is correct |
10 |
Correct |
152 ms |
165356 KB |
Output is correct |
11 |
Correct |
148 ms |
165356 KB |
Output is correct |
12 |
Correct |
138 ms |
165512 KB |
Output is correct |
13 |
Correct |
149 ms |
165512 KB |
Output is correct |
14 |
Correct |
147 ms |
165512 KB |
Output is correct |
15 |
Correct |
148 ms |
165512 KB |
Output is correct |
16 |
Correct |
148 ms |
165512 KB |
Output is correct |
17 |
Correct |
149 ms |
165512 KB |
Output is correct |
18 |
Correct |
152 ms |
165520 KB |
Output is correct |
19 |
Correct |
153 ms |
165520 KB |
Output is correct |
20 |
Correct |
151 ms |
165520 KB |
Output is correct |
21 |
Correct |
149 ms |
165520 KB |
Output is correct |
22 |
Correct |
148 ms |
165668 KB |
Output is correct |
23 |
Correct |
149 ms |
165668 KB |
Output is correct |
24 |
Correct |
156 ms |
165668 KB |
Output is correct |
25 |
Correct |
153 ms |
165672 KB |
Output is correct |
26 |
Correct |
130 ms |
165676 KB |
Output is correct |
27 |
Correct |
198 ms |
165828 KB |
Output is correct |
28 |
Correct |
149 ms |
165828 KB |
Output is correct |
29 |
Correct |
145 ms |
165828 KB |
Output is correct |
30 |
Correct |
150 ms |
165828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
164904 KB |
Output is correct |
2 |
Correct |
127 ms |
164904 KB |
Output is correct |
3 |
Correct |
124 ms |
164904 KB |
Output is correct |
4 |
Correct |
124 ms |
164904 KB |
Output is correct |
5 |
Correct |
129 ms |
164976 KB |
Output is correct |
6 |
Correct |
134 ms |
165296 KB |
Output is correct |
7 |
Correct |
149 ms |
165296 KB |
Output is correct |
8 |
Correct |
150 ms |
165296 KB |
Output is correct |
9 |
Correct |
148 ms |
165308 KB |
Output is correct |
10 |
Correct |
152 ms |
165356 KB |
Output is correct |
11 |
Correct |
148 ms |
165356 KB |
Output is correct |
12 |
Correct |
138 ms |
165512 KB |
Output is correct |
13 |
Correct |
149 ms |
165512 KB |
Output is correct |
14 |
Correct |
147 ms |
165512 KB |
Output is correct |
15 |
Correct |
148 ms |
165512 KB |
Output is correct |
16 |
Correct |
148 ms |
165512 KB |
Output is correct |
17 |
Correct |
149 ms |
165512 KB |
Output is correct |
18 |
Correct |
152 ms |
165520 KB |
Output is correct |
19 |
Correct |
153 ms |
165520 KB |
Output is correct |
20 |
Correct |
151 ms |
165520 KB |
Output is correct |
21 |
Correct |
149 ms |
165520 KB |
Output is correct |
22 |
Correct |
148 ms |
165668 KB |
Output is correct |
23 |
Correct |
149 ms |
165668 KB |
Output is correct |
24 |
Correct |
156 ms |
165668 KB |
Output is correct |
25 |
Correct |
153 ms |
165672 KB |
Output is correct |
26 |
Correct |
130 ms |
165676 KB |
Output is correct |
27 |
Correct |
198 ms |
165828 KB |
Output is correct |
28 |
Correct |
149 ms |
165828 KB |
Output is correct |
29 |
Correct |
145 ms |
165828 KB |
Output is correct |
30 |
Correct |
150 ms |
165828 KB |
Output is correct |
31 |
Correct |
2272 ms |
252916 KB |
Output is correct |
32 |
Correct |
256 ms |
252916 KB |
Output is correct |
33 |
Correct |
1693 ms |
252916 KB |
Output is correct |
34 |
Correct |
2387 ms |
255832 KB |
Output is correct |
35 |
Correct |
2268 ms |
255832 KB |
Output is correct |
36 |
Correct |
1422 ms |
255832 KB |
Output is correct |
37 |
Correct |
1134 ms |
255832 KB |
Output is correct |
38 |
Correct |
893 ms |
255832 KB |
Output is correct |
39 |
Correct |
721 ms |
255832 KB |
Output is correct |
40 |
Correct |
680 ms |
255832 KB |
Output is correct |
41 |
Correct |
1415 ms |
255832 KB |
Output is correct |
42 |
Correct |
1462 ms |
255832 KB |
Output is correct |
43 |
Correct |
225 ms |
255832 KB |
Output is correct |
44 |
Correct |
1641 ms |
255832 KB |
Output is correct |
45 |
Correct |
1527 ms |
255832 KB |
Output is correct |
46 |
Correct |
1236 ms |
255832 KB |
Output is correct |
47 |
Correct |
550 ms |
255832 KB |
Output is correct |
48 |
Correct |
537 ms |
255832 KB |
Output is correct |
49 |
Correct |
683 ms |
255832 KB |
Output is correct |
50 |
Correct |
746 ms |
255832 KB |
Output is correct |
51 |
Correct |
710 ms |
255832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5072 ms |
422136 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5111 ms |
422312 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
164904 KB |
Output is correct |
2 |
Correct |
127 ms |
164904 KB |
Output is correct |
3 |
Correct |
124 ms |
164904 KB |
Output is correct |
4 |
Correct |
124 ms |
164904 KB |
Output is correct |
5 |
Correct |
129 ms |
164976 KB |
Output is correct |
6 |
Correct |
134 ms |
165296 KB |
Output is correct |
7 |
Correct |
149 ms |
165296 KB |
Output is correct |
8 |
Correct |
150 ms |
165296 KB |
Output is correct |
9 |
Correct |
148 ms |
165308 KB |
Output is correct |
10 |
Correct |
152 ms |
165356 KB |
Output is correct |
11 |
Correct |
148 ms |
165356 KB |
Output is correct |
12 |
Correct |
138 ms |
165512 KB |
Output is correct |
13 |
Correct |
149 ms |
165512 KB |
Output is correct |
14 |
Correct |
147 ms |
165512 KB |
Output is correct |
15 |
Correct |
148 ms |
165512 KB |
Output is correct |
16 |
Correct |
148 ms |
165512 KB |
Output is correct |
17 |
Correct |
149 ms |
165512 KB |
Output is correct |
18 |
Correct |
152 ms |
165520 KB |
Output is correct |
19 |
Correct |
153 ms |
165520 KB |
Output is correct |
20 |
Correct |
151 ms |
165520 KB |
Output is correct |
21 |
Correct |
149 ms |
165520 KB |
Output is correct |
22 |
Correct |
148 ms |
165668 KB |
Output is correct |
23 |
Correct |
149 ms |
165668 KB |
Output is correct |
24 |
Correct |
156 ms |
165668 KB |
Output is correct |
25 |
Correct |
153 ms |
165672 KB |
Output is correct |
26 |
Correct |
130 ms |
165676 KB |
Output is correct |
27 |
Correct |
198 ms |
165828 KB |
Output is correct |
28 |
Correct |
149 ms |
165828 KB |
Output is correct |
29 |
Correct |
145 ms |
165828 KB |
Output is correct |
30 |
Correct |
150 ms |
165828 KB |
Output is correct |
31 |
Correct |
2272 ms |
252916 KB |
Output is correct |
32 |
Correct |
256 ms |
252916 KB |
Output is correct |
33 |
Correct |
1693 ms |
252916 KB |
Output is correct |
34 |
Correct |
2387 ms |
255832 KB |
Output is correct |
35 |
Correct |
2268 ms |
255832 KB |
Output is correct |
36 |
Correct |
1422 ms |
255832 KB |
Output is correct |
37 |
Correct |
1134 ms |
255832 KB |
Output is correct |
38 |
Correct |
893 ms |
255832 KB |
Output is correct |
39 |
Correct |
721 ms |
255832 KB |
Output is correct |
40 |
Correct |
680 ms |
255832 KB |
Output is correct |
41 |
Correct |
1415 ms |
255832 KB |
Output is correct |
42 |
Correct |
1462 ms |
255832 KB |
Output is correct |
43 |
Correct |
225 ms |
255832 KB |
Output is correct |
44 |
Correct |
1641 ms |
255832 KB |
Output is correct |
45 |
Correct |
1527 ms |
255832 KB |
Output is correct |
46 |
Correct |
1236 ms |
255832 KB |
Output is correct |
47 |
Correct |
550 ms |
255832 KB |
Output is correct |
48 |
Correct |
537 ms |
255832 KB |
Output is correct |
49 |
Correct |
683 ms |
255832 KB |
Output is correct |
50 |
Correct |
746 ms |
255832 KB |
Output is correct |
51 |
Correct |
710 ms |
255832 KB |
Output is correct |
52 |
Correct |
708 ms |
422312 KB |
Output is correct |
53 |
Correct |
704 ms |
422312 KB |
Output is correct |
54 |
Correct |
1231 ms |
422312 KB |
Output is correct |
55 |
Correct |
1170 ms |
422312 KB |
Output is correct |
56 |
Correct |
1224 ms |
422312 KB |
Output is correct |
57 |
Correct |
1572 ms |
422312 KB |
Output is correct |
58 |
Correct |
1314 ms |
422312 KB |
Output is correct |
59 |
Correct |
1116 ms |
422312 KB |
Output is correct |
60 |
Correct |
1508 ms |
422312 KB |
Output is correct |
61 |
Correct |
218 ms |
422312 KB |
Output is correct |
62 |
Correct |
717 ms |
422312 KB |
Output is correct |
63 |
Correct |
1151 ms |
422312 KB |
Output is correct |
64 |
Correct |
1346 ms |
422312 KB |
Output is correct |
65 |
Correct |
1656 ms |
422312 KB |
Output is correct |
66 |
Correct |
1769 ms |
422312 KB |
Output is correct |
67 |
Correct |
336 ms |
422312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
164904 KB |
Output is correct |
2 |
Correct |
127 ms |
164904 KB |
Output is correct |
3 |
Correct |
124 ms |
164904 KB |
Output is correct |
4 |
Correct |
124 ms |
164904 KB |
Output is correct |
5 |
Correct |
129 ms |
164976 KB |
Output is correct |
6 |
Correct |
134 ms |
165296 KB |
Output is correct |
7 |
Correct |
149 ms |
165296 KB |
Output is correct |
8 |
Correct |
150 ms |
165296 KB |
Output is correct |
9 |
Correct |
148 ms |
165308 KB |
Output is correct |
10 |
Correct |
152 ms |
165356 KB |
Output is correct |
11 |
Correct |
148 ms |
165356 KB |
Output is correct |
12 |
Correct |
138 ms |
165512 KB |
Output is correct |
13 |
Correct |
149 ms |
165512 KB |
Output is correct |
14 |
Correct |
147 ms |
165512 KB |
Output is correct |
15 |
Correct |
148 ms |
165512 KB |
Output is correct |
16 |
Correct |
148 ms |
165512 KB |
Output is correct |
17 |
Correct |
149 ms |
165512 KB |
Output is correct |
18 |
Correct |
152 ms |
165520 KB |
Output is correct |
19 |
Correct |
153 ms |
165520 KB |
Output is correct |
20 |
Correct |
151 ms |
165520 KB |
Output is correct |
21 |
Correct |
149 ms |
165520 KB |
Output is correct |
22 |
Correct |
148 ms |
165668 KB |
Output is correct |
23 |
Correct |
149 ms |
165668 KB |
Output is correct |
24 |
Correct |
156 ms |
165668 KB |
Output is correct |
25 |
Correct |
153 ms |
165672 KB |
Output is correct |
26 |
Correct |
130 ms |
165676 KB |
Output is correct |
27 |
Correct |
198 ms |
165828 KB |
Output is correct |
28 |
Correct |
149 ms |
165828 KB |
Output is correct |
29 |
Correct |
145 ms |
165828 KB |
Output is correct |
30 |
Correct |
150 ms |
165828 KB |
Output is correct |
31 |
Correct |
2272 ms |
252916 KB |
Output is correct |
32 |
Correct |
256 ms |
252916 KB |
Output is correct |
33 |
Correct |
1693 ms |
252916 KB |
Output is correct |
34 |
Correct |
2387 ms |
255832 KB |
Output is correct |
35 |
Correct |
2268 ms |
255832 KB |
Output is correct |
36 |
Correct |
1422 ms |
255832 KB |
Output is correct |
37 |
Correct |
1134 ms |
255832 KB |
Output is correct |
38 |
Correct |
893 ms |
255832 KB |
Output is correct |
39 |
Correct |
721 ms |
255832 KB |
Output is correct |
40 |
Correct |
680 ms |
255832 KB |
Output is correct |
41 |
Correct |
1415 ms |
255832 KB |
Output is correct |
42 |
Correct |
1462 ms |
255832 KB |
Output is correct |
43 |
Correct |
225 ms |
255832 KB |
Output is correct |
44 |
Correct |
1641 ms |
255832 KB |
Output is correct |
45 |
Correct |
1527 ms |
255832 KB |
Output is correct |
46 |
Correct |
1236 ms |
255832 KB |
Output is correct |
47 |
Correct |
550 ms |
255832 KB |
Output is correct |
48 |
Correct |
537 ms |
255832 KB |
Output is correct |
49 |
Correct |
683 ms |
255832 KB |
Output is correct |
50 |
Correct |
746 ms |
255832 KB |
Output is correct |
51 |
Correct |
710 ms |
255832 KB |
Output is correct |
52 |
Execution timed out |
5072 ms |
422136 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |