\(\mathcal{Description}\)

  Link.

  给定序列 \(\{a_n\}\),定义一次操作为:

  • 选择 \(a_i<a_j\),以及一个 \(x\in\mathbb R_+\),使得 \(a_i+x\le a_j-x\);
  • 令 \(a_i\leftarrow a_i+x,a_j\leftarrow a_j-x\),本次操作的得分为 \(x\)。

  定义序列的得分为进行任意次操作能得到的最大得分和,现给定 \(m\) 次形如 \(a_x\leftarrow y\) 的修改操作,在每次修改操作后求出当前序列的得分。

\(\mathcal{Solution}\)

  定义序列的势能 \(\Phi=\sum_{i<j}|a_i-a_j|\),设进行一次操作后得到新势能 \(\Phi'\),显然有 \(\Phi'-\Phi\le-2x\),其中 \(x\) 即操作时所选取的正数。同时,取等条件容易看出为 \(\not\exist a_k,a_k\in(a_i,a_j)\),由此,又能得到只要 \(\Phi>0\),我们必然能以势能减少 \(2x\) 的代价增加 \(x\) 分。所以答案就是 \(\frac{\Phi}{2}\)。

  选用平衡树来动态维护 \(\Phi\) 即可支持修改。复杂度 \(\mathcal O((n+m)\log n)\)。

\(\mathcal{Code}\)

/*~Rainybunny~*/

#include <bits/stdc++.h>

#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i ) typedef long long LL; const int MAXN = 6e5, MOD = 998244353, INV2 = 499122177;
int n, m, root, a[MAXN + 5]; inline int mul( const int u, const int v ) { return 1ll * u * v % MOD; }
inline int sub( int u, const int v ) { return ( u -= v ) < 0 ? u + MOD : u; }
inline int add( int u, const int v ) { return ( u += v ) < MOD ? u : u - MOD; } struct Treap {
int node, root, ch[MAXN + 5][2], aux[MAXN + 5], val[MAXN + 5];
int siz[MAXN + 5], sum[MAXN + 5];
Treap() { srand( 20120712 ); } inline int newnd( const int v ) {
++node, aux[node] = rand(), sum[node] = val[node] = v, siz[node] = 1;
return node;
} inline void pushup( const int u ) {
siz[u] = siz[ch[u][0]] + siz[ch[u][1]] + 1;
sum[u] = add( add( sum[ch[u][0]], sum[ch[u][1]] ), val[u] );
} inline int merge( const int u, const int v ) {
if ( !u || !v ) return u | v;
if ( aux[u] < aux[v] ) {
return ch[u][1] = merge( ch[u][1], v ), pushup( u ), u;
} else {
return ch[v][0] = merge( u, ch[v][0] ), pushup( v ), v;
}
} inline void vsplit( const int u, const int k, int& x, int& y ) {
if ( !u ) return void( x = y = u );
if ( val[u] <= k ) vsplit( ch[u][1], k, ch[x = u][1], y ), pushup( x );
else vsplit( ch[u][0], k, x, ch[y = u][0] ), pushup( y );
} inline int erase( const int v ) {
int x, y, z; vsplit( root, v, x, z );
int ret = add( sub( mul( sub( siz[x], siz[z] ), v ), sum[x] ), sum[z]);
vsplit( x, v - 1, x, y ), y = merge( ch[y][0], ch[y][1] );
return root = merge( x, merge( y, z ) ), ret;
} inline int insert( const int v ) {
int x, y = newnd( v ), z; vsplit( root, v, x, z );
int ret = add( sub( mul( sub( siz[x], siz[z] ), v ), sum[x] ), sum[z]);
return root = merge( x, merge( y, z ) ), ret;
}
} trp; int main() {
scanf( "%d %d", &n, &m );
int ans = 0;
rep ( i, 1, n ) scanf( "%d", &a[i] ), ans = add( ans, trp.insert( a[i] ) );
for ( int x, v; m--; ) {
scanf( "%d %d", &x, &v );
ans = sub( ans, trp.erase( a[x] ) );
ans = add( ans, trp.insert( a[x] = v ) );
printf( "%d\n", mul( ans, INV2 ) );
}
return 0;
}

