Description
Input

第一行为两个正整数N,M,表示棋盘的大小。 第二行为两个正整数X,Y,表示棋盘守护者的位置。 第三行仅有一个正整数T,表示棋盘守护者将进行次操作。 接下来N行,每行有M个正整数,用来描述初始时棋盘上每个位置的数。 接下来T行,按操作的时间顺序给出T次操作。每行描述一次操作,以一个数字0或1开头: 若以数字0开头,表示此操作为询问,随后会有四个非负整数x1,y1,x2,y2,表示询问的区域是以棋盘守护者的位置为基础向上扩展x1行,向下扩展y1行,向左扩展x2列,向右扩展y2列得到的矩形区域(详见样例)。 若以数字1开头,表示此操作为修改,随后会有四个正整数x1,y1,x2,y2和一个整数c,表示修改区域的上、下边界分别为第x1,x2行,左、右边界分别为第y1,y2列(详见样例),在此矩形区域内的所有数统一加上c(注意c可能为负数)。
Output

对于每次询问操作,每行输出一个数,表示该区域内所有数的最大公约数。
Sample Input
2 2
1 1
4
6 12
18 24
0 0 0 1 0
1 1 1 1 2 6
1 2 1 2 2 6
0 0 0 1 1

Sample Output
6 6

一开始觉得直接放在一个二维线段树上,但是太麻烦了(打了20多个if,没有力气debug)

想了一晚上,果断改成两个一维的和一个二维的,好打多了,早上改了一下就A了

首先按点(x,y)把棋盘化成4个部分,然后就相当于只要得到前缀gcd,所以横着差分一下,竖着差分一下,然后把4个矩形拼在一起就变成了正解里的那个奇怪的矩阵

第一次打二维线段树

 const
