列表

详情


NC213954. RikkawithStorehouse

描述

On activities held by Algorithm Association, lemon teas are provided to the participants almost infinitely. To store such a large number of lemon teas, Rikka wants to build some storehouses.
Rikka plans to build storehouses, together with N-1 bidirectional roads. The i-th road connects the (i+1)-th storehouse and the -th storehouse.
These storehouses will be built on an uneven piece of land. For each , the altitude of the i-th storehouse is known to be a_i. Rikka can choose the altitudes for other storehouses arbitrarily. (The value of altitude can be any real number).

Carrying lemon teas on a steep hill is difficult. Rikka wants to make the road system as convenient as possible. Therefore, Rikka wants to minimize the square sum of the altitude difference of each road, i.e.

Due to the geological movement, the altitude of the last storehouses often changes. Lucky, it is free for Rikka to change the altitude of the first storehouses at any time.
Your task is to help Rikka to find out the optimal plan after each change.

输入描述

The first line contains two integers .
The second line contains integers , representing in order, where .
Then m lines follow, each line with two integers , representing the altitude of the x_i-th storehouse is changed to w_i 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 a_1=2 and the value of ans is (2-1)^2 + (2-3)^2 = 2.

示例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());
	}
}

上一题