# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
376123 | ijxjdjd | Segments (IZhO18_segments) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using namespace std;
using ll = long long;
const int MAXN = (int)(2e5) + 5;
const int K = 300;
int curAdd = 0;
int sz = 0;
int e[MAXN][2];
int mnSz[MAXN];
int mxL[MAXN];
bool del[MAXN];
auto byL = [&] (const int& lhs, const int& rhs) {
return e[lhs][0] < e[rhs][0];
};
auto byR = [&] (const int& lhs, const int& rhs) {
return e[lhs][1] < e[rhs][1];
};
auto bySz = [&] (const int& lhs, const int& rhs) {
return (e[lhs][1] - e[lhs][0]) < (e[rhs][1] - e[rhs][0]);
};
vector<int> L;
vector<int> Sz;
vector<int> curdel;
void rebuild() {
curdel.clear();
L.clear();
Sz.clear();
curAdd = 0;
for (int i = 0; i < sz; i++) {
if (!del[i]) {
L.push_back(i);
Sz.push_back(i);
}
}
sort(all(Sz), bySz);
for (int i = 0; i < Sz.size(); i+=K) {
mnSz[i] = e[Sz[i]][1] - e[Sz[i]][0] + 1;
if (i + K >= Sz.size()) {
sort(Sz.begin()+i, Sz.end(), byL);
}
else {
sort(Sz.begin()+i, Sz.begin()+i+K, byL);
}
}
sort(all(L), byL);
for (int i = 0; i < L.size(); i+=K) {
if (i + K >= L.size()) {
mxL[i] = e[*L.rbegin()][0];
sort(L.begin()+i, L.end(), byR);
}
else {
mxL[i] = e[*(L.begin()+i+K-1)][0];
sort(L.begin()+i, L.begin()+i+K, byR);
}
}
}
void add(int l, int r) {
e[sz][0] = l;
e[sz][1] = r;
sz++;
curAdd++;
}
void rem(int i) {
del[i] = true;
curdel.push_back(i);
}
bool good(int l, int r, int id, int s) {
int l2 = max(e[id][0], l);
int r2 = min(e[id][1], r);
if (r2 - l2 + 1 >= s) {
return true;
}
return false;
}
int getSz(int l1, int l2, int s) {
int cnt = 0;
for (int j = (sz(Sz)-1)/K * K; j >= 0; j-=K) {
if (mnSz[j] < s) {
for (int i = 0; j + i < Sz.size() && i < K; i++) {
if (e[Sz[j+i]][1] - e[Sz[j+1]][0] + 1 >= s && e[Sz[j+i]][0] <= l1 && e[Sz[j+i]][0] <= l2) {
cnt++;
}
}
return cnt;
}
else {
int low = j;
int high = min(sz(Sz)-1, j+K-1);
while (low < high) {
int mid = (low + high)/2;
if (l1 <= e[Sz[mid]][0]) {
high = mid;
}
else {
low = mid+1;
}
}
int bot = high;
low = j;
high = min(sz(Sz)-1, j+K-1);
while (low < high) {
int mid = (low + high+1)/2;
if (e[Sz[mid]][0] <= l2) {
low = mid;
}
else {
high = mid-1;
}
}
cnt += (low - bot + 1);
}
}
return cnt;
}
int getL(int lb, int rb) {
int cnt = 0;
for (int j = 0; j < sz(L); j += K) {
if (mxL[j] >= lb) {
for (int i = 0; j + i < L.size() && i < K; i++) {
if (e[L[j+i]][0] < lb && e[L[j+i]][0] >= rb) {
cnt++;
}
}
return cnt;
}
else {
int low = j;
int high = min(j+K, sz(L)-1);
int h = min(j+K, sz(L)-1);
while (low < high) {
int mid = (low + high)/2;
if (e[L[mid]][1] >= rb) {
low = mid;
}
else {
high = mid-1;
}
}
cnt += (h - low + 1);
}
}
return cnt;
}
int count(int l, int r, int k) {
if (r - l + 1 < k) {
return 0;
}
int temp = getSz(l, r-k+1, k) + getL(l, l+K-1);
for (int ed : curdel) {
if (good(l, r, ed, k)) {
temp--;
}
}
for (int i = 1; i <= curAdd; i++) {
if (good(l, r, sz-i, k)) {
temp++;
}
}
return temp;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int N, t;
cin >> N >> t;
int lst = 0;
FR(iter, N) {
if (iter%K == 0) {
rebuild();
}
int id;
cin >> id;
if (id == 1) {
int l, r;
cin >> l >> r;
l = l^(lst*t);
r = r^(lst*t);
if (l > r) {
swap(l, r);
}
add(l, r);
}
else if (id == 2) {
int i;
cin >> i;
i--;
rem(i);
}
else {
int l, r, k;
cin >> l >> r >> k;
l = l^(lst*t);
r = r^(lst*t);
if (l > r) {
swap(l, r);
}
lst = count(l, r, k);
cout << lst << '\n';
}
}
return 0;
}