maxn=;
type
node=record
l,r,lc,rc:longint;
a:int64;
end;
var
a,b:array[..maxn]of int64;
f,f2:array[..maxn*]of node;
n,m,t,x,y,tot,cnt,ll,rr,root1,root2:longint; function calc(a,b:longint):longint;
begin
exit((a-)*m+b);
end; function gcd(a,b:int64):int64;
begin
if b= then exit(a);
exit(gcd(b,a mod b));
end; procedure new(x,y,z:longint);
begin
f2[x].a:=gcd(abs(f2[y].a),abs(f2[z].a));
if f2[x].l=f2[x].r then exit;
new(f2[x].lc,f2[y].lc,f2[z].lc);
new(f2[x].rc,f2[y].rc,f2[z].rc);
end; procedure build2(l,r:longint);
var
now,mid:longint;
begin
if l>r then exit;
inc(cnt);now:=cnt;
f2[now].l:=l;f2[now].r:=r;
if l=r then
begin
if ll=rr then f2[now].a:=b[calc(ll,l)];
exit;
end;
mid:=(l+r)>>;
f2[now].lc:=cnt+;
build2(l,mid);
f2[now].rc:=cnt+;
build2(mid+,r);
if ll=rr then f2[now].a:=gcd(abs(f2[f2[now].lc].a),abs(f2[f2[now].rc].a));
end; procedure build(l,r:longint);
var
now,mid:longint;
begin
if l>r then exit;
inc(tot);now:=tot;
f[now].l:=l;f[now].r:=r;
if l=r then
begin
f[now].a:=cnt+;
ll:=l;rr:=r;
build2(,m-);
exit;
end;
mid:=(l+r)>>;
f[now].lc:=tot+;
build(l,mid);
f[now].rc:=tot+;
build(mid+,r);
f[now].a:=cnt+;
ll:=l;rr:=r;
build2(,m-);
new(f[now].a,f[f[now].lc].a,f[f[now].rc].a);
end; function get(now,l,r:longint):int64;
var
mid:longint;
begin
if (f2[now].l>=l) and (f2[now].r<=r) then exit(abs(f2[now].a));
mid:=(f2[now].l+f2[now].r)>>;
get:=;
if l<=mid then get:=gcd(get(f2[now].lc,l,r),get);
if r>mid then get:=gcd(get(f2[now].rc,l,r),get);
end; function get(now,x1,y1,x2,y2:longint):int64;
var
mid:longint;
begin
if (x1>y1) or (x2>y2) then exit();
if (f[now].l>=x1) and (f[now].r<=y1) then exit(get(f[now].a,x2,y2));
get:=;mid:=(f[now].l+f[now].r)>>;
if x1<=mid then get:=gcd(get(f[now].lc,x1,y1,x2,y2),get);
if y1>mid then get:=gcd(get(f[now].rc,x1,y1,x2,y2),get);
end; procedure new(x,y,z,k:longint);
var
mid:longint;
begin
f2[x].a:=gcd(abs(f2[y].a),abs(f2[z].a));
if f2[x].l=f2[x].r then exit;
mid:=(f2[x].l+f2[x].r)>>;
if k<=mid then new(f2[x].lc,f2[y].lc,f2[z].lc,k)
else new(f2[x].rc,f2[y].rc,f2[z].rc,k);
end; procedure add(now,x:longint;c:int64);
var
mid:longint;
begin
if f2[now].l=f2[now].r then
begin
inc(f2[now].a,c);
exit;
end;
mid:=(f2[now].l+f2[now].r)>>;
if x<=mid then add(f2[now].lc,x,c)
else add(f2[now].rc,x,c);
f2[now].a:=gcd(abs(f2[f2[now].lc].a),abs(f2[f2[now].rc].a));
end; procedure add(now,x,y:longint;c:int64);
var
mid:longint;
begin
if (x<) or (x>=n) or (y<) or (y>=m) then exit;
if f[now].l=f[now].r then
begin
add(f[now].a,y,c);
exit;
end;
mid:=(f[now].l+f[now].r)>>;
if x<=mid then add(f[now].lc,x,y,c)
else add(f[now].rc,x,y,c);
new(f[now].a,f[f[now].lc].a,f[f[now].rc].a,y);
end; procedure main;
var
i,j,k,x1,y1,x2,y2:longint;
c:int64;
begin
read(n,m,x,y,t);
for i:= to n do
for j:= to m do
read(a[calc(i,j)]);
for i:= to n- do
for j:= to m- do
b[calc(i,j)]:=a[calc(i,j)]+a[calc(i+,j+)]-a[calc(i,j+)]-a[calc(i+,j)];
build(,n-);
ll:=rr+;
root1:=cnt+;
build2(,n);
root2:=cnt+;
build2(,m);
for i:= to n do
if i=x then add(root1,x,a[calc(x,y)])
else
if i<x then add(root1,i,a[calc(i,y)]-a[calc(i+,y)])
else add(root1,i,a[calc(i,y)]-a[calc(i-,y)]);
for i:= to m do
if i=y then add(root2,y,a[calc(x,y)])
else
if i<y then add(root2,i,a[calc(x,i)]-a[calc(x,i+)])
else add(root2,i,a[calc(x,i)]-a[calc(x,i-)]);
for i:= to t do
begin
read(k,x1,y1,x2,y2);
if k= then writeln(gcd(gcd(get(,x-x1,x+x2-,y-y1,y+y2-),get(root1,x-x1,x+x2)),get(root2,y-y1,y+y2)))
else
begin
read(c);
if (x1<=x) and (x2>=x) and (y1<=y) and (y2>=y) then
begin
add(root1,x,c);
add(root2,y,c);
end;
if (x1<=x) and (x2>=x) then
begin
if (y1<=y) and (y1>) then add(root2,y1-,-c);
if y1>y then add(root2,y1,c);
if (y2>=y) and (y2<m) then add(root2,y2+,-c);
if y2<y then add(root2,y2,c);
end;
if (y1<=y) and (y2>=y) then
begin
if (x1<=x) and (x1>) then add(root1,x1-,-c);
if x1>x then add(root1,x1,c);
if (x2>=x) and (x2<n) then add(root1,x2+,-c);
if x2<x then add(root1,x2,c);
end;
add(,x1-,y1-,c);
add(,x1-,y2,-c);
add(,x2,y2,c);
add(,x2,y1-,-c);
end;
end;
end; begin
main;
end.

