이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using nagai = long long;
struct dss {
vector<pair<int,int>> segs;
void insert(int l, int r) {
segs.push_back({l, r});
}
int get(int l, int r, int k) {
if (k == 0) return segs.size();
int res = 0;
for (auto& x : segs) {
int ll = max(x.first, l), rr = min(x.second, r);
if (rr - ll + 1 >= k)
++res;
}
return res;
}
};
const int oo = 0x3f3f3f3f;
struct static1d {
int* coords = nullptr;
int cnt = 0;
void clear() {
if (coords)
delete[] coords;
cnt = 0;
}
void preadd(int c) {
++cnt;
}
void preaddfinish() {
coords = new int[cnt];
cnt = 0;
}
void add(int c) {
coords[cnt++] = c;
}
void finalize() {
}
int get(int l, int r) {
if (l > r) return 0;
auto itr = upper_bound(coords, coords + cnt, r);
auto itl = lower_bound(coords, coords + cnt, l);
//cerr << "coords\n";
//for(auto& x : coords)
//cerr << x << ' ';
//cerr << '\n';
//cerr << l << ' ' << r << ' ' << itr - itl << '\n';
return itr - itl;
}
};
bool ycmp(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second)
return a.second < b.second;
return a.first < b.first;
}
const int N=200200;
struct static2d {
static1d v[N];
//static1d all;
vector<int> xs;
void preadd(int x, int y) {
for (++x; x <= xs.size(); x = (x | x - 1) + 1)
v[x].preadd(y);
}
void add(int x, int y) {
for (++x; x <= xs.size(); x = (x | x - 1) + 1)
v[x].add(y);
}
void build(pair<int,int>* pts, int sz) {
xs.clear();
xs.reserve(sz);
for (int i = 0; i < sz; ++i)
xs.push_back(pts[i].first);
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < xs.size() + 1; ++i)
v[i].clear();
for (int i = 0; i < sz; ++i)
preadd(lower_bound(xs.begin(), xs.end(), pts[i].first) - xs.begin(), pts[i].second);
for (int i = 0; i <= xs.size(); ++i)
v[i].preaddfinish();
for (int i = 0; i < sz; ++i)
add(lower_bound(xs.begin(), xs.end(), pts[i].first) - xs.begin(), pts[i].second);
//all.add(p.second);
for (int i = 0; i <= xs.size(); ++i)
v[i].finalize();
//all.finalize();
}
int get(int xr, int y1, int y2) {
//if (xr == oo)
//return all.get(y1, y2);
if (xr < 0) return 0;
++xr;
int xrr = xr;
xr = lower_bound(xs.begin(), xs.end(), xr) - xs.begin();
int res = 0;
for (; xr; xr &= xr - 1)
res += v[xr].get(y1, y2);
//cerr << xrr << ' ' << y1 << ' ' << y2 << ' ' << res << '\n';
return res;
}
// all inclusive
int get(int x1, int x2, int y1, int y2) {
if (x1 > x2) return 0;
return get(x2, y1, y2) - get(x1 - 1, y1, y2);
}
};
const int BL = 4000;
struct rofl2d {
pair<int,int>* all;
vector<pair<int,int>> block;
vector<pair<int,int>> erblock;
//pair<int, int>* newall;
int call = 0, cnewall = 0;
static2d st;
rofl2d() {
all = new pair<int, int>[N];
//newall = new pair<int, int>[N];
}
void check_rebuild() {
if (block.size() + erblock.size() >= BL) {
sort(block.begin(), block.end(), ycmp);
for (auto x : block)
all[call++] = x;
sort(all, all + call, ycmp);
//cnewall = call + block.size();
sort(erblock.begin(), erblock.end(), ycmp);
int ptr = 0;
int eptr = 0;
for (int i = 0; i < call; ++i) {
if (eptr < erblock.size() && erblock[eptr] == all[i])
++eptr;
else
all[ptr++] = all[i];
}
call = ptr;
block.clear();
erblock.clear();
st.build(all, call);
}
}
void insert(int x, int y) {
block.push_back({x, y});
check_rebuild();
}
void erase(int x, int y) {
erblock.push_back({x, y});
check_rebuild();
}
int get(int x1, int x2, int y1, int y2) {
int res = st.get(x1, x2, y1, y2);
/*
for (auto p : block)
if (x1 <= p.first && p.first <= x2 && y1 <= p.second && p.second <= y2)
++res;
for (auto p : erblock)
if (x1 <= p.first && p.first <= x2 && y1 <= p.second && p.second <= y2)
--res;
*/
return res;
}
};
struct ds {
int tot = 0;
rofl2d rofl_l, rofl_r;
void insert(int l, int r) {
++tot;
rofl_l.insert(l, r - l);
rofl_r.insert(r, r - l);
//rofl_len.insert(0, r - l);
}
void erase(int l, int r) {
--tot;
rofl_l.erase(l, r - l);
rofl_r.erase(r, r - l);
//rofl_len.erase(0, r - l);
}
int get(int l, int r, int k) {
if (r - l + 1 < k) return 0;
if (k == 0) return tot;
int res = tot - rofl_l.block.size() + rofl_l.erblock.size();
for (auto p : rofl_l.block) {
int ll = max(p.first, l);
int rr = min(p.first + p.second, r);
if (rr - ll + 1 >= k)
++res;
}
for (auto p : rofl_l.erblock) {
int ll = max(p.first, l);
int rr = min(p.first + p.second, r);
if (rr - ll + 1 >= k)
--res;
}
//cerr << "rofl_l" << '\n';
res -= rofl_l.get(r - k + 2, oo, k - 1, oo);
//cerr << "rofl_r" << '\n';
res -= rofl_r.get(0, l + k - 2, k - 1, oo);
//cerr << "rofl_len" << '\n';
res -= rofl_l.get(0, oo, 0, k - 2);
return res;
}
} add;
void stress() {
int C = 5;
for (int i = 0; i < 10000; ++i) {
ds smart;
dss stupid;
stringstream ss;
for (int i = 0; i < 10; ++i) {
int x = rand() % C, y = rand() % C;
if (x > y) swap(x, y);
ss << "1 " << x << ' ' << y << '\n';
smart.insert(x, y);
stupid.insert(x, y);
int a = rand() % C, b = rand() % C, k = rand() % C;
if (a > b) swap(a, b);
if (smart.get(a, b, k) != stupid.get(a, b, k)) {
ss << smart.get(a, b, k) << ' ' << stupid.get(a, b, k) << '\n';
ss << "3 " << a << ' ' << b << ' ' << k << '\n';
cerr << ss.str();
exit(1);
}
}
}
exit(0);
}
int main() {
//stress();
ios::sync_with_stdio(false);
cin.tie(0);
int n, t;
cin >> n >> t;
int lastans = 0;
vector<pair<int, int>> segs;
while (n--) {
int tt;
cin >> tt;
if (tt == 1) {
int a, b;
cin >> a >> b;
a ^= t * lastans;
b ^= t * lastans;
if (a > b) swap(a, b);
segs.push_back({a, b});
add.insert(a, b);
}
else if (tt == 2) {
int id;
cin >> id;
--id;
add.erase(segs[id].first, segs[id].second);
}
else {
int a, b, k;
cin >> a >> b >> k;
a ^= t * lastans;
b ^= t * lastans;
if (a > b) swap(a, b);
lastans = add.get(a, b, k);
cout << lastans << '\n';
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
segments.cpp: In member function 'void static2d::preadd(int, int)':
segments.cpp:77:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (++x; x <= xs.size(); x = (x | x - 1) + 1)
| ~~^~~~~~~~~~~~
segments.cpp:77:40: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
77 | for (++x; x <= xs.size(); x = (x | x - 1) + 1)
| ~~^~~
segments.cpp: In member function 'void static2d::add(int, int)':
segments.cpp:82:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (++x; x <= xs.size(); x = (x | x - 1) + 1)
| ~~^~~~~~~~~~~~
segments.cpp:82:40: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
82 | for (++x; x <= xs.size(); x = (x | x - 1) + 1)
| ~~^~~
segments.cpp: In member function 'void static2d::build(std::pair<int, int>*, int)':
segments.cpp:93:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < xs.size() + 1; ++i)
| ~~^~~~~~~~~~~~~~~
segments.cpp:97:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i <= xs.size(); ++i)
| ~~^~~~~~~~~~~~
segments.cpp:102:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int i = 0; i <= xs.size(); ++i)
| ~~^~~~~~~~~~~~
segments.cpp: In member function 'int static2d::get(int, int, int)':
segments.cpp:112:7: warning: unused variable 'xrr' [-Wunused-variable]
112 | int xrr = xr;
| ^~~
segments.cpp: In member function 'void rofl2d::check_rebuild()':
segments.cpp:154:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | if (eptr < erblock.size() && erblock[eptr] == all[i])
| ~~~~~^~~~~~~~~~~~~~~~
# | 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... |