小米 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; ...
随机推荐
- python整形及浮点型求余数的区别
1.代码如下 a=7.0b=4.0c=7e=4 #整形求余print("%d/%d=%d" %(c,e,c/e)) #将浮点型强制转换为整形,余数用浮点型表示print(" ...
- Flink状态专题:keyed state和Operator state
众所周知,flink是有状态的计算.所以学习flink不可不知状态. 正好最近公司有个需求,要用到flink的状态计算,需求是这样的,收集数据库新增的数据. ...
- .NETCoreCSharp 中级篇2-3 Linq简介
.NETCoreCSharp 中级篇2-3 本节内容为Linq及其拓展方法.Linq中表达式树的使用 简介 语言集成查询(LINQ)是一系列直接将查询功能集成到C#语言的技术统称.数据查询历来都表示为 ...
- Spring cloud Feign不支持对象传参解决办法[完美解决]
spring cloud 使用 Feign 进行服务调用时,不支持对象参数. 通常解决方法是,要么把对象每一个参数平行展开,并使用 @RequestParam 标识出每一个参数,要么用 @Reques ...
- Java学习多线程第一天
内容介绍 Thread 线程创建 线程池 线程状态图 1 多线程 1.1 多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念. 进程:进程指正在运行的程序.确切的来说,当一个程序 ...
- Appium+python自动化(二十九)- 模拟手指在手机上多线多点作战 - 多点触控(超详解)
简介 在网页中我们经常使用缩放操作来便利的查看具体的信息,在appium中使用MultiAction多点触控的类来实现.MultiAction是多点触控的类,可以模拟用户多点操作.主要包含加载add( ...
- dotnetcore 与 hbase 之二——thrift 客户端的制作
说明 在上一篇文章dotnetcore 与 hbase 之一--hbase 环境准备结束后,我们已经有了 hbase 数据库环境.接下来就可以利用 thrift 生成 c# hbase 客户端了.如果 ...
- android——卡片式布局
一.CardView <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk ...
- Unity进阶之ET网络游戏开发框架 01-下载、运行
版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...
- 解决 Nginx 代理Apex慢的问题
前不久用 Nginx 代理 Oracle 的 Apex 速度非常慢,我之前Nginx 配置如下: server{ listen 80; server_name localhost; client_ma ...