1h40min 没调出来一道绿。我真的是人类吗。
注意到区间 能被覆盖,当且仅当至少满足以下两个条件之一:
- 存在 满足 。形如下图:
- 存在 满足 。形如下图:
我们分类讨论两种情况。
情况 1
有点烦人,考虑去掉这个限制。
我们可以将限制改为 ,。这样如果满足限制,那么 , 一定不相等。
但是这个限制会漏计算 的情况。但又注意到满足情况 2,所以不影响。
我们可以开一个数据结构,记录以 为左端点,可能的右端点,与以 为右端点,可能的左端点。显然这个数据结构可以用 个 set 实现。
查询时,我们只需要找到以 为左端点,右端点 的最大值,以及以 为右端点,左端点 的最小值,比较这两个的大小即可。
情况 2
有点烦人,考虑去掉这个限制。
我们可以将这个限制改为两个限制:
- 存在 满足 。
- 存在至少 个 满足 。
显然和之前的限制等价。
对于第一个限制,我们开个 set 记录 ,然后直接查询即可。
对于第二个限制,是一个二维偏序问题,可以将询问离线后扫描线解决。
code
#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;
}时间复杂度:,假设 同阶。

