题目链接:https://code.mi.com/problem/list/view?id=139&cid=7
思路:cdq分治离线直接做,需要二维树状数组区间修改,区间查询的知识http://www.cnblogs.com/RabbitHu/p/BIT.html附上相关资料。
#pragma GCC optimize(2)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-8
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
ll n,m,q,ans[100008],x,y,xz,yz,op,z,k,b[100008],d[500008],fd[100008];
struct as{
ll op,x,y,ans;}c[500008],e[500008];
struct data
{
ll f[1000008];
void add(ll x,ll y,ll z) {for(ll j=y;j<=m;j+=j&-j) f[j]+=z,f[j]%=mod;}
ll sum(ll x,ll y)
{
ll ans=0;
for(ll j=y;j;j-=j&-j) ans+=f[j],ans%=mod;
return ans;
}
}A,B,C,D;
void modify(ll x,ll y,ll z) {A.add(x,y,z),B.add(x,y,((x*z)%mod)),C.add(x,y,((y*z)%mod)),D.add(x,y,(((x*y%mod)*z)%mod));}
ll solve(ll x ,ll y) {return (((((x+1)*(y+1)%mod)*A.sum(x,y))%mod)-(((y+1)*B.sum(x,y))%mod)-(((x+1)*C.sum(x,y))%mod)+D.sum(x,y))%mod;}
bool cmp(as a,as b)
{
if(a.x==b.x) return a.op<b.op;
return a.x<b.x;
}
ll gmod(ll a,ll b)
{
ll res=1;
while(b)
{
if(b&1) res=res*a%mod;
b>>=1,a=a*a%mod;
}
return res;
}
void cdq(int l,int r)
{
if(l==r) return;
int mid=(l+r)>>1;
cdq(l,mid),cdq(mid+1,r);
int zz=0,qw=0;
FOR(i,l,mid) if(c[i].op==1) e[++zz]=c[i];
FOR(i,mid+1,r) if(c[i].op!=1) e[++zz]=c[i];
sort(e+1,e+zz+1,cmp);
FOR(i,1,zz)
{
if(e[i].op==1) {modify(e[i].x,e[i].y,e[i].ans),d[++qw]=i;continue;}
else if(e[i].op==2) ans[e[i].ans]+=solve(e[i].x,e[i].y);
else ans[e[i].ans]-=solve(e[i].x,e[i].y);
ans[e[i].ans]%=mod;
}
FOR(i,1,qw) modify(e[d[i]].x,e[d[i]].y,-e[d[i]].ans);
}
int main()
{
cin.tie(0);
cout.tie(0);
cin>>n>>m>>q;
int zz=0,qw=0;
FOR(i,1,q)
{
sl(op),sl(x),sl(y),sl(xz),sl(yz);
if(op==1)
{
sl(z);
c[++qw]=as{1,x,y,z};
c[++qw]=as{1,x,yz+1,-z};
c[++qw]=as{1,xz+1,y,-z};
c[++qw]=as{1,xz+1,yz+1,z};
}
else
{
k=(xz-x+1)*(yz-y+1)%mod;
fd[i]=1;
b[i]=gmod(k,mod-2);
c[++qw]=as{2,xz,yz,i};
c[++qw]=as{2,x-1,y-1,i};
c[++qw]=as{3,xz,y-1,i};
c[++qw]=as{3,x-1,yz,i};
}
}
cdq(1,qw);
ll s;
FOR(i,1,q)
{
if(!fd[i]) continue;
s=(((ans[i]*b[i])%mod)+mod)%mod;
printf("%lld\n",s);
}
return 0;
}