1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include<map> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std;
#define fir first #define sec second const int N=1e5+10,MOD=998244353; typedef pair<int,int> pii;
inline int Add(const int &A,const int &B){return A+B>=MOD? A+B-MOD:A+B;} inline int Mul(const int &A,const int &B){return 1ll*A*B%MOD;}
map<int,int> las; map<pii,int> key;
int rol,col,n; vector<pii> sp; pii f[N<<4];
inline int Ri(){ register int a=0,b=1,c=getchar(); while(c<'0' || '9'<c) b=c=='-'? -1:b,c=getchar(); while('0'<=c && c<='9') a=(a<<1)+(a<<3)+c-'0',c=getchar(); return a*b; } inline bool cmp(pii A,pii B){return A.fir+A.sec==B.fir+B.sec? A.fir<B.fir:A.fir+A.sec>B.fir+B.sec;} inline pii Calc(int x,int y){ if(key.count(make_pair(x,y))) return make_pair(key[make_pair(x,y)],key[make_pair(x,y)]); pii key1(0,0),key2(0,0); if(las.count(x-y+1)) key1=f[las[x-y+1]]; if(las.count(x-y-1)) key2=f[las[x-y-1]]; return make_pair(min(key1.sec,key2.sec),max(key1.fir,key2.fir)); } inline int Model(const int &x){return (x%MOD+MOD)%MOD;} int main(){ rol=Ri(),col=Ri(),n=Ri(); for(int i=1;i<=n;i++){ int x=Ri(),y=Ri(),val=Ri(); key[make_pair(x,y)]=val; for(int p=0;p<=2 && x-p>0;p++) for(int q=0;q<=2 && y-q>0;q++) sp.push_back(make_pair(x-p,y-q)); } sort(sp.begin(),sp.end(),cmp); sp.erase(unique(sp.begin(),sp.end()),sp.end()); long long ans=0; for(int o=0,lo=sp.size();o<lo;o++){ int tag=sp[o].fir+sp[o].sec,beg=o; while(o+1<lo && sp[o+1].fir+sp[o+1].sec==tag) o++; for(int i=beg;i<=o;i++){ int x=sp[i].fir,y=sp[i].sec; if(las.count(x-y)) ans=Add(ans,Mul(Model(f[las[x-y]].fir),sp[las[x-y]].fir-x)); f[i]=Calc(x,y); las[x-y]=i; } } for(map<int,int>::iterator it=las.begin();it!=las.end();it++) ans=Add(ans,Mul(Model(f[it->sec].fir),min(sp[it->sec].fir,sp[it->sec].sec))); printf("%d\n",ans); return 0; }
|