NC213954. RikkawithStorehouse
描述
输入描述
The first line contains two integers.
The second line containsintegers
, representing
in order, where
.
Then m lines follow, each line with two integers, representing the altitude of the
-th storehouse is changed to
during the i-th movement.
输出描述
Output m+1 lines, each with a single number: the initial value of ans followed by the value of ans after each movement.
Writing a special judge is a tiring task. Therefore, you are required to output the answer module 998244353. Formally, if the simplest fraction representation of ans is, you need to output
.
示例1
输入:
2 0 1 3
输出:
2
说明:
For the first sample, one optimal solution is示例2
输入:
3 3 1 2 3 4 6 4 5 4 4 4
输出:
332748120 83187032 748683270 0
C++(clang++11) 解法, 执行用时: 684ms, 内存消耗: 10160K, 提交时间: 2020-11-29 23:00:02
#include<bits/stdc++.h> using namespace std; const int maxn=1<<19,mod=998244353; int add(int x,int y){ x+=y;return x>=mod?x-mod:x; } int mul(int x,int y){ return 1ll*x*y%mod; } int qpow(int x,int y){ int ans=1; while(y){ if(y&1)ans=mul(ans,x); x=mul(x,x),y>>=1; } return ans; } struct data{ int a,b,c; friend data operator+(const data&x,const data&y){ int A=add(add(x.a,y.a),1); int B=add(x.b,y.b); int C=add(x.c,y.c); data z; int i4A=qpow(mul(A,4),mod-2); z.a=add(1,mod-mul(4,i4A)); z.b=mul(mul(4,B),i4A); z.c=add(C,mod-mul(B,mul(B,i4A))); return z; } }tr[maxn<<2]; int n,h[maxn<<2],N,m; int get(){ int A=add(tr[2].a,tr[3].a); int B=add(tr[2].b,tr[3].b); int C=add(tr[2].c,tr[3].c); return mul(add(mul(4,mul(A,C)),mod-mul(B,B)),qpow(mul(4,A),mod-2)); } int main(){ cin>>n>>m; N=1<<n; for(int i=1<<n-1;i<N;i++) scanf("%d",&h[i]),tr[i]=(data){1,mod-add(h[i],h[i]),mul(h[i],h[i])}; for(int i=(1<<n-1)-1;i;i--) tr[i]=tr[i<<1]+tr[i<<1|1]; printf("%d\n",get()); for(int i=1;i<=m;i++){ int x,w; scanf("%d%d",&x,&w); h[x]=w; tr[x]=(data){1,mod-add(h[x],h[x]),mul(h[x],h[x])}; x>>=1; while(x)tr[x]=tr[x<<1]+tr[x<<1|1],x>>=1; printf("%d\n",get()); } }