Description

  给定一个长度为n的正整数数列a[i]。

  定义2个位置的f值为两者位置差与数值差的和,即f(x,y)=|x-y|+|a[x]-a[y]|。

  你需要写一个程序支持2种操作(k都是正整数):

  Modify x k:将第x个数的值修改为k。

  Query x k:询问有几个i满足f(x,i)<=k。询问不仅要考虑当前数列,还要考虑任意历史版本,即统计任意位置上出现过的任意数值与当前的a[x]的f值<=k的对数。(某位置多次修改为同样的数值,按多次统计)

Main

  令F(x,y)=|x-y|+|a[x]-a[y]|,每次可以将a[x]修改为k,或者查询满足f(x,i)≤k的个数。

Analysis

【二维线段树/树状数组】  

  看到题目这样的粗俗,笼统,简洁明了,便知道一定要用一个数据结构维护

  我们可以将x抽象为x坐标,a[x]抽象为y坐标,那么f(x,y)的意思就显然了:表示x点(x,a[x])和y点(y,a[y])的曼哈顿距离。

  但是这个曼哈顿距离比较蛋疼,不着急,画(截)个图看看。

  

  我们发现图片大概是这样的,发现这个可以取的范围是个菱形,旋转90°就变成了正方形。旋转就是使坐标(x,y)变成(x+y,x-y)

  

  那么,问题就转化为每次可以加入一些点,求某个正方形内包含点的个数。

【K-Dtree】

  。。。

【cbq分治+主席树】

  ...

Solution

  我们考虑用一个数据结构来维护,如二维线段树/树状数组。

  每次相当于插入点(x+a[x],x-a[x])到图中,线段树/树状数组维护左下角为(x1,y1),右上角为(x2,y2)的矩阵信息,查询即可。

  对于二维线段树,有两种实现方法。一种是划分为4个区域,第二种是划分为2个,根据长宽的大小来切。

  

  要动态开点。直接做会超时,要加优化:当前子树没有贡献,直接退出。

【K-Dtree】

  。。。

【cbq分治+主席树】

  ...

Code

{$inline on}
var
ch,lala:char;
n,m,i,x,y,tot,ans:longint;
a:array[..] of longint;
tree:array[..] of longint;
son:array[..,..] of longint;
procedure dispose; inline;
begin
read(ch); lala:='';
while lala<>' ' do read(lala); readln(x,y);
end; procedure fyj(x:longint); inline;
begin
if son[x,]= then
begin
inc(tot); son[x,]:=tot;
inc(tot); son[x,]:=tot;
end;
end; procedure change(root,x1,y1,x2,y2,fx,fy:longint); inline;
var
mid:longint;
begin
if (x1=x2) and (y1=y2) and (x1=fx) and (y1=fy) then
begin
inc(tree[root]); exit;
end; fyj(root); if x2-x1>=y2-y1 then
begin
mid:=(x1+x2) shr ; if fx<=mid then
change(son[root,],x1,y1,mid,y2,fx,fy)
else
change(son[root,],mid+,y1,x2,y2,fx,fy);
end
else
begin
mid:=(y1+y2) shr ; if fy<=mid then
change(son[root,],x1,y1,x2,mid,fx,fy)
else
change(son[root,],x1,mid+,x2,y2,fx,fy);
end; tree[root]:=tree[son[root,]]+tree[son[root,]];
end; procedure find(root,x1,y1,x2,y2,fx1,fy1,fx2,fy2:longint); inline;
var
mid:longint;
begin
if (x1=fx1) and (x2=fx2) and (y1=fy1) and (y2=fy2) then
begin
ans:=ans+tree[root]; exit;
end; if son[root,]= then
exit; if x2-x1>=y2-y1 then
begin
mid:=(x1+x2) shr ; if fx2<=mid then
find(son[root,],x1,y1,mid,y2,fx1,fy1,fx2,fy2)
else
if fx1>mid then
find(son[root,],mid+,y1,x2,y2,fx1,fy1,fx2,fy2)
else
begin
find(son[root,],x1,y1,mid,y2,fx1,fy1,mid,fy2);
find(son[root,],mid+,y1,x2,y2,mid+,fy1,fx2,fy2);
end;
end
else
begin
mid:=(y1+y2) shr ; if fy2<=mid then
find(son[root,],x1,y1,x2,mid,fx1,fy1,fx2,fy2)
else
if fy1>mid then
find(son[root,],x1,mid+,x2,y2,fx1,fy1,fx2,fy2)
else
begin
find(son[root,],x1,y1,x2,mid,fx1,fy1,fx2,mid);
find(son[root,],x1,mid+,x2,y2,fx1,mid+,fx2,fy2);
end;
end;
end;
begin
readln(n,m); tot:=; for i:= to n do
begin
read(a[i]); change(,,,,,i+a[i]+,i-a[i]+);
end; readln; for i:= to m do
begin
dispose; if ch='M' then
begin
change(,,,,,x+y+,x-y+); a[x]:=y;
end
else
begin
ans:=;
find(,,,,,x+a[x]-y+,x-a[x]-y+,x+a[x]+y+,x-a[x]+y+); writeln(ans);
end;
end;
end.