2877: [Noi2012]魔幻棋盘 - BZOJ的更多相关文章

  1. BZOJ2877:[NOI2012]魔幻棋盘

    浅谈树状数组与主席树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  2. NOI2012 魔幻棋盘

    http://www.lydsy.com/JudgeOnline/problem.php?id=2877 二维线段树. 好恶...... B类数据: 棋盘是一维的. 我们有一个结论: $gcd(a_{ ...

  3. 【bzoj2877】 Noi2012—魔幻棋盘

    http://www.lydsy.com/JudgeOnline/problem.php?id=2877 (题目链接) 题意 一个${n*m}$的矩阵,维护两个操作:给任意子矩阵${+val}$:问某 ...

  4. BZOJ2877 [Noi2012]魔幻棋盘

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. 题解 洛谷 P2086 【[NOI2012]魔幻棋盘】

    先考虑只有一维的情况,要求支持区间加和求区间 \(\gcd\),根据 \(\gcd\) 的性质,发现: \[ \gcd(a_1,a_2,a_3,\ldots a_n)=\gcd(a_i,a_2-a_1 ...

  6. 数据结构(二维线段树,差分): NOI2012 魔幻棋盘

    貌似想复杂了…… #include <iostream> #include <cstring> #include <cstdio> #define mid ((l+ ...

  7. BZOJ2877 NOI2012魔幻棋盘(二维线段树)

    显然一个序列的gcd=gcd(其差分序列的gcd,序列中第一个数).于是一维情况直接线段树维护差分序列即可. 容易想到将该做法拓展到二维.于是考虑维护二维差分,查询时对差分矩阵求矩形的gcd,再对矩形 ...

  8. [BZOJ2877][NOI2012]魔幻棋盘(二维线段树)

    https://blog.sengxian.com/solutions/bzoj-2877 注意二维线段树的upd()也是一个O(log n)的函数(pushdown()应该也是但没写过). #inc ...

  9. 【NOI2012】魔幻棋盘

    Description 将要读二年级的小 Q 买了一款新型益智玩具——魔幻棋盘,它是一个N行M列的网格棋盘,每个格子中均有一个正整数.棋盘守护者在棋盘的第X行Y列(行与列均从1开始编号) 并且始终不会 ...

随机推荐

  1. Oracle学习笔记4 使用Navicat for Oracle 连接Oracle时出现错误:ORA-28547: connection to server failed, probable Oracle Net admin error

    出问题到的机器环境: Oracle 11gR2 64bit Navicat for Oracle 11.0.10 根据网上一些大神的做法及个人的一些推测,总结如下: 问题出现的原因:Navicat与O ...

  2. EL表达式获取数据

    EL 全名为Expression Language. EL主要作用 获取数据: •EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数据.(某个web域 ...

  3. 一场ACM一场梦——我的一年

    听着裁判倒计时比赛结束,看着全场鲜艳的气球,今天的结果是the last result i can image. 过几天给校赛出题,去年此时的我,还从来没有过竞赛的经验,只因为在大学开学前看了一点点c ...

  4. Part 89 to 91 Talking about pass the parameters in thread

    Part 89   ParameterizedThreadStart delegate Use ParameterizedThreadStart delegate to pass data to th ...

  5. eclipse安装tomcate插件步骤

    今天在网上看了很多关于eclipse需要安装tomcat的文章,小编也没有指责的意思,只是想说为什么有那么多雷同且不正确的搭建过程,真是ctrl+c, ctrl+v玩多了吧,不知道流程有的是错误的,不 ...

  6. (转)RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)

    上篇文章中,我们把每个Message都是deliver到某个Consumer.在这篇文章中,我们将会将同一个Message deliver到多个Consumer中.这个模式也被成为 "pub ...

  7. js获取url及url参数的方法

    <script language="JavaScript" type="text/javascript"> function GetUrlParms ...

  8. java使用sigar 遇到问题的解决方案

    先给大家介绍一个开源工具Sigar 官网:http://sigar.hyperic.com/ API:http://www.hyperic.com/support/docs/sigar/index-a ...

  9. 济南学习 Day 2 T3 pm

    它[问题描述]N个人坐成一圈,其中第K个人拿着一个球.每次每个人会以一定的概率向左边的人和右边的人传球.当所有人都拿到过球之后,最后一个拿到球的人即为胜者.求第N个人获胜的概率. (所有人按照编号逆时 ...

  10. Codevs 2875 RY哥查字典

    时间限制: 1 s  空间限制: 16000 KB  题目等级 : 钻石 Diamond  题目描述 Description RY哥最近新买了一本字典,他十分高兴,因为这上面的单词都十分的和谐,他天天 ...