【2020.11.19提高组模拟】二次剩余two 题解

题目描述

有\(n\)个二次函数,每个二次函数可以用两个值\(m,k\)描述:

\[f(x)=(x-m)^2+k
\]

现在有\(q\)次操作:

1.插入一个二次函数\((m,k)\)

2.\((x,t)\)删除所有\(f(x)\le t\)的二次函数。

输出每次操作后还剩下的二次函数个数。

对于所有的数据,保证\(n,q,m,k,x,t\in [1,1e5]\)

以下是数据范围表格节选。

\(n,q\le\) \(m\le\)
1~4 20000 100000
5~6 100000 1000
7~10 100000 100000

Solution

考场上我先想到了对\(n^2\)的暴力进行卡常优化——结果就过了qaq老师不要重测卡我啊!

虽然这不是正解,但是我还是记录一下吧。

用了两个栈,先把开始的二次函数存到栈1。

对于操作1,直接入栈1。

对于操作2,扫描栈1内所有的二次函数,若无需删去就加入栈2,否则不加;再将栈2复制到栈1。

可以利用类似滚动数组的方法在两个栈之间回滚,省去复制操作;不要暴力清空栈,只要把size赋值为0就好。这样每次进行操作2需要遍历的次数仅仅为当前二次函数的个数。