[JZOJ3615]【NOI2014模拟】数列(平面几何+二维线段树)的更多相关文章

  1. [CSP-S模拟测试]:表格(动态开点二维线段树+离散化)

    题目传送门(内部题112) 输入格式 一个数$N$,表示矩形的个数. 接下来$N$行,每行四个整数$X_a,Y_a,X_b,Y_b$.分别表示每个矩形左下角和右上角的坐标. 保证$(X_a<X_ ...

  2. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  3. bzoj4785:[ZJOI2017]树状数组:二维线段树

    分析: "如果你对树状数组比较熟悉,不难发现可怜求的是后缀和" 设数列为\(A\),那么可怜求的就是\(A_{l-1}\)到\(A_{r-1}\)的和(即\(l-1\)的后缀减\( ...

  4. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  5. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  6. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  7. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  8. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  9. HDU 4819 Mosaic (二维线段树)

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

随机推荐

  1. Zabbix 添加端口监控链接

    zabbix 添加端口监控 连接数: zabbix  配置  添加 监控项: 其他检测 方式也是 示例: zabbix 客户端配置文件添加: 编辑 zabbix_agentd.conf  添加  端口 ...

  2. 小程序通过 url 向内嵌 H5 传参注意事项

    当在小程序中通过 url 向 <web-view> 内嵌的 H5 传参时,当包含特殊字符时需要进行编码处理(不然 <web-view> 中是拿不到值的,小程序竟然没有错误提示. ...

  3. pandas.read_csv() 报错 OSError: Initializing from file failed,报错原因分析和解决方法

    今天调用pandas读取csv文件时,突然报错“ OSError: Initializing from file failed ”,我是有点奇怪的,以前用的好好的,read_csv(path)方法不是 ...

  4. 模拟登陆github

    import requests from lxml import etree class Login(object): def __init__(self): self.headers = { 'Re ...

  5. JavaFX - 富互联网应用

    JavaFX教程™ --必看https://www.yiibai.com/javafx /================= 富互联网应用 是那些提供与Web应用程序类似的功能,并可作为桌面应用程序体 ...

  6. Spring Cloud 2-RabbitMQ 集成(八)

    Spring Cloud  RabbitMQ  pom.xml application.yml 提供者 消费者 队列配置 单元测试 通过消息队列MQ做为通信中心,这里采用RabbitMQ.安装方参考: ...

  7. identifier of an instance of **** was altered from **** to *****

    在用hibernate getSession().save(entity)方法保存数据库表实体类的时候报这个异常 我的需求是一个请求要往数据库表插两条数据,根据传值判断做了for循环调两次save() ...

  8. juypter4.4.0 自动补全

    python -m pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user --skip ...

  9. VisualStudio相关序列号

    VisualStudio相关序列号   Visual Studio 2019 Enterprise:BF8Y8-GN2QH-T84XB-QVY3B-RC4DF   Visual Studio 2019 ...

  10. Nginx负载-nginx转发到Swoole服务器(nginx配置文件变更)