This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://oj.uz/problem/view/IZhO18_segments
#include<bits/stdc++.h>
const int N = 2e5 + 5;
const int MAGIC = 1501;
using namespace std;
struct segment{
int l, r, id;
} mv[MAGIC+5];
int n, t, L[N], R[N], ans, cnt, cur, sizmv;
int l_sort[N], r_sort[N], l_nosort[N], r_nosort[N], p[N], num;
bool ck[N];
int bsearch(int l, int r, int k){
while (l != r){
int mid = (l + r) >> 1;
if (r_nosort[mid] - l_nosort[mid] + 1 >= k) r = mid;
else l = mid + 1;
}
if (r_nosort[l] - l_nosort[l] + 1 < k) return l + 1;
return l;
}
void get_L(int pos, int k){
int Max = (pos / MAGIC + 1) * MAGIC;
for (int i = pos; i < min(Max, cnt); i++) if (r_nosort[i] < k) ans--;
for (int i = Max; i < cnt; i += MAGIC){
int x = i, y = min(i + MAGIC, cnt);
int val = lower_bound(r_sort+x, r_sort+y, k) - r_sort - x;
ans -= val;
}
}
void get_R(int pos, int k){
int Max = (pos / MAGIC + 1) * MAGIC;
for (int i = pos; i < min(Max, cnt); i++) if (l_nosort[i] > k) ans--;
for (int i = Max; i < cnt; i += MAGIC){
int x = i, y = min(i + MAGIC, cnt);
int val = y - (upper_bound(l_sort+x, l_sort+y, k) - l_sort);
ans -= val;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> t;
while (n--){
if (sizmv == MAGIC){
cnt = 0; sizmv = 0;
for (int i = 1; i < N; i++) if (ck[i]) p[cnt++] = i;
sort(p, p+cnt, [](int x, int y){return (R[x] - L[x]) < (R[y] - L[y]);});
for (int i = 0; i < cnt; i++) l_sort[i] = l_nosort[i] = L[p[i]];
for (int i = 0; i < cnt; i++) r_sort[i] = r_nosort[i] = R[p[i]];
for (int i = 0; i < cnt; i += MAGIC){
int x = i, y = min(i + MAGIC, cnt);
sort(l_sort+x, l_sort+y);
sort(r_sort+x, r_sort+y);
}
}
int type, l, r, id, k;
cin >> type;
if (type == 1){
num++; id = ++cur;
cin >> l >> r;
l ^= (t * ans); r ^= (t * ans); if (r < l) swap(l, r);
ck[id] = true;
L[id] = l; R[id] = r;
mv[sizmv++] = {l, r, 1};
}
if (type == 2){
num--;
cin >> id;
ck[id] = false;
mv[sizmv++] = {L[id], R[id], -1};
}
if (type == 3){
cin >> l >> r >> k;
l ^= (t * ans); r ^= (t * ans); if (r < l) swap(l, r);
if (r - l + 1 < k){
ans = 0; cout << ans << "\n";
continue;
}
ans = num;
if (cnt > 0){
int pos = bsearch(0, cnt-1, k);
ans -= pos;
get_L(pos, l + k - 1);
get_R(pos, r - k + 1);
}
for (int i = 0; i < sizmv; i++){
int x = max(mv[i].l, l), y = min(mv[i].r, r);
if (y - x + 1 < k) ans -= mv[i].id;
}
cout << ans << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |