[LOJ3083] [GXOI2019] 与或和
题目链接
LOJ:https://loj.ac/problem/3083
洛谷:https://www.luogu.org/problemnew/show/P5300
Solution
逐位考虑,可以发现问题就是求一个\(\rm 01\)矩阵的全\(\rm 0\)子矩形个数。
那么我们可以用一个上升的单调栈来求这个,总复杂度\(O(n^2\log v)\)。
#include<bits/stdc++.h>
using namespace std;
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
#define pii pair<int,int >
#define vec vector<int >
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 1e3+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int add(int x,int y) {return x+y>=mod?x+y-mod:x+y;}
int del(int x,int y) {return x-y<0?x-y+mod:x-y;}
int mul(int x,int y) {return 1ll*x*y-1ll*x*y/mod*mod;}
int s[maxn][maxn],a[maxn][maxn],in[maxn][maxn],n,ans1,ans2,top,sta[maxn];
int calc() {
FOR(i,1,n) FOR(j,1,n) s[i][j]=a[i][j]*(s[i-1][j]+1);
ll res=0;
FOR(i,1,n) {
sta[top=0]=0;ll tmp=0;
FOR(j,1,n) {
tmp+=s[i][j];
while(top&&s[i][sta[top]]>=s[i][j])
tmp-=(s[i][sta[top]]-s[i][j])*(sta[top]-sta[top-1]),top--; //弹栈的时候把不合法贡献减去
sta[++top]=j;res+=tmp;
}
}return res%mod;
}
void solve(int x) {
FOR(i,1,n) FOR(j,1,n) a[i][j]=(in[i][j]>>x)&1;
ans1=add(ans1,mul(calc(),1<<x));
FOR(i,1,n) FOR(j,1,n) a[i][j]^=1,ans2=add(ans2,mul(i*j,1<<x));
ans2=del(ans2,mul(calc(),1<<x));
}
int main() {
read(n);FOR(i,1,n) FOR(j,1,n) read(in[i][j]);
FOR(i,0,30) solve(i);printf("%d %d\n",ans1,ans2);
return 0;
}
[LOJ3083] [GXOI2019] 与或和的更多相关文章
- [LOJ3083][GXOI/GZOI2019]与或和——单调栈
题目链接: [GXOI/GZOI2019]与或和 既然求的是二进制运算的和,那么我们按位考虑,这样就将矩阵变成了一个$01$矩阵. 对于或运算,就是求有多少个子矩形中有$1$. 直接求不好办,考虑有多 ...
- [LOJ3086] [GXOI2019] 逼死强迫症
题目链接 LOJ:https://loj.ac/problem/3086 洛谷:https://www.luogu.org/problemnew/show/P5303 Solution 显然不考虑\( ...
- GZOI/GXOI2019
陆陆续续做完了-- 与或和(单调栈) 这是一道一眼题-- 看到位运算,按位考虑贡献.对于每一位,将矩阵中的元素变为"当前元素的这一位是否为\(1\)",那么原矩阵变为\(01\)矩 ...
- 「ZJOI Day2」游记
Day-1 晚上一直在出自己做的模拟赛的T1,真的快要死掉了. 分类讨论几十种情况. 窝还是找了Bluesky大佬一起来验题,她瞬间就A掉了这一道题目...自闭了.. 诶,我还是太弱了. 之前教练组织 ...
- Codeforces Forethought Future Cup Elimination Round 选做
1146C Tree Diameter 题意 交互题.有一棵 \(n(n\le 100)\) 个点的树,你可以进行不超过 \(9\) 次询问,每次询问两个点集中两个不在同一点集的点的最大距离.求树的直 ...
随机推荐
- BMP文件格式详解(BMP file format)
BMP文件格式,又称为Bitmap(位图),或是DIB(Device-Independent Device,设备无关图),是windows系统中广泛使用的图片文件格式,由于它可以不作任何变换地址保存图 ...
- mysql distinct()函数 去重
mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...
- 15、基于consul+consul-template+registrator+nginx实现自动服务发现
一.架构图 二.组件介绍 1.Registrator Registrator:一个由Go语言编写的,针对docker使用的,通过检查本机容器进程在线或者停止运行状态,去注册服务的工具.所以我们要做的实 ...
- Monkey框架(测试方法篇) - monkey日志分析
Monkey日志分析是Monkey测试中非常重要的一个环节,通过日志分析,可以获取当前测试对象在测试过程中是否会发生异常,以及发生的概率,同时还可以获取对应的错误信息,帮助开发定位和解决问题.介绍日志 ...
- CentOS7安装JDK1.8图文教程
https://blog.csdn.net/weixin_42266606/article/details/80863781 1.下载 jdk 的 tar 包 网址:http://www.oracle ...
- swoole流程图
程图,便于以后回忆下 总结几点如下: 首先主进程监听pipe_master事件, 子进程监听pipe_worker事件 通过主进程派生的线程 swReactorThread *thread = swS ...
- FFmpeg av_seek_frame规律详解
本帖最后由 TangMonk 于 2016-7-27 10:26 编辑 1 av_seek_frame对视频进行跳转规律 1.1 flags参数 #define AVSEEK_FLAG_BACKWAR ...
- Flutter Dart List.map() 获取下标
class HomePageState extends State{ final topTitles = ['审批单', '机票列表', '客服']; final topIcons = ['asset ...
- Dockerfile实例
一.先看最简单的例子,定制nginx镜像,打印出 <h1>Hello, Docker!</h1> Dockerfile文件: FROM nginx RUN echo '< ...
- python读取yaml文件,在unittest中使用
python读取yaml文件使用,有两种方式: 1.使用ddt读取 2,使用方法读取ddt的内容,在使用方法中进行调用 1.使用ddt读取 @ddt.ddt class loginTestPage(u ...