Solution -「ARC 126E」Infinite Operations的更多相关文章

  1. Solution -「ARC 104E」Random LIS

    \(\mathcal{Description}\)   Link.   给定整数序列 \(\{a_n\}\),对于整数序列 \(\{b_n\}\),\(b_i\) 在 \([1,a_i]\) 中等概率 ...

  2. Solution -「ARC 101D」「AT4353」Robots and Exits

    \(\mathcal{Description}\)   Link.   有 \(n\) 个小球,坐标为 \(x_{1..n}\):还有 \(m\) 个洞,坐标为 \(y_{1..m}\),保证上述坐标 ...

  3. Solution -「ARC 110D」Binomial Coefficient is Fun

    \(\mathcal{Description}\)   Link.   给定非负整数序列 \(\{a_n\}\),设 \(\{b_n\}\) 是一个非负整数序列且 \(\sum_{i=1}^nb_i\ ...

  4. Solution -「ARC 124E」Pass to Next

    \(\mathcal{Description}\)   Link.   有 \(n\) 个人站成一个环,初始时第 \(i\) 个人手里有 \(a_i\) 个球.第 \(i\) 个人可以将自己手中任意数 ...

  5. Solution -「ARC 126F」Affine Sort

    \(\mathcal{Description}\)   Link.   给定 \(\{x_n\}\),令 \[f(k)=\left|\{(a,b,c)\mid a,b\in[0,c),c\in[1,k ...

  6. Solution -「ARC 125F」Tree Degree Subset Sum

    \(\mathcal{Description}\)   Link.   给定含有 \(n\) 个结点的树,求非负整数对 \((x,y)\) 的数量,满足存在 \(\exist S\subseteq V ...

  7. Solution -「ARC 125E」Snack

    \(\mathcal{Description}\)   Link.   把 \(n\) 种零食分给 \(m\) 个人,第 \(i\) 种零食有 \(a_i\) 个:第 \(i\) 个人得到同种零食数量 ...

  8. Solution -「ARC 058C」「AT 1975」Iroha and Haiku

    \(\mathcal{Description}\)   Link.   称一个正整数序列为"俳(pái)句",当且仅当序列中存在连续一段和为 \(x\),紧接着连续一段和为 \(y ...

  9. Solution -「ARC 101E」「AT 4352」Ribbons on Tree

    \(\mathcal{Description}\)   Link.   给定一棵 \(n\) 个点的树,其中 \(2|n\),你需要把这些点两两配对,并把每对点间的路径染色.求使得所有边被染色的方案数 ...

随机推荐

  1. Pandas系列(十七)-EDA(pandas-profiling)

    对于探索性数据分析来说,做数据分析前需要先看一下数据的总体概况,pandas_profiling工具可以快速预览数据. 安装 pip install pandas-profiling 使用 impor ...

  2. INFO client.RMProxy: Connecting to ResourceManager at hadoop

    1.查看防火墙是否没关闭. 2.用jps 命令查看是否没有启动resourcemanager

  3. vue学习17-插槽作用域

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta http ...

  4. Servlet三种创建方式

    直接实现 Servlet 接口不太方便,所以 Servlet 又内置了两个 Servlet 接口的实现类(抽象类),分别为 GenericServlet 和 HttpServlet,因此,创建 Ser ...

  5. golang中将函数当做函数参数使用

    package main import ( "fmt" "strings" ) // 使用type关键字让函数变成一个自定义类型 type caseFunc f ...

  6. AOP-底层原理

    AOP(底层原理) 1,AOP底层使用动态代理 (1)有两种情况动态代理 第一种 有接口情况,使用JDK动态代理 *创建接口实现类代理对象,增强类的方法 第二种 无接口情况,使用CGLIB动态代理 * ...

  7. 字的研究(2)Fonttools-字体文件的解析

    前言 本文主要介绍如果使用Python第三方库fontTools对TrueType字体文件(指使用TrueType描述轮廓的OpenType字体文件)的解析.修改和创建等操作. fontTools简介 ...

  8. mysql加强(3)~分组(统计)查询

    一.分组(统计) 查询 1.语法 : select [distinct] *| 分组字段1[别名] [,分组字段2[别名],...] | 统计函数 from 表名 [别名] [where 条件(s)] ...

  9. K8S SVC 转发原理

    在前面的文章中,我们已经多次使用到了 Service 这个 Kubernetes 里重要的服务对象.而 Kubernetes 之所以需要 Service,一方面是因为 Pod 的 IP 不是固定的,另 ...

  10. python 小兵内置函数进制转换

    Python内置函数进制转换的用法 使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Conve ...