Description

给出一个$n\times m$的$01$矩阵$A$。

记矩阵$X$每一个元素取反以后的矩阵为$X'$,(每一个cell 都01倒置)

定义对$n \times m$的矩阵$A$进行一次变幻操作,变幻后矩阵的大小是$2n \times 2m$的。

具体来说,我们会把$A$复制一份到$A$的右下方,计算$A'$并放置在$A$的正右方和正下方。

设连续操作$n$的结果是$f^n(A)$ 即 $f^n(A) = \left\{\begin{matrix} f(f^{n-1}(A)) & (n\geq 2)\\  \begin{Bmatrix} A & A' \\  A' & A  \end{Bmatrix}  & (n=1)\\A & (n = 0)\end{matrix}\right.$

设矩阵$L = f^{\infty} (A)$ ,给出$Q$个询问,$x1,y1,x2,y2$,求出$L$中子矩阵的和。

对于$100\%$的数据满足$1 \leq n,m \leq 10^3 , 1 \leq Q \leq 10^6 , 1 \leq x1\leq x2 \leq 10^9 ,  1 \leq y1\leq y2 \leq 10^9$

Idea & Solution

我们不妨对于每个矩阵整体考虑。设没有进行翻转运算的矩阵为$0$,否则为$1$

必然是长这样的:$\begin{matrix} 0 & 1 &  1& 0 & 1 & 0 & 0 & 1&  ...\\  1 & 0 & 0 & 1 & 0 &1  & 1  & 0 &...\\  1 & 0 & 0 & 1 & 0 &1  & 1  & 0 & ...\\  0 & 1 &  1& 0 & 1 & 0 & 0 & 1&  ... \\  &&&&...\end{matrix}$

我们会显然的发现第一个数字为$0$的序列都相同,第一个数字为$1$的序列都相同。

而两个序列恰好取反,于是我们可以尝试寻找第一行的性质。

如果我们从$0$开始编号,那么起始点就是$0$,其值为$0$.

对于第1行的第$i$个数字,必然是某一次扩展后产生的,我们会发现,一次扩展会对第一行的宽度$\times 2$

所以,第$i$个数字是$01$是和$Highestbit(i)$相反的,所以我们可以归纳一下发现,对于第$1$行第$i$个元素如果二进制上的$1$的个数为偶数那么就是$0$否则就是$1$.

同时我们会发现纵向和横向的情况一模一样,所以可以进一步推论,$countbit(x) + countbit(y)  $为偶数那么就是$0$否则就是$1$.

我们可能会发现,对于二维前缀和上的一个矩阵,$0$的个数和$1$的个数大致相等,可以分$x , y$坐标的奇偶性讨论$4$种可能即可计算。

最后使用二维前缀和求子矩阵和。

复杂度是$O(nm + T)$

# include<bits/stdc++.h>
# define int long long
using namespace std;
const int N=;
int s0[N][N],s1[N][N],a[N][N],b[N][N];
int n,m,q;
int count(int x) { int ret=;while(x){if(x&)ret++;x>>=;}return ret;}
pair<int,int> find(int x,int y) { return make_pair((x-)/n,(y-)/m);}
int check(int x,int y) { return (count(x)+count(y))&;}
inline int read()
{
int X=,w=; char c=;
while(c<''||c>'') {w|=c=='-';c=getchar();}
while(c>=''&&c<='') X=(X<<)+(X<<)+(c^),c=getchar();
return w?-X:X;
}
void write(int x)
{
if (x>) write(x/);
putchar(''+x%);
}
int solve(int x,int y)
{
if (x<= || y<=) return ;
pair<int,int>tmp=find(x,y); int cx = tmp.first, cy = tmp.second;
if ((cx&) && (cy&)) {
int ret = ((cx*cy-)>>)*n*m,h=x-cx*n,w=y-cy*m;
ret=ret+h*((cy-)>>)*m+w*((cx-)>>)*n;
if (check(cx,cy)==) ret+=s0[h][w]; else ret+=s1[h][w];
if (cx>= && cy>=) { if (check(cx-,cy-)==) ret+=s0[n][m]; else ret+=s1[n][m]; }
if (cx>=) { if (check(cx-,cy)==) ret+=s0[n][w]; else ret+=s1[n][w]; }
if (cy>=) { if (check(cx,cy-)==) ret+=s0[h][m]; else ret+=s1[h][m]; }
return ret;
} else if ((cx&) && !(cy&)) {
int ret = (cx*cy>>)*n*m,h=x-cx*n,w=y-cy*m;
ret=ret+h*(cy>>)*m+w*((cx-)>>)*n;
if (check(cx,cy)==) ret+=s0[h][w]; else ret+=s1[h][w];
if (cx>=) { if (check(cx-,cy)==) ret+=s0[n][w]; else ret+=s1[n][w]; }
return ret;
} else if (!(cx&) && (cy&)) {
int ret = (cx*cy>>)*n*m,h=x-cx*n,w=y-cy*m;
ret=ret+h*((cy-)>>)*m+w*(cx>>)*n;
if (check(cx,cy)==) ret+=s0[h][w]; else ret+=s1[h][w];
if (cy>=) { if (check(cx,cy-)==) ret+=s0[h][m]; else ret+=s1[h][m]; }
return ret;
} else if (!(cx&) && !(cy&)) {
int ret = (cx*cy>>)*n*m,h=x-cx*n,w=y-cy*m;
ret=ret+h*(cy>>)*m+w*(cx>>)*n;
if (check(cx,cy)==) ret+=s0[h][w]; else ret+=s1[h][w];
return ret;
}
}
signed main()
{
n=read();m=read();q=read();
for (int i=;i<=n;i++) {
for (int j=;j<=m;j++) {
char c=; while (c!=''&&c!='') c=getchar();
a[i][j]=(c==''); b[i][j]=-a[i][j];
}
}
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
s0[i][j]=s0[i-][j]+s0[i][j-]-s0[i-][j-]+a[i][j],
s1[i][j]=s1[i-][j]+s1[i][j-]-s1[i-][j-]+b[i][j];
while (q--) {
int x1=read(),y1=read(),x2=read(),y2=read();
int ans = solve(x2,y2)-solve(x2,y1-)-solve(x1-,y2)+solve(x1-,y1-);
write(ans); putchar('\n');
}
return ;
}

『Codeforces 1186E 』Vus the Cossack and a Field (性质+大力讨论)的更多相关文章

  1. E. Vus the Cossack and a Field (求一有规律矩形区域值) (有一结论待证)

    E. Vus the Cossack and a Field (求一有规律矩形区域值) 题意:给出一个原01矩阵,它按照以下规则拓展:向右和下拓展一个相同大小的 0 1 分别和原矩阵对应位置相反的矩阵 ...

  2. CodeForces - 1186 C. Vus the Cossack and Strings (异或)

    Vus the Cossack has two binary strings, that is, strings that consist only of "0" and &quo ...

  3. Codeforces F. Vus the Cossack and Numbers(贪心)

    题目描述: D. Vus the Cossack and Numbers Vus the Cossack has nn real numbers aiai. It is known that the ...

  4. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  5. Codeforces Round #571 (Div. 2)-D. Vus the Cossack and Numbers

    Vus the Cossack has nn real numbers aiai. It is known that the sum of all numbers is equal to 00. He ...

  6. 似魔鬼的 『 document.write 』

    在平时的工作中,楼主很少用 document.write 方法,一直觉得 document.write 是个危险的方法.楼主不用,并不代表别人不用,最近给维护的项目添了一点代码,更加深了我对 &quo ...

  7. 拾遗:『Linux Capability』

    『Linux Capability』 For the purpose of performing permission checks, traditional UNIX implementations ...

  8. 『创意欣赏』20款精致的 iOS7 APP 图标设计

    这篇文章给大家分享20款精致的 iOS7 移动应用程序图标,遵循图形设计的现代潮流,所有图标都非常了不起,给人惊喜.通过学习这些移动应用程序图标,设计人员可以提高他们的创作,使移动用户界面看起来更有趣 ...

  9. 『设计前沿』14款精致的国外 iOS7 图标设计示例

    每天都有大量的应用程序发布到 iOS App Store 上,在数量巨大的应用中想要引起用户的主要,首要的就是独特的图标设计.这篇文章收集了14款精致的国外 iOS7 图标设计示例,希望能带给你设计灵 ...

随机推荐

  1. 带坑使用微信小程序框架WePY组件化开发项目,附带第三方插件使用坑

    纯粹用来记录wepy及相关联内容,以防再犯~ 1. 接手的wepy项目版本是 1.7.2 ,so我没有初始化的过程.... 2. 安装wepy命令工具,npm install wepy-cli -g ...

  2. 发明专利定稿&递交申请啦,开心

    也不想写些什么,只是想简单的分享一下当前的心情! 第一版到最后一版中间因为各种事情耽误,一直弄到现在.5月中旬找的专利代理局中间连续修改很多次,从大改到小改,再到微调真的是学习到了! 下面就是搞定&l ...

  3. c# 后台隐式使用webBrowser

    c#不使用 webBrowser 控件, 在后台加载html流 private void button1_Click(object sender, EventArgs e) { string urlP ...

  4. SpringBoot整合MyBatis的分页插件PageHelper

    1.导入依赖(maven) <dependency> <groupId>com.github.pagehelper</groupId> <artifactId ...

  5. Ubuntu 忘记系统登录密码,如何修改密码

    Ubuntu 忘记系统登录密码,如何修改密码. 1.重新启动,按ESC键进入Boot Menu,选择recovery mode(一般是第二个选项). 2.在#号提示符下用cat /etc/shadow ...

  6. electron-vue在npm run build时报错 ⨯ cannot execute cause=fork/exec C:\Users\801\AppData\Local\electron-builder\Cache\winCodeSign\winCodeSign-2.5.0\rcedit-ia32.exe: Access is denied.

    问题描述 在electron-vue执行npm run build时报错,错误如下: ⨯ cannot execute cause=fork/exec C:\Users\801\AppData\Loc ...

  7. 仍然有人在叫喊C语言已经过时了

    现在,仍然有人在叫喊C语言已经过时了.还有什么值得学习的?看看现在使用Python.PHP和其他语言有多简单.谁去学旧的C语言?是真的吗?作者下载了这两种语言的底层源代码.由于空间的限制,它没有分析框 ...

  8. day07 类

    一.目录 1.模块 2.包 3.isinstance issubclass type 4.方法和函数 5.反射 6.约束 7.继承 8.特殊成员 9.异常处理 补充知识点 10.hashlib模块 1 ...

  9. vuejs 深度监听

    data: { obj: { a: 123 } }, 监听obj中a属性 watch: { 'obj.a': { handler(newName, oldName) { console.log('ob ...

  10. 写Java也得了解CPU–CPU缓存

    CPU,一般认为写C/C++的才需要了解,写高级语言的(Java/C#/pathon…)并不需要了解那么底层的东西.我一开始也是这么想的,但直到碰到LMAX的Disruptor,以及马丁的博文,才发现 ...