#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 15e5;
int n, m, q;
pii interval[MAXN];
class group {
public:
set<int> vals;
int pos;
group(int a, int b) {
vals.insert(a);
pos = b;
}
int size() {
return vals.size();
}
void insert(int a) {
vals.insert(a);
}
void erase(int a) {
vals.erase(a);
}
void clear() {
vals.clear();
}
};
vector<group> groups;
int lgroup[MAXN];
int rgroup[MAXN];
int merge(int a, int b, int garr[]) {
if (groups[a].size() < groups[b].size()) swap(a, b);
for (int v: groups[b].vals) {
groups[a].insert(v);
garr[v] = a;
}
groups[b].clear();
return a;
}
class stree {
public:
priority_queue<pii, vector<pii>, greater<pii>> lvals;
priority_queue<pii> rvals;
int lp, rp;
int mid;
stree *l = nullptr;
stree *r = nullptr;
stree(int lp, int rp) : lp(lp), rp(rp) {
mid = (lp+rp)/2;
}
void ins(int i) {
if (interval[i].second < mid) {
if (!l) l = new stree(lp, mid-1);
l->ins(i);
}
else if (interval[i].first > mid) {
if (!r) r = new stree(mid+1, rp);
r->ins(i);
}
else {
groups.push_back(group(i, interval[i].first));
groups.push_back(group(i, interval[i].second));
lgroup[i] = groups.size()-2;
rgroup[i] = groups.size()-1;
lvals.push(pii(interval[i].first, lgroup[i]));
rvals.push(pii(interval[i].second, rgroup[i])); // could merge but not necessary
}
}
void lalign(int p) { // move l to this pos
if (p > mid) {
if (r) r->lalign(p);
while (!rvals.empty() && rvals.top().first >= p) {
pii tp = rvals.top();
rvals.pop();
// if (tp.second == 3) {
// cerr << groups[3].pos << '\n';
// for (int v: groups[3].vals) cerr << v << ' ';
// cerr << '\n';
// }
for (auto it = groups[tp.second].vals.begin(); it != groups[tp.second].vals.end(); it++) {
int v = *it;
interval[v].first = p;
interval[v].second = groups[rgroup[v]].pos;
groups[lgroup[v]].erase(v);
if (!r) r = new stree(mid+1, rp);
r->ins(v);
}
groups[tp.second].clear();
}
}
else {
if (p < mid && l) l->lalign(p);
int merged = -1;
while (!lvals.empty() && lvals.top().first <= p) {
pii tp = lvals.top();
lvals.pop();
if (merged == -1) merged = tp.second;
else {
merged = merge(tp.second, merged, lgroup);
}
}
if (merged != -1) {
lvals.push(pii(p, merged));
groups[merged].pos = p;
}
}
}
void ralign(int p) { // move r to this pos
if (p < mid) {
if (l) l->ralign(p);
while (!lvals.empty() && lvals.top().first <= p) {
pii tp = lvals.top();
lvals.pop();
for (auto it = groups[tp.second].vals.begin(); it != groups[tp.second].vals.end(); it++) {
int v = *it;
interval[v].second = p;
interval[v].first = groups[lgroup[v]].pos;
groups[rgroup[v]].erase(v);
if (!l) l = new stree(lp, mid-1);
l->ins(v);
}
groups[tp.second].clear();
}
}
else {
if (p > mid && r) r->ralign(p);
int merged = -1;
while (!rvals.empty() && rvals.top().first >= p) {
pii tp = rvals.top();
rvals.pop();
if (merged == -1) merged = tp.second;
else {
merged = merge(tp.second, merged, rgroup);
}
}
if (merged != -1) {
rvals.push(pii(p, merged));
groups[merged].pos = p;
}
}
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m >> q;
stree *tree = new stree(0, n+1);
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
interval[i] = pii(x, n-y);
tree->ins(i);
}
for (int i = 0; i < q; i++) {
int t; cin >> t;
if (t == 1) {
int p;
cin >> p; p--;
cout << groups[lgroup[p]].pos << ' ' << (n-groups[rgroup[p]].pos) << '\n';
}
if (t == 2) {
int l; cin >> l;
tree->lalign(n-l);
}
if (t == 3) {
int l; cin >> l;
tree->ralign(l);
}
if (t == 4) {
int x, y; cin >> x >> y;
interval[m] = pii(x, n-y);
tree->ins(m++);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2732 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1432 KB |
Output is correct |
4 |
Correct |
8 ms |
2732 KB |
Output is correct |
5 |
Correct |
12 ms |
2736 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3310 ms |
739044 KB |
Output is correct |
2 |
Correct |
3332 ms |
549064 KB |
Output is correct |
3 |
Correct |
3354 ms |
548096 KB |
Output is correct |
4 |
Correct |
1613 ms |
407112 KB |
Output is correct |
5 |
Correct |
8249 ms |
1155188 KB |
Output is correct |
6 |
Correct |
5422 ms |
695932 KB |
Output is correct |
7 |
Correct |
3426 ms |
527300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2797 ms |
593280 KB |
Output is correct |
2 |
Correct |
2808 ms |
617368 KB |
Output is correct |
3 |
Correct |
1016 ms |
275692 KB |
Output is correct |
4 |
Correct |
3864 ms |
1138548 KB |
Output is correct |
5 |
Correct |
2694 ms |
604356 KB |
Output is correct |
6 |
Correct |
2290 ms |
563160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2797 ms |
593280 KB |
Output is correct |
2 |
Correct |
2808 ms |
617368 KB |
Output is correct |
3 |
Correct |
1016 ms |
275692 KB |
Output is correct |
4 |
Correct |
3864 ms |
1138548 KB |
Output is correct |
5 |
Correct |
2694 ms |
604356 KB |
Output is correct |
6 |
Correct |
2290 ms |
563160 KB |
Output is correct |
7 |
Correct |
3523 ms |
480564 KB |
Output is correct |
8 |
Correct |
3633 ms |
475260 KB |
Output is correct |
9 |
Correct |
3356 ms |
478728 KB |
Output is correct |
10 |
Correct |
1763 ms |
407900 KB |
Output is correct |
11 |
Correct |
5489 ms |
622420 KB |
Output is correct |
12 |
Correct |
5265 ms |
589200 KB |
Output is correct |
13 |
Correct |
4514 ms |
575120 KB |
Output is correct |
14 |
Correct |
98 ms |
4172 KB |
Output is correct |
15 |
Correct |
791 ms |
145872 KB |
Output is correct |
16 |
Correct |
2914 ms |
445764 KB |
Output is correct |
17 |
Correct |
2866 ms |
455520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2732 KB |
Output is correct |
2 |
Correct |
4 ms |
1116 KB |
Output is correct |
3 |
Correct |
3 ms |
1432 KB |
Output is correct |
4 |
Correct |
8 ms |
2732 KB |
Output is correct |
5 |
Correct |
12 ms |
2736 KB |
Output is correct |
6 |
Correct |
4 ms |
852 KB |
Output is correct |
7 |
Correct |
3310 ms |
739044 KB |
Output is correct |
8 |
Correct |
3332 ms |
549064 KB |
Output is correct |
9 |
Correct |
3354 ms |
548096 KB |
Output is correct |
10 |
Correct |
1613 ms |
407112 KB |
Output is correct |
11 |
Correct |
8249 ms |
1155188 KB |
Output is correct |
12 |
Correct |
5422 ms |
695932 KB |
Output is correct |
13 |
Correct |
3426 ms |
527300 KB |
Output is correct |
14 |
Correct |
2797 ms |
593280 KB |
Output is correct |
15 |
Correct |
2808 ms |
617368 KB |
Output is correct |
16 |
Correct |
1016 ms |
275692 KB |
Output is correct |
17 |
Correct |
3864 ms |
1138548 KB |
Output is correct |
18 |
Correct |
2694 ms |
604356 KB |
Output is correct |
19 |
Correct |
2290 ms |
563160 KB |
Output is correct |
20 |
Correct |
3523 ms |
480564 KB |
Output is correct |
21 |
Correct |
3633 ms |
475260 KB |
Output is correct |
22 |
Correct |
3356 ms |
478728 KB |
Output is correct |
23 |
Correct |
1763 ms |
407900 KB |
Output is correct |
24 |
Correct |
5489 ms |
622420 KB |
Output is correct |
25 |
Correct |
5265 ms |
589200 KB |
Output is correct |
26 |
Correct |
4514 ms |
575120 KB |
Output is correct |
27 |
Correct |
98 ms |
4172 KB |
Output is correct |
28 |
Correct |
791 ms |
145872 KB |
Output is correct |
29 |
Correct |
2914 ms |
445764 KB |
Output is correct |
30 |
Correct |
2866 ms |
455520 KB |
Output is correct |
31 |
Correct |
2770 ms |
538132 KB |
Output is correct |
32 |
Correct |
3562 ms |
743948 KB |
Output is correct |
33 |
Correct |
2074 ms |
726228 KB |
Output is correct |
34 |
Correct |
3964 ms |
755360 KB |
Output is correct |
35 |
Correct |
4027 ms |
753540 KB |
Output is correct |
36 |
Correct |
1732 ms |
382924 KB |
Output is correct |
37 |
Correct |
8427 ms |
1153692 KB |
Output is correct |
38 |
Correct |
5700 ms |
1088044 KB |
Output is correct |
39 |
Correct |
4539 ms |
572256 KB |
Output is correct |
40 |
Correct |
3053 ms |
496336 KB |
Output is correct |