小米 OJ 编程比赛 02 月常规赛 3 Logic Gatekeeper CDQ分治
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分治的更多相关文章
- 小米 OJ 编程比赛 02 月常规赛
Carryon 数数字 描述 Carryon 最近迷上了数数字,然后 Starry 给了他一个区间[l,r] ,然后提了几个要求, 需要将 ll 到 rr 之间的数全部转化成 16 进制,然后连起来. ...
- 小米 OJ 编程比赛 01 月常规赛_灯_找规律
灯 序号:#125难度:有挑战时间限制:1000ms内存限制:32M 描述 一个屋子有 n 个开关控制着 n 盏灯,但奇怪的是,每个开关对应的不是一盏灯,而是 n-1 盏灯,每次按下这个开关,其对应 ...
- 小米 OJ 编程比赛 03 月常规赛
A.数学等式 数据比较小,可以暴力+折半枚举. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(in ...
- 小米 oj 硬币比赛(思维+动态规划)
硬币比赛 序号:#47难度:困难时间限制:1000ms内存限制:10M 描述 有 n 个不同价值的硬币排成一条线.有 A 与 B 两个玩家,指定由 A 开始轮流(A 先手,然后 B,然后再 A..) ...
- 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板
第10单元 - 单元作业:OJ编程 - 创建数组类模板 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...
- iPad mini Retina越狱小结【2014年02月06日 - 初稿】
Update History 2014年02月06日 - 初稿 0.引言 本来一直都没有苹果的产品除了第一代的iPod(没怎么使用最后大学送人了 @李清纯(255270520) ,巧合的是老妈学校发了 ...
- CSDN挑战编程——《金色十月线上编程比赛第二题:解密》
金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...
- java:编程比赛中有用的方法整理(一)数组
我曾经参加过几次编程比赛,但是当时用的是c语言,现在学习了java,打算专攻java组,故以此整理. 数组无论在哪里都必不可少. 一.数组的拷贝: 使用Arrays类的copyOf方法: 1.将一个数 ...
- [小米OJ] 10. 爬楼梯
dp 另: 小米oj上的测试样例是错的 ; ) function solution(line) { if (line == 0) return 0; if (line == 1) return 1; ...
随机推荐
- 【MySQL】java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column
问题原因: 输入内容包含特殊字符,MySQL 中的列不支持. 解决方法 多数是修改 MySQL 的数据库和表结构,CHARSET 改为 utf8mb4,但本人测试还是不能传入 emoji. 后来在代码 ...
- 【iOS】PLA 3.3.12
发件人 Apple Program License Agreement PLA We found that your app uses the Advertising Identifier but d ...
- CodeForces 372 A. Counting Kangaroos is Fun
题意,有n只袋鼠,没每只袋鼠有个袋子,大小为si,一个袋鼠可以进入另外一个袋鼠的袋子里面,当且仅当另一个袋鼠的袋子是他的二倍或二倍一上,然后中国袋鼠就是不可见的,不能出现多个袋鼠嵌套的情况.让你求最少 ...
- UE4 游戏模块初始化顺序
最近看教学,有个讲解UE4初始化顺序的,记录一下. 首先创建一个Actor,Character,GameInstance,GameMode,LevelScriptActor(关卡),PlayerCon ...
- 从windows10迁移到Linux Deepin
如题, 这几天从windows系统迁移到deepin的linux系统花了很多时间, 以致最近都没时间来博客园.现在将这几天的成果分享出来, 顺便也做个记录.先不多说, 上一张新系统界面. 其实在装de ...
- .c和.h文件的区别
.h文件(头文件): 一般写一些函数声明.宏定义.结构体等内容. 其实就是将各个.c文件中重复的声明.宏定义.结构体,枚举变量等提取出来,放进一个新的文件中,便于其他.c文件共享这部分的代码,同时也方 ...
- 夯实Java基础(九)——final关键字
1.前言 Java语言中的final关键字,想必大家都不是很陌生,我们自己用的最多的应该是用来定义常量吧,那么今天我们就来了解final这个关键字的用法,这个关键字还是非常简单的. final从字面意 ...
- kubernetes离线包分析
k8s离线包解析 产品地址 鸣谢 大家好,首先感谢大家对我们产品的支持,特别是一些老客户的持续支持,让我可以有动力把这个事情持续进行下去. 感谢大家对付费产品的认可,尊重付费 产品介绍 我们专注于k8 ...
- Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F
传送门 A 贪心的取 每个字母n/k次 令r=n%k 让前r个字母各取一次 #include <bits/stdc++.h> using namespace std; typedef lo ...
- Linux系统关机与重新引导流程简介
引言 在<Linux启动之旅>中,我们了解了Linux的启动过程,下面我们一同来学习Linux关机与重新引导流程. 不同于桌面系统,作为服务器,我们较少对Linux系统进行系统重启,但在以 ...