小米 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> 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<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 998244353; const double esp = 1e-8; 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=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',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+9; struct node{ int op,x,y; ll val; }a[maxn << 2],tmp[maxn<<2]; 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 = 0; while(x > 0) { 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 = 0; res = (res + 1ll*(x+1) * (y+1) % mod * A.getsum(y)%mod )%mod; res = (res - 1ll*(y+1) * B.getsum(y))%mod; res = (res - 1ll*(x+1) * 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) >> 1; cdq(le, mid); cdq(mid+1, ri); int p = le,q = mid+1; int tot = 0; while(p <= mid && q <= ri) { if(a[p].x <= a[q].x){ if(a[p].op == 1) { update(a[p].x, a[p].y,a[p].val); que.push(p); } tmp[++tot] = a[p++]; } else { if(a[q].op == 2) { ans[a[q].val] = (ans[a[q].val] + solve(a[q].x, a[q].y) ) % mod; } else if(a[q].op == 3) { ans[a[q].val] = ((ans[a[q].val] - solve(a[q].x, a[q].y) ) ) % mod; if(ans[a[q].val]< 0) 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 == 2) { ans[a[q].val] = (ans[a[q].val] + solve(a[q].x, a[q].y) ) % mod; } else if(a[q].op == 3) { ans[a[q].val] = ((ans[a[q].val] - solve(a[q].x, a[q].y) ) ) % mod; if(ans[a[q].val]< 0) 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, 1, tot) a[i+le-1] = tmp[i]; } ll ksm(ll a, ll n){ ll res = 1; while(n > 0){ if(n & 1) res = res * a % mod; a = a * a % mod; n>>=1; } return res; } int main(){ int n,m,q; scanf("%d%d%d", &n, &m, &q); int tot = 0,id = 0; while(q--) { int op; scanf("%d", &op); if(op == 1) { 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 = 1; tot++; a[tot].x = x1; a[tot].y = y2+1; a[tot].val = -k;a[tot].op = 1; tot++; a[tot].x = x2+1; a[tot].y = y1; a[tot].val = -k;a[tot].op = 1; tot++; a[tot].x = x2+1; a[tot].y = y2+1; a[tot].val = k;a[tot].op = 1; } else { int x1,y1,x2,y2; id++; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); tot++; a[tot].x = x1-1; a[tot].y = y1-1; a[tot].val = id; a[tot].op = 2; tot++; a[tot].x = x1-1; a[tot].y = y2; a[tot].val = id; a[tot].op = 3; tot++; a[tot].x = x2; a[tot].y = y1-1; a[tot].val = id; a[tot].op = 3; tot++; a[tot].x = x2; a[tot].y = y2; a[tot].val = id;a[tot].op = 2; sz[id] = 1ll*(y2-y1+1)*(x2-x1+1)%mod; } } cdq(1, tot); rep(i, 1, id) { printf("%lld\n", 1ll*ans[i] * ksm(sz[i], mod-2)%mod); } return 0; }
skr