link:https://code.mi.com/problem/list/view?id=139

题意:

  有一个1e6 * 1e6 大的格子,现在有两种操作:1,给一个子矩阵中的每个格子加上k。2,计算一个子矩阵中格子数字的和,在mod意义下除以子矩阵的大小。

思路:

  首先要学一下( http://www.cnblogs.com/RabbitHu/p/BIT.html)中关于二位矩阵区间修改,求区间和的知识,然后由于这个格子太大,我们就要用cdq分治降维。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
//#include <unordered_map>
/* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}
#define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b))
#define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/
const int maxn = 1e6+;
struct node{
int op,x,y;
ll val;
}a[maxn << ],tmp[maxn<<]; int lowbit(int x){
return x & (-x);
} struct bit{
ll sum[maxn]; void add(ll x,ll c){
while(x < maxn){
sum[x] = ((sum[x] + c)%mod + mod)%mod;
x += lowbit(x);
}
}
ll getsum(int x){
ll res = ;
while(x > ) {
res = ((res + sum[x])% mod + mod)%mod;
x -= lowbit(x);
}
return res;
}
}A,B,C,D; queue<int>que;
ll ans[maxn],sz[maxn]; void update(int x,int y,ll val){
A.add(y, (val%mod + mod )%mod);
B.add(y, (val*x%mod + mod )% mod);
C.add(y, (val*y%mod + mod) % mod);
D.add(y, (val*x%mod*y%mod+mod)%mod);
} ll solve(int x, int y){
ll res = ;
res = (res + 1ll*(x+) * (y+) % mod * A.getsum(y)%mod )%mod;
res = (res - 1ll*(y+) * B.getsum(y))%mod;
res = (res - 1ll*(x+) * C.getsum(y))%mod;
res = (res + D.getsum(y))%mod;
res = (res + mod)%mod;
return res;
}
void cdq(int le,int ri){
if(le >= ri) return;
int mid = (le + ri) >> ;
cdq(le, mid); cdq(mid+, ri); int p = le,q = mid+;
int tot = ;
while(p <= mid && q <= ri) {
if(a[p].x <= a[q].x){
if(a[p].op == ) {
update(a[p].x, a[p].y,a[p].val);
que.push(p);
}
tmp[++tot] = a[p++];
}
else {
if(a[q].op == ) {
ans[a[q].val] = (ans[a[q].val] + solve(a[q].x, a[q].y) ) % mod;
}
else if(a[q].op == ) {
ans[a[q].val] = ((ans[a[q].val] - solve(a[q].x, a[q].y) ) ) % mod;
if(ans[a[q].val]< ) ans[a[q].val] = (ans[a[q].val]+mod)%mod;
}
tmp[++tot] = a[q++];
}
} while(p <= mid) tmp[++tot] = a[p++];
while(q <= ri){
if(a[q].op == ) {
ans[a[q].val] = (ans[a[q].val] + solve(a[q].x, a[q].y) ) % mod;
}
else if(a[q].op == ) {
ans[a[q].val] = ((ans[a[q].val] - solve(a[q].x, a[q].y) ) ) % mod;
if(ans[a[q].val]< ) ans[a[q].val] = (ans[a[q].val]+mod)%mod;
}
tmp[++tot] = a[q++];
}
while(!que.empty()) {
int p = que.front(); que.pop();
update(a[p].x, a[p].y,-1ll*a[p].val);
}
rep(i, , tot) a[i+le-] = tmp[i];
}
ll ksm(ll a, ll n){
ll res = ;
while(n > ){
if(n & ) res = res * a % mod;
a = a * a % mod;
n>>=;
}
return res;
}
int main(){
int n,m,q;
scanf("%d%d%d", &n, &m, &q);
int tot = ,id = ;
while(q--) {
int op; scanf("%d", &op);
if(op == ) {
int x1,y1,x2,y2,k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
tot++; a[tot].x = x1; a[tot].y = y1; a[tot].val = k;a[tot].op = ;
tot++; a[tot].x = x1; a[tot].y = y2+; a[tot].val = -k;a[tot].op = ;
tot++; a[tot].x = x2+; a[tot].y = y1; a[tot].val = -k;a[tot].op = ;
tot++; a[tot].x = x2+; a[tot].y = y2+; a[tot].val = k;a[tot].op = ;
}
else {
int x1,y1,x2,y2;
id++;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
tot++; a[tot].x = x1-; a[tot].y = y1-; a[tot].val = id; a[tot].op = ;
tot++; a[tot].x = x1-; a[tot].y = y2; a[tot].val = id; a[tot].op = ;
tot++; a[tot].x = x2; a[tot].y = y1-; a[tot].val = id; a[tot].op = ;
tot++; a[tot].x = x2; a[tot].y = y2; a[tot].val = id;a[tot].op = ;
sz[id] = 1ll*(y2-y1+)*(x2-x1+)%mod;
}
} cdq(, tot); rep(i, , id) {
printf("%lld\n", 1ll*ans[i] * ksm(sz[i], mod-)%mod);
}
return ;
}

小米 OJ 编程比赛 02 月常规赛 3 Logic Gatekeeper CDQ分治的更多相关文章

  1. 小米 OJ 编程比赛 02 月常规赛

    Carryon 数数字 描述 Carryon 最近迷上了数数字,然后 Starry 给了他一个区间[l,r] ,然后提了几个要求, 需要将 ll 到 rr 之间的数全部转化成 16 进制,然后连起来. ...

  2. 小米 OJ 编程比赛 01 月常规赛_灯_找规律

     灯 序号:#125难度:有挑战时间限制:1000ms内存限制:32M 描述 一个屋子有 n 个开关控制着 n 盏灯,但奇怪的是,每个开关对应的不是一盏灯,而是 n-1 盏灯,每次按下这个开关,其对应 ...

  3. 小米 OJ 编程比赛 03 月常规赛

    A.数学等式 数据比较小,可以暴力+折半枚举. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(in ...

  4. 小米 oj 硬币比赛(思维+动态规划)

     硬币比赛 序号:#47难度:困难时间限制:1000ms内存限制:10M 描述 有 n 个不同价值的硬币排成一条线.有 A 与 B 两个玩家,指定由 A 开始轮流(A 先手,然后 B,然后再 A..) ...

  5. 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板

    第10单元 - 单元作业:OJ编程 - 创建数组类模板 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...

  6. iPad mini Retina越狱小结【2014年02月06日 - 初稿】

    Update History 2014年02月06日 - 初稿 0.引言 本来一直都没有苹果的产品除了第一代的iPod(没怎么使用最后大学送人了 @李清纯(255270520) ,巧合的是老妈学校发了 ...

  7. CSDN挑战编程——《金色十月线上编程比赛第二题:解密》

    金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...

  8. java:编程比赛中有用的方法整理(一)数组

    我曾经参加过几次编程比赛,但是当时用的是c语言,现在学习了java,打算专攻java组,故以此整理. 数组无论在哪里都必不可少. 一.数组的拷贝: 使用Arrays类的copyOf方法: 1.将一个数 ...

  9. [小米OJ] 10. 爬楼梯

    dp 另: 小米oj上的测试样例是错的 ; ) function solution(line) { if (line == 0) return 0; if (line == 1) return 1; ...

随机推荐

  1. Angular JS 中的内置方法之表单验证

    angular js 结合html5 可以实现强大的表单验证功能 关闭html5自带的表单验证功能可以用

  2. Docker 容器高级操作[Docker 系列-3]

    关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 上篇文章向读者介绍了一个 Nginx 的例子,对于 Nginx 这样一个容器而言,当它启动成功后,我们 ...

  3. SQLServer2000同步复制技术实现步骤

    SQLServer2000同步复制技术实现步骤 一. 预备工作 1.发布服务器,订阅服务器都创建一个同名的windows用户,并设置相同的密码,做为发布快照文件夹的有效访问用户 --管理工具 --计算 ...

  4. JSON合并,并按时间排序

    mergeJson: function (json1, json2) { var json = Object.assign([], json1, json2); return json.sort(fu ...

  5. 【Java例题】7.5 文件题2-学生成绩统计

    5.学生成绩统计.已有一个学生成绩文件,含有多位学生的各三门课的成绩:读取这个文件中的每位学生的三门课成绩,然后计算均分:最后对这些均分按照大于或小于75分的界限,分别写到另两个文件中. packag ...

  6. Java下载文件方法

    public static void download(String path, HttpServletResponse response) { try { // path是指欲下载的文件的路径. F ...

  7. 【linux】【qt5界面】【系统托盘图标的实现】

    前言: 博主最近在做一个聊天软件,虽然技术不咋滴,但遇到点干货肯定是要跟大家分享的啦.下面就给大家分享一个qt实现程序隐藏才系统托盘的技巧. 装备: 系统:linux, qt版本:5.9.2,GCC: ...

  8. spring架构解析--入门一

    Spring 框架中的核心组件只有三个:Core.Context 和 Beans.它们构建起了整个 Spring 的骨骼架构.简单理解: spring core是工具,context是环境,而bean ...

  9. cs231n--详解卷积神经网络

    原版地址:http://cs231n.github.io/convolutional-networks/ 知乎翻译地址:https://zhuanlan.zhihu.com/p/22038289?re ...

  10. 写论文的第三天 自建zookeeper集群

    日志___2019.1.25 基于hadoop集群搭建zookeeper集群 Filezilla上传zookeeper压缩包到主节点 安装zookeeper到/usr/local目录 命令:tar – ...