2020牛客暑期多校训练营(第二场) H Happy Triangle
这道题可以用\(splay\)等平衡树操作,我利用的线段树。
直接思考如何判断三角形成立,即小的两边和大于第三边即可。那么对于找到的三元组{\(x,a,b\)}就有如下的判断(默认\(a<b\))
1.\(x<a\)&&\(x<b\)得到\(x+a>b\)就有\(x>b-a\)
2.\(x>a\)&&\(x<b\)得到\(x+a>b\)就有\(x>b-a\)
3.\(x>b\)&&\(x>a\)得到\(b+a<x\)。
\(1,2\)可以合并在一起,变成\(x<\min{dist(相邻两个数字的差)}\)
故针对前两个操作,我们维护一颗线段树,线段树内维护最小的两个数字的差是多少。
再看看第三个操作,其实本质上是查询是否存在比x小的两个数字,满足条件,所以我们在维护一个最大值和次大值。
故我们首先离线操作,离散化后建树,修改单点,查询区间。
注意的是,线段树合并中的细节,需要注意节点大小,节点大小为1是没有次大值的。还有就是单点修改对于线段树的改变。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e5 + 10;
vector<int>Hash;
int n;
int a[N];
int op[N];
struct node{
int dis;
int maxn;
int submaxn;
int minn;
int sz;
void clear()
{
sz = 0; dis = INF; maxn = minn = submaxn = -1;
}
node operator+(const node temp)const
{
node res;
if (temp.sz == 0)
{
res = *this;
return res;
}
if (sz == 0) {
res = temp; return res;
}
res.maxn = temp.maxn;
vector<int>tt;
tt.push_back(temp.maxn); tt.push_back(temp.submaxn); tt.push_back(maxn); tt.push_back(submaxn);
sort(tt.begin(), tt.end());
res.submaxn = tt[2];
res.minn = minn;
int t1 = Hash[maxn - 1];
int t2 = Hash[temp.minn - 1];
res.dis = min(temp.dis, dis);
res.dis = min(res.dis, t2 - t1);
res.sz = sz + temp.sz;
return res;
}
};
struct SEG {
node tree[N << 2];
void pushup(int root)
{
tree[root] = tree[lrt] + tree[rrt];
}
void build(int l, int r, int root)
{
if (l == r)
{
tree[root].clear(); return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
pushup(root);
}
void insert(int l, int r, int root, int pos)
{
if (l == r)
{
if (tree[root].sz)
{
tree[root].sz++;
tree[root].submaxn = Hash[pos - 1];
tree[root].dis = 0;
}
else {
tree[root].maxn = tree[root].minn = pos;
tree[root].submaxn = -1;
tree[root].dis = INF;
tree[root].sz = 1;
}
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)insert(lson, pos);
else insert(rson, pos);
pushup(root);
}
void del(int l, int r, int root, int pos)
{
if (l == r)
{
if (tree[root].sz > 1)
{
tree[root].sz--;
if (tree[root].sz == 1)
{
tree[root].submaxn = -1;
tree[root].dis = INF;
}
}
else tree[root].clear();
return;
}
int mid = (l + r) >> 1;
if (mid >= pos)del(lson, pos);
else del(rson, pos);
pushup(root);
}
node query(int l, int r, int root, int L, int R)
{
if (L > R)
{
node ans; ans.clear(); return ans;
}
if (L <= l && r <= R)
{
return tree[root];
}
int mid = (l + r) >> 1;
node ans; ans.clear();
bool flag = 0;
if (L <= mid) {
ans = query(lson, L, R);
flag = 1;
}
if (R > mid)
{
if (flag)
ans = ans + query(rson, L, R);
else ans = query(rson, L, R);
}
return ans;
}
}T;
int search(int x)
{
return lower_bound(Hash.begin(), Hash.end(), x) - Hash.begin() + 1;
}
int main()
{
n = read();
upd(i, 1, n)
{
op[i] = read();
a[i] = read();
Hash.push_back(a[i]);
}
sort(Hash.begin(), Hash.end());
Hash.erase(unique(Hash.begin(), Hash.end()), Hash.end());
int nn = Hash.size();
T.build(1, nn, 1);
vector<int>ans;
upd(i, 1, n)
{
if (op[i] == 1)
{
T.insert(1, nn, 1, search(a[i]));
}
else if (op[i] == 2)
{
T.del(1, nn, 1, search(a[i]));
}
else {
int pos = search(a[i]);
node tans = T.query(1, nn, 1, 1, pos - 1);
if (tans.sz >= 2)
{
int t1 = tans.maxn;
int t2 = tans.submaxn;
if (a[i] < Hash[t1-1] + Hash[t2-1])
{
ans.push_back(1); continue;
}
}
node tanss = T.query(1, nn, 1, pos, nn);
if (tanss.sz)
{
tans.dis = INF;
node res = tans + tanss;
if (res.sz >= 2)
{
if (res.dis < a[i]) {
ans.push_back(1);
}
else ans.push_back(0);
}
else ans.push_back(0);
}
else ans.push_back(0);
}
}
for (auto k : ans)
{
if (k)puts("Yes");
else puts("No");
}
return 0;
}
橙橙橙