本场 ABC 写出了 0 题!ABC457E 题解
cjdst
·
·
题解
1h40min 没调出来一道绿。我真的是人类吗。
注意到区间 [s,t] 能被覆盖,当且仅当至少满足以下两个条件之一:
- 存在 i,j 满足 i\not=j,s=L_i\le R_i\le t,s\le L_j\le R_j=t,R_i+1\ge L_j。形如下图:
:::info[情况 1]
:::
- 存在 i,j 满足 i\not= j,s=L_i\le L_j\le R_j\le R_i=t。形如下图:
:::info[情况 2]
:::
我们分类讨论两种情况。
情况 1
我们可以将限制改为 $s=L_i\le R_i\red{\lt} t$,$s\le L_j\le R_j=t$。这样如果满足限制,那么 $R_i\lt R_j$,$i,j$ 一定不相等。
但是这个限制会漏计算 $L_i=L_j=s,R_i=R_j=t$ 的情况。但又注意到满足情况 2,所以不影响。
我们可以开一个数据结构,记录以 $i$ 为左端点,可能的右端点,与以 $i$ 为右端点,可能的左端点。显然这个数据结构可以用 $2m$ 个 `set` 实现。
查询时,我们只需要找到以 $s$ 为左端点,右端点 $\lt t$ 的最大值,以及以 $t$ 为右端点,左端点 $\ge s$ 的最小值,比较这两个的大小即可。
#### 情况 2
$i\not= j$ 有点烦人,考虑去掉这个限制。
我们可以将这个限制改为两个限制:
- 存在 $i$ 满足 $s=L_i,R_i=t$。
- 存在至少 $2$ 个 $i$ 满足 $s\le L_i\le R_i\le t$。
显然和之前的限制等价。
对于第一个限制,我们开个 `set` 记录 $L_i,R_i$,然后直接查询即可。
对于第二个限制,是一个二维偏序问题,可以将询问离线后扫描线解决。
:::success[code]
```cpp
#include <bits/stdc++.h>
namespace cjdst{
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair <int, int> pii;
typedef std::pair <ll, ll> pll;
template <typename T>
class Fenwick_Tree{
std::vector <T> tr;
int n;
public:
Fenwick_Tree(){}
Fenwick_Tree(int n){
tr.resize(n + 5, 0);
this -> n = n;
}
void modify(int idx, T val){
idx++;
for(int i = idx; i <= n; i += (i & (-i))) tr[i] += val;
}
T query(int idx){
idx++;
T ans = 0;
for(int i = idx; i; i -= (i & (-i))) ans += tr[i];
return ans;
}
};
void init(){
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
}
struct node{
int l, r, id;
bool operator < (const node b) const{
return l < b.l;
}
bool operator > (const node b) const{
return l > b.l;
}
};
void solve(){
int n, m, q;
std::cin >> m >> n;
std::vector <pii> a(n + 5);
std::vector <std::set <int>> x(m + 5), y(m + 5);
Fenwick_Tree <int> tr(m + 5);
std::set <pii> st;
for(int i = 1; i <= n; i++){
std::cin >> a[i].first >> a[i].second;
x[a[i].first].insert(a[i].second);
y[a[i].second].insert(a[i].first);
st.insert({a[i].first, a[i].second});
}
std::sort(a.begin() + 1, a.begin() + n + 1);
std::cin >> q;
std::vector <node> query(q + 5);
std::vector <int> ans(q + 5);
for(int i = 1; i <= q; i++){
int l, r;
std::cin >> l >> r;
query[i] = {l, r, i};
if(x[l].empty() || y[r].empty()) continue;
auto idx1 = x[l].upper_bound(r), idx2 = y[r].lower_bound(l);
idx1--;
if(idx2 != y[r].end() && *idx2 >= l && *idx1 >= l && *idx1 < r){
if(*idx1 + 1 >= *idx2){
ans[i] = 1;
}
}
}
std::sort(query.begin() + 1, query.begin() + q + 1);
std::vector <node> q1(n + 5);
int ct = 0;
std::priority_queue <node, std::vector <node>, std::greater <node>> pq, qq;
std::vector <int> cur(q + 5);
int l = 1, ql = 1;
for(int i = 1; i <= n; i++){
tr.modify(a[i].second, 1);
pq.push({a[i].first, a[i].second, 0});
}
for(int i = 1; i <= m; i++){
while(!pq.empty() && pq.top().l < i){
tr.modify(pq.top().r, -1);
pq.pop();
}
while(ql <= q && query[ql].l <= i){
cur[query[ql].id] = tr.query(query[ql].r);
ql++;
}
}
std::sort(query.begin() + 1, query.begin() + q + 1, [](node x, node y){
return x.id < y.id;
});
for(int i = 1; i <= q; i++){
std::cout << ((ans[i] || (st.count({query[i].l, query[i].r}) && cur[i] > 1)) ? "Yes\n" : "No\n");
}
}
}
int main(){
cjdst::init();
int T = 1;
// std::cin >> T;
for(int _ = 1; _ <= T; _++){
cjdst::solve();
}
std::cout.flush();
return 0;
}
```
:::
时间复杂度:$O(n\log n)$,假设 $n,m,q$ 同阶。