Code-\(O(n^2)\)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
#define debug printf("Now is %d\n",__LINE__);
using namespace std; template<class T>inline void read(T&x)
{
char ch=getchar();
int fu;
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') fu=-1,ch=getchar();
x=ch-'0';ch=getchar();
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
x*=fu;
}
inline int read()
{
int x=0,fu=1;
char ch=getchar();
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') fu=-1,ch=getchar();
x=ch-'0';ch=getchar();
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
return x*fu;
}
int G[55];
template<class T>inline void write(T x)
{
int g=0;
if(x<0) x=-x,putchar('-');
do{G[++g]=x%10;x/=10;}while(x);
for(re int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
}
int n,q;
struct two
{
LL m,k;
}sk[2][1000010];
int size[2];
int main()
{
freopen("two.in","r",stdin);
freopen("two.out","w",stdout);
n=read();
q=read();
for(re int i=1;i<=n;i++) sk[0][i].m=read(),sk[0][i].k=read();
size[0]=n;
int op,x,t;
bool flag=0;
while(q--)
{
op=read();
if(op==1)
{
sk[flag][++size[flag]].m=read();
sk[flag][size[flag]].k=read();
write(size[flag]);
}
else
{
x=read();
t=read();
for(re int i=1;i<=size[flag];i++)
{
if((x-sk[flag][i].m)*(x-sk[flag][i].m)+sk[flag][i].k>t) sk[!flag][++size[!flag]]=sk[flag][i];
}
size[flag]=0;
flag=!flag;
write(size[flag]);
}
}
return 0;
}

记得开\(\texttt{long long}\)。

Std

因为\(k\in \mathbb N^+\),所以先不考虑k。

\[f(x)=(x-m)^2\le t\\
\Rightarrow -\sqrt t \le x-m\le \sqrt t\\
\Rightarrow x-\sqrt t\le m\le x+\sqrt t
\]

所以,可能会被删去的二次函数的\(m\)只可能在\(x\pm\sqrt t\)中。

而对于相同的\(m\),k小的应该越会被删去。

所以我们对每种m都用一个小根堆,扫描时只要扫\(x-\sqrt t\le m\le x+\sqrt t\)区间中的堆,然后尽量取出合法的二次函数即可。

时间复杂度:\(O((n+q)\log n+q\sqrt t)\)。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
#define debug printf("Now is %d\n",__LINE__);
using namespace std; template<class T>inline void read(T&x)
{
char ch=getchar();
int fu;
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') fu=-1,ch=getchar();
x=ch-'0';ch=getchar();
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
x*=fu;
}
inline int read()
{
int x=0,fu=1;
char ch=getchar();
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') fu=-1,ch=getchar();
x=ch-'0';ch=getchar();
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
return x*fu;
}
int G[55];
template<class T>inline void write(T x)
{
int g=0;
if(x<0) x=-x,putchar('-');
do{G[++g]=x%10;x/=10;}while(x);
for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
}
LL n,q,ans;
priority_queue<int>a[100010];
int main()
{
// freopen("two.in","r",stdin);
// freopen("two.out","w",stdout);
n=read();
q=read();
for(re int i=1,m,k;i<=n;i++) m=read(),k=read(),a[m].push(-k);
ans=n;
int op,x,y,l,r;
while(q--)
{
op=read();
if(op==1)
{
x=read();
y=read();
a[x].push(-y);
ans++;
}
else
{
x=read();
y=read();
l=x-sqrt(y);
r=x+sqrt(y);
for(re int i=max(l,1);i<=r&&i<=100000;i++)
{
while(!a[i].empty())
{
if((x-i)*(LL)(x-i)-a[i].top()>y) break;
a[i].pop();
ans--;
}
}
}
write(ans);
}
return 0;
}

Attention

不要抱有侥幸心理。这次能过,下次还是会\(40pts\)乃至\(20pts\)的。

【2020.11.19提高组模拟】二次剩余two 题解的更多相关文章

  1. 【2020.11.28提高组模拟】T1染色(color)

    [2020.11.28提高组模拟]T1染色(color) 题目 题目描述 给定 \(n\),你现在需要给整数 \(1\) 到 \(n\) 进行染色,使得对于所有的 \(1\leq i<j\leq ...

  2. 【2020.11.28提高组模拟】T2 序列(array)

    序列(array) 题目描述 ​给定一个长为 \(m\) 的序列 \(a\). 有一个长为 \(m\) 的序列 \(b\),需满足 \(0\leq b_i \leq n\),\(\sum_{i=1}^ ...

  3. 【2020.11.30提高组模拟】剪辣椒(chilli)

    剪辣椒(chilli) 题目描述 在花园里劳累了一上午之后,你决定用自己种的干辣椒奖励自己. 你有n个辣椒,这些辣椒用n-1条绳子连接在一起,任意两个辣椒通过用若干个绳子相连,即形成一棵树. 你决定分 ...

  4. 【2020.11.30提高组模拟】删边(delete)

    删边(delete) 题目 题目描述 给你一棵n个结点的树,每个结点有一个权值,删除一条边的费用为该边连接的两个子树中结点权值最大值之和.现要删除树中的所有边,删除边的顺序可以任意设定,请计算出所有方 ...

  5. JZOJ 【2020.11.30提高组模拟】剪辣椒(chilli)

    题目大意 给出一棵 \(n\) 个节点的树,删去其中两条边 使得分出的三个子树大小中最大与最小的差最小 分析 先一边 \(dfs\) 预处理出以 \(1\) 为根每个点的 \(size\) 然后按 \ ...

  6. JZOJ 6904. 【2020.11.28提高组模拟】T3 树上询问(query)

    题目 你有一棵 \(n\) 节点的树 ,回答 \(m\) 个询问,每次询问给你两个整数 \(l,r\) ,问存在多少个整数 \(k\) 使得从 \(l\) 沿着 \(l \to r\) 的简单路径走 ...

  7. 11.5NOIP2018提高组模拟题

    书信(letter) Description 有 n 个小朋友, 编号为 1 到 n, 他们每人写了一封信, 放到了一个信箱里, 接下来每个人从中抽取一封书信. 显然, 这样一共有 n!种拿到书信的情 ...

  8. 【2020.12.03提高组模拟】A组反思

    估计:40+10+0+0=50 实际:40+10+0+0=50 rank40 T1 赛时看到\(n,m\leq9\),我当机立断决定打表,暴力打了几个点之后发现在\(n\ne m\)且\(k\ne0\ ...

  9. 【2020.12.01提高组模拟】卡特兰数(catalan)

    题目 题目描述 今天,接触信息学不久的小\(A\)刚刚学习了卡特兰数. 卡特兰数的一个经典定义是,将\(n\)个数依次入栈,合法的出栈序列个数. 小\(A\)觉得这样的情况太平凡了.于是,他给出了\( ...

  10. 【2020.12.01提高组模拟】A组反思

    105,rk45 T1 赛时一开始先打了\(m=0\)的情况,也就是普通的卡特兰数,然后打了暴力,样例过了,把样例改改就不行了,原因没有保证是枚举的是合法的出栈序列 得分:\(WA\&TLE1 ...

随机推荐

  1. qa 工作

    1.定流程--监控参照规范(cmmi,公司自己的,scrum[例会.启动会])--产出物报告 (项目维度)-配置--经盈.财务 2.培训组织-组织讲师(知识库).外部拓展

  2. Linux运维必备:sort 命令快速上手指南

    作为运维工程师,处理日志.分析数据是家常便饭.sort命令是Linux中高效整理文本的神器,能快速对文件内容排序.去重.统计.本文用最简洁的方式,帮你掌握sort的核心用法. 一.基础排序:秒杀杂乱文 ...

  3. 保存深度值——小端序,位数,Android,Huawei AR engine

    保存深度值--小端序,位数,Android accuireDepthImage 华为Mate Pro系列基本上前置摄像头都是有TOF的,也就是能够得到场景的深度信息,在华为的AR engine里提供了 ...

  4. 【Win32】VC6 Visual C/C++ 6.0 修改程序图标

    零.需求 就想给自己的C程序加个图标,好看些 一.解决 1.操作步骤 1.新建一个资源脚本 2.在新建的脚本上右键,选择插入 3.选择Icon,点新建或者引入,如果你没有准备图标点新建,有的话直接点引 ...

  5. 如何查看 linux 发行版本

    以 debian 10 buster 为例 有时候我们需要知道当前正在使用的 linux 的发行版本信息...可以通过下面几种方式来查看 使用 lsb_release 命令查看 lsb_release ...

  6. zk基础—4.zk实现分布式功能

    大纲 1.zk实现数据发布订阅 2.zk实现负载均衡 3.zk实现分布式命名服务 4.zk实现分布式协调(Master-Worker协同) 5.zk实现分布式通信 6.zk实现Master选举 7.z ...

  7. AIops

    How does AIOps work?With AIOps, your organization takes a more proactive approach to resolve IT oper ...

  8. mongo db集群故障选举分析

    转载请注明出处: 一.MongoDB集群基础架构 1. 副本集(Replica Set)核心原理 节点角色: Primary:唯一可写节点,处理所有写操作和默认读请求 Secondary:异步复制Pr ...

  9. ShardingJdbc学习笔记

    Mysql主从复制遇到问题 安装mysql Install/Remove of the Service Denied!错误的解决办法

  10. 康谋方案 | AVM合成数据仿真验证方案

    随着自动驾驶技术的快速发展,仿真软件在开发过程中扮演着越来越重要的角色.仿真传感器与环境不仅能够加速算法验证,还能在安全可控的条件下进行复杂场景的重复测试. 本文将分享如何利用自动驾驶仿真软件配置仿真 ...