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. jboss反序列化漏洞复现(CVE-2017-7504)

    jboss反序列化漏洞复现(CVE-2017-7504) 一.漏洞描述 Jboss AS 4.x及之前版本中,JbossMQ实现过程的JMS over HTTP Invocation Layer的HT ...

  2. Could not launch "APP_NAME" process launch failed: 4294967295

    真机调试忽然遇到这个问题, Could not launch "APP_NAME" process launch failed: 如图所示: 模拟器上能正常调试………… 这个问题还 ...

  3. Go slice:切片的“陷阱”和本质

    文章说明 总结了go语言中切片slice的特殊性和使用时的注意事项. 个人理解,不足之处欢迎指出. slice:切片,是go语言中一种常用的数据结构,基于数组构建,表示相同数据类型的集合. 数组 Go ...

  4. 什么是https?http升级为https需要什么?

    一.什么是https? https是一种加密传输协议,网站使用https后可以避免敏感信息被第三方获取.https加密协议=SSL / TLS+http协议,也就是说,在传统的http协议上加上SSL ...

  5. 商贸型企业 Java 收货 + 入库 + 生成付款单

    package cn.hybn.erp.modular.system.service.impl; import cn.hybn.erp.core.common.page.LayuiPageFactor ...

  6. 客户端埋点实时OLAP指标计算方案

    背景 产品经理想要实时查询一些指标数据,在新版本的APP上线之后,我们APP的一些质量指标,比如课堂连接掉线率,课堂内崩溃率,APP崩溃率等指标,以此来看APP升级之后上课的体验是否有所提升,上课质量 ...

  7. Winform DataGridView 取消默认选中行

    困境 网上有很多解决方法,可是很多读者照做并不生效.追究其原因,问题出现在许多博主没有搞清楚DataGridView绑定与当前触发事件的关系. 复现 private void Frm_Load(obj ...

  8. 利用jQuery中的serialize方法大量获取页面中表单的数据,发送的服务器

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. 如何使用dmidecode命令查看硬件信息

    引言 当我们需要获取机器硬件信息时,可使用linux系统自带的dmidecode工具进行查询. dmidecode命令通过读取系统DMI表,显示服务器硬件和BIOS信息.除了可使用dmidecode查 ...

  10. SQLServer数据库处于恢复挂起状态的解决办法

    一.总结 如果数据库处于一个恢复挂起的状态,并且对数据库做脱机和分离的操作,报出数据库文件不可访问的错误,可能是因为数据库的数据文件和日志文件在数据库正常连接的情况下,文件所在的磁盘脱机了,导致数据库 ...