题解:P16955 「NLOI Round1」宇宙冷漠
P16955 「NLOI Round1」宇宙冷漠
P16955 「NLOI Round1」宇宙冷漠
首先进行一个比较套路的题意转化。原题等价于问是否所有坐标为
转化的证明:考虑两条直线的情况。交点
x 坐标为x = \frac{b_2 - b_1}{k_1 - k_2} 。而两个坐标为(k_1,b_1),(k_2,b_2) 的点所连成的线,它的斜率为- \left( \frac{b_2 - b_1}{k_1 - k_2} \right) 。我们惊奇的发现,它们刚好是相反数的关系。于是,如果一堆线共点,可以直接等价为所有(k_i,b_i) 的连线的斜率相等,也就是它们共线。
然后思考如何快速查询这个东西。考虑线段树,每个节点维护以下信息:
- 当前区间是否合法。
- 若合法,当前区间表示的直线。
其中,第二个信息可以使用两个坐标(两个点)来表示。
合并时,讨论一下左右子树的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Point{
ll x,y;
bool operator==(const Point &a)const{
return a.x == x && a.y == y;
}
bool operator!=(const Point &a)const{
return !(*this == a);
}
bool check(const Point &a,const Point &b){
return (b.x - a.x) * (y - a.y) == (b.y - a.y) * (x - a.x);
}
};
struct Node{
bool fl = 1;
Point p1,p2;
Node operator+(const Node &a)const{
if(!fl || !a.fl){
return {0,{(ll)"I",(ll)"Love"},{(ll)"Columbina",(ll)"!"}};
}
array<Point,4> tmp;
int sz = 0;
tmp[sz++] = p1;
if(p1 != p2) tmp[sz++] = p2;
if(a.p1 != p1 && a.p1 != p2) tmp[sz++] = a.p1;
if(a.p2 != p1 && a.p2 != p2 && a.p2 != a.p1) tmp[sz++] = a.p2;
if(sz == 1){
return {1,p1,p1};
}
else if(sz == 2){
if(tmp[0].x == tmp[1].x){
return {0,{(ll)"I",(ll)"Love"},{(ll)"Columbina",(ll)"!"}};
}
return {1,tmp[0],tmp[1]};
}
else{
ll chk = 1;
for(int i = 2;i < sz;i++){
if(!tmp[i].check(tmp[0],tmp[1])) chk = 0;
}
if(chk && tmp[0].x != tmp[1].x){
return {1,tmp[0],tmp[1]};
}
else{
return {0,{(ll)"I",(ll)"Love"},{(ll)"Columbina",(ll)"!"}};
}
}
}
};
struct Segment_tree{
int n;
vector<Node> nodes;
Segment_tree(const vector<Node> &arr){
n = arr.size() - 1;
nodes.resize(n * 4);
build(1,1,n,arr);
}
void pushup(int p){
nodes[p] = nodes[p<<1] + nodes[p<<1|1];
}
void build(int p,int l,int r,const vector<Node> &a){
if(l == r){
nodes[p] = a[l];
return;
}
int mid = (l + r) >> 1;
build(p<<1,l,mid,a);
build(p<<1|1,mid + 1,r,a);
pushup(p);
}
void insert(int p,int l,int r,int d,const Node &x){
if(l == r && l == d){
nodes[p] = x;
return;
}
int mid = (l + r) >> 1;
if(mid >= d) insert(p<<1,l,mid,d,x);
else insert(p<<1|1,mid + 1,r,d,x);
pushup(p);
}
Node query(int p,int l,int r,int d,int b){
if(l >= d && r <= b){
return nodes[p];
}
int mid = (l + r) >> 1;
if(mid >= b) return query(p<<1,l,mid,d,b);
if(mid < d) return query(p<<1|1,mid + 1,r,d,b);
return query(p<<1,l,mid,d,b) + query(p<<1|1,mid + 1,r,d,b);
}
};
int main(){
cin.tie(0) -> sync_with_stdio(0);
int n,q;
cin>>n>>q;
vector<Node> a(n + 1);
for(int i = 1;i <= n;i++){
ll k,b;
cin>>k>>b;
a[i] = {1,{k,b},{k,b}};
}
Segment_tree t(a);
while(q--){
int l,r;
cin>>l>>r;
if(t.query(1,1,n,l,r).fl) cout<<"Yes"<<'\n';
else cout<<"No"<<'\n';
}
return 0;
}