# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
100264 | choikiwon | Segments (IZhO18_segments) | C++17 | 4698 ms | 10980 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>
using namespace std;
typedef pair<int, int> pii;
const int MN = 200010;
const int SQ = 2000;
int Q, T, N, n;
pii seg[MN], tmp1[MN], tmp2[MN];
int chk[MN], mn[MN], mx[MN];
vector<int> add, del;
bool cmp(pii a, pii b) {
return a.second < b.second;
}
void relax() {
if(add.size() > SQ || del.size() > SQ) {
for(int i = 0; i < add.size(); i++) chk[ add[i] ] = 1;
for(int i = 0; i < del.size(); i++) chk[ del[i] ] = 0;
add.clear();
del.clear();
n = 0;
for(int i = 0; i < N; i++) if(chk[i]) {
tmp1[n] = pii(seg[i].second, seg[i].first);
tmp2[n] = pii(seg[i].second - seg[i].first, seg[i].first);
n++;
}
sort(tmp1, tmp1 + n, cmp);
sort(tmp2, tmp2 + n, cmp);
for(int i = 0; i < n; i += SQ) {
int l = i;
int r = min(n, i + SQ) - 1;
mn[i] = tmp1[l].second;
mx[i] = tmp1[r].second;
sort(tmp1 + l, tmp1 + r + 1);
sort(tmp2 + l, tmp2 + r + 1);
}
}
}
int main() {
scanf("%d %d", &Q, &T);
int ans = 0;
while(Q--) {
relax();
int t; scanf("%d", &t);
if(t == 1) {
int a, b; scanf("%d %d", &a, &b);
if(T) {
a ^= ans;
b ^= ans;
}
if(a > b) swap(a, b);
//cout << "add : " << a << ' ' << b << endl;
add.push_back(N);
seg[N++] = pii(a, b);
}
if(t == 2) {
int id; scanf("%d", &id);
id--;
del.push_back(id);
}
if(t == 3) {
int a, b, k; scanf("%d %d %d", &a, &b, &k);
k--;
if(T) {
a ^= ans;
b ^= ans;
}
if(a > b) swap(a, b);
//cout << "query : " << a << ' ' << b << ' ' << k << endl;
if(b - a < k) {
ans = 0;
printf("%d\n", ans);
continue;
}
ans = 0;
for(int i = 0; i < add.size(); i++) if(min(b, seg[ add[i] ].second) - max(a, seg[ add[i] ].first) >= k) ans++;
for(int i = 0; i < del.size(); i++) if(min(b, seg[ del[i] ].second) - max(a, seg[ del[i] ].first) >= k) ans--;
if(k == -1) {
ans += n;
printf("%d\n", ans);
continue;
}
for(int i = 0; i < n; i += SQ) {
int l = i;
int r = min(n, i + SQ) - 1;
if(mx[i] < a) {
int x = lower_bound(tmp1 + l, tmp1 + r + 1, pii(a + k, -1)) - tmp1;
ans += r + 1 - x;
}
else {
for(int j = l; j <= r; j++) {
if(tmp1[j].second < a && tmp1[j].first >= a + k) ans++;
}
break;
}
}
for(int i = 0; i < n; i += SQ) {
int l = i;
int r = min(n, i + SQ) - 1;
if(b - k < mn[i] || mx[i] < a) continue;
if(a <= mn[i] && mx[i] <= b - k) {
int x = lower_bound(tmp2 + l, tmp2 + r + 1, pii(k, -1)) - tmp2;
ans += r + 1 - x;
}
else {
for(int j = l; j <= r; j++) {
if(a <= tmp2[j].second && tmp2[j].second <= b - k && tmp2[j].first >= k) ans++;
}
}
}
printf("%d\n", ans);
}
}
}
Compilation message (stderr)
# | 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... |