P6108 [Ynoi2009] rprsvq

$\text{res}=\sum_{\empty\sub S\subseteq[l,r]}\frac{1}{|S|}\sum_{a_i\in S}(a_i-\frac{1}{|S|}\sum_{a_i\in S}a_i)^2\\=\sum_{\empty\sub S\subseteq[l,r]}\frac{1}{|S|}(\sum_{a_i\in S}a_i^2-\sum_{a_i\in S}\frac{2a_i}{|S|}\sum_{a_i\in S}a_i+\frac{1}{|S|}(\sum_{a_i\in S}a_i)^2)\\ =\sum_{\empty\sub S\subseteq[l,r]}\frac{1}{|S|}(\sum_{a_i\in S}a_i^2-\frac{1}{|S|}(\sum_{a_i\in S}a_i)^2)\\ =\text{res1}-\text{res2}\\$

$\text{res1}=\sum_{a_i\in[l,r]}\sum_{j=0}^{r-l}\binom{r-l}{j}\frac{a_i^2}{j+1}\\=\sum_{j=0}^{r-l}\binom{r-l}{j}\frac{1}{j+1}\sum_{a_i\in[l,r]}a_i^2\\ =\frac{1}{r-l+1}\sum_{j=1}^{r-l+1}\binom{r-l+1}{j}\sum_{a_i\in[l,r]}a_i^2\\ =\frac{1}{r-l+1}(2^{r-l+1}-1)\sum_{a_i\in[l,r]}a_i^2\\$

$\text{res2}=\sum_{\empty\sub S\subseteq[l,r]}(\frac{1}{|S|}\sum_{a_i\in S}a_i)^2\\=\sum_{a_i\in[l,r]}a_i\sum_{\{a_i\}\subseteq S\subseteq[l,r]}\frac{1}{|S|^2}\sum_{a_j\in S}a_j\\ =\sum_{a_i\in[l,r]}a_i\sum_{j=0}^{r-l}\frac{1}{(j+1)^2}(\binom{r-l}{j}a_i+\sum_{a_k\in([l,r]-\{a_i\})}\binom{r-l-1}{j-1}a_k)\\ =\sum_{a_i\in[l,r]}a_i\sum_{j=0}^{r-l}\frac{1}{(j+1)^2}(\binom{r-l}{j}a_i+\sum_{a_k\in([l,r]-\{a_i\})}(\binom{r-l}{j}-\binom{r-l-1}{j})a_k)\\ =\sum_{a_i\in[l,r]}a_i\sum_{j=0}^{r-l}\frac{1}{j+1}(\sum_{a_k\in[l,r]}\frac{a_k}{r-l+1}\binom{r-l+1}{j+1}-\sum_{a_k\in([l,r]-\{a_i\})}\frac{a_k}{r-l}\binom{r-l}{j+1})\\ =\text{res3}-\text{res4}\\$

$\text{res3}=\frac{1}{r-l+1}(\sum_{a_i\in[l,r]}a_i)^2\sum_{j=0}^{r-l}\frac{1}{j+1}\binom{r-l+1}{j+1}\\$

$\text{res4}=\sum_{a_i\in[l,r]}a_i\sum_{j=0}^{r-l}\frac{1}{j+1}\sum_{a_k\in([l,r]-\{a_i\})}\frac{a_k}{r-l}\binom{r-l}{j+1}\\=\sum_{a_i\in[l,r]}a_i\sum_{j=0}^{r-l}\frac{1}{j+1}(\sum_{a_k\in[l,r]}\frac{a_k}{r-l}\binom{r-l}{j+1}-\frac{a_i}{r-l}\binom{r-l}{j+1})\\ =\text{res5}-\text{res6}\\$

$\text{res5}=\frac{1}{r-l}(\sum_{a_i\in[l,r]}a_i)^2\sum_{j=0}^{r-l}\frac{1}{j+1}\binom{r-l}{j+1}\\$

$\text{res6}=\frac{1}{r-l}\sum_{a_i\in[l,r]}a_i^2\sum_{j=0}^{r-l}\frac{1}{j+1}\binom{r-l}{j+1}\\$

$f(n)=\sum_{i=1}^n\frac{1}{i}\binom{n}{i}\\f(n+1)=\sum_{i=1}^{n+1}\frac{1}{i}\binom{n+1}{i}\\ f(n+1)-f(n)=\sum_{i=1}^n\frac{1}{i}\binom{n}{i-1}+\frac{1}{n+1}\binom{n+1}{n+1}\\ =\frac{1}{n+1}\sum_{i=1}^n\binom{n+1}{i}+\frac{1}{n+1}\binom{n+1}{n+1}\\ =\frac{1}{n+1}\sum_{i=1}^{n+1}\binom{n+1}{i}\\ =\frac{2^{n+1}-1}{n+1}\\$

#include<bits/stdc++.h>
using namespace std;
const int N=5e6+5;
const int MOD=998244353;
int TIME(int x,int y){return (int)(1ll*x*y%MOD);}
int ksm(int x,int k=MOD-2){int res=1;for(;k;k>>=1,x=TIME(x,x))if(k&1)res=TIME(res,x);return res;}
int n,m,f[N];
int C[3][3]={{1,1,1,},{1,2,3},{1,3,6}};
int fact[N],ifact[N],ksm2[N];
int inv(int x){return TIME(ifact[x],fact[x-1]);}
struct Data{int f[3];int &operator [](int x){return f[x];}};
Data operator * (Data f,Data g){
Data res;
memset(res.f,0,sizeof(res.f));
for(int i=0;i<=2;++i){
for(int j=0;j<=2-i;++j)
}
return res;
}
Data operator + (Data f,Data g){
Data res;
memset(res.f,0,sizeof(res.f));
return res;
}
Data fuck(int x){
Data res;
res[0]=1,res[1]=x,res[2]=TIME(x,x);
return res;
}
struct Seg_Tree{
struct Node{Data f;int tag;}tr[N<<2];
void up(int u){tr[u].f=tr[u<<1].f+tr[u<<1|1].f;}
void down(int u){update(u<<1,tr[u].tag),update(u<<1|1,tr[u].tag),tr[u].tag=0;}
void build(int u,int l,int r){
if(l==r) return tr[u].f=fuck(0),void();
int mid=(l+r)>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r),up(u);
}
void modify(int u,int l,int r,int x,int y,int z){
if(x<=l&&r<=y) return update(u,z);
int mid=(l+r)>>1;down(u);
if(x<=mid) modify(u<<1,l,mid,x,y,z);
if(y>mid) modify(u<<1|1,mid+1,r,x,y,z);
return up(u);
}
Data query(int u,int l,int r,int x,int y){
if(x<=l&&r<=y) return tr[u].f;
int mid=(l+r)>>1;Data res1,res2;down(u);
if(x<=mid) res1=query(u<<1,l,mid,x,y);
if(y>mid) res2=query(u<<1|1,mid+1,r,x,y);
if(x<=mid&&y>mid) return res1+res2;
return x<=mid?res1:res2;
}
}t;
int main(){
cin>>n>>m;
fact[0]=1;for(int i=1;i<=n;++i) fact[i]=TIME(fact[i-1],i);
ifact[n]=ksm(fact[n]);for(int i=n;i>=1;--i) ifact[i-1]=TIME(ifact[i],i);
t.build(1,1,n);
for(int i=1;i<=m;++i){
int opt,l,r,x;
scanf("%d%d%d",&opt,&l,&r);
if(opt==2){
Data tmp=t.query(1,1,n,l,r);int res=0;
printf("%d\n",res);
}
else scanf("%d",&x),t.modify(1,1,n,l,r,x);
}
return 0;
}

P7880 [Ynoi2006] rldcot

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=5e5+5;
int n,m;long long c[N];vector<long long> col;
struct Edge{int nxt,to,val;}e[N<<1];int fir[N];
void add(int u,int v,int w,int i){e[i]=(Edge){fir[u],v,w},fir[u]=i;}
struct Query{int l,r,id;}a[M];bool cmp(Query a,Query b){return a.r<b.r;}
vector<pair<int,int> > opt[N];set<int> bag[N];
int get_id(long long c){return lower_bound(col.begin(),col.end(),c)-col.begin()+1;}
void merge(int c,int u,int v){
if(bag[u].size()<bag[v].size()) swap(bag[u],bag[v]);
for(set<int>::iterator i=bag[v].begin(),j;i!=bag[v].end();++i){
j=bag[u].upper_bound(*i);
if(j!=bag[u].end()) opt[*j].push_back(make_pair(c,*i));
j=bag[u].lower_bound(*i);
if(j!=bag[u].begin()) j--,opt[*i].push_back(make_pair(c,*j));
}
while(!bag[v].empty()){
bag[u].insert(*bag[v].begin());
bag[v].erase(bag[v].begin());
}
}
void dfs1(int u,int fa){
for(int i=fir[u];i;i=e[i].nxt){
int v=e[i].to;if(v==fa) continue;
c[v]=c[u]+e[i].val,dfs1(v,u);
}
}
void dfs2(int u,int fa){
for(int i=fir[u];i;i=e[i].nxt){
int v=e[i].to;if(v!=fa) dfs2(v,u);
}
bag[u].insert(u);int tmp=get_id(c[u]);
opt[u].push_back(make_pair(tmp,u));
for(int i=fir[u];i;i=e[i].nxt){
int v=e[i].to;if(v!=fa) merge(tmp,u,v);
}
}
struct Tree_Array{
int tr[N];
int lowbit(int k){return k&(-k);}
int sum(int k){int res=0;for(;k;k-=lowbit(k))res+=tr[k];return res;}
}t;
int pos[N],res[M];
int main(){
cin>>n>>m;
for(int i=1;i<n;++i){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
}
for(int i=1;i<=m;++i) scanf("%d%d",&a[i].l,&a[i].r),a[i].id=i;
sort(a+1,a+1+m,cmp),dfs1(1,0);
for(int i=1;i<=n;++i) col.push_back(c[i]);
sort(col.begin(),col.end());
col.erase(unique(col.begin(),col.end()),col.end());
dfs2(1,0);int R=0;
for(int i=1;i<=m;++i){
while(R<a[i].r){
++R;
for(int j=0;j<(int)opt[R].size();++j){
pair<int,int> tmp=opt[R][j];
pos[tmp.first]=max(pos[tmp.first],tmp.second);
}
}
res[a[i].id]=t.sum(a[i].r)-t.sum(a[i].l-1);
}
for(int i=1;i<=m;++i) printf("%d\n",res[i]);
return 0;
}
/*

--------------------------------------------------------------

-------------------------------------------------------------

lca 处做贡献。

------------------------------------------------------------------------

NB啊，这是什么神仙题。
*/

P7897 [Ynoi2006] spxmcq

$f_{u}=a_u+x+\sum_v\max(f_v,0)$

$f_u=a_u+x+\sum_vf_v$

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,Q=1e6+5;
int n,q;
struct DSU{
int fa[N];
void init(int n){for(int i=1;i<=n;++i)fa[i]=i;}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
}d;
struct Node{long long siz,sum;int id;}tr[N];
bool operator > (Node a,Node b){return (-a.sum)*b.siz>(-b.sum)*a.siz;}
bool operator != (Node a,Node b){return a.siz!=b.siz||a.sum!=b.sum;}
struct Query{int u,x,id;}a[Q];
bool cmp(Query a,Query b){return a.x<b.x;}
long long res[Q];
priority_queue<Node,vector<Node>,greater<Node> > pq;
struct Tree_Array{
long long tr[N];
int lowbit(int k){return k&(-k);}
long long sum(int k){long long res=0;for(;k;k-=lowbit(k))res+=tr[k];return res;}
}t1,t2;
int fa[N],L[N],R[N],cnt_dfn;
struct Edge{int nxt,to;}e[N];int fir[N];
void dfs(int u){
L[u]=++cnt_dfn;
for(int i=fir[u];i;i=e[i].nxt) dfs(e[i].to);
R[u]=cnt_dfn;
}
int main(){
cin>>n>>q,d.init(n);
for(int i=1;i<=n;++i) scanf("%lld",&tr[i].sum),tr[i].siz=1,tr[i].id=i;
for(int i=1;i<=q;++i) scanf("%d%d",&a[i].u,&a[i].x),a[i].id=i;
sort(a+1,a+1+q,cmp),dfs(1);
for(int i=1;i<=n;++i){
}
for(int i=1;i<=n;++i) pq.push(tr[i]);
for(int i=1;i<=q;++i){
while(!pq.empty()){
if(pq.top()!=tr[pq.top().id]){pq.pop();continue;}
if(pq.top().sum+pq.top().siz*a[i].x<0) break;
Node u=pq.top();pq.pop();
if(fa[u.id]){
int fu=d.find(fa[u.id]),fv=d.find(u.id);d.fa[fv]=fu;
tr[fu].siz+=tr[fv].siz,tr[fu].sum+=tr[fv].sum,pq.push(tr[fu]);
}
}
long long tmp1=t1.sum(R[a[i].u])-t1.sum(L[a[i].u]-1);
long long tmp2=t2.sum(R[a[i].u])-t2.sum(L[a[i].u]-1);
res[a[i].id]=a[i].x*tmp1+tmp2;
}
for(int i=1;i<=q;++i) printf("%lld\n",res[i]);
return 0;
}
/*

------------------------------------------------------------------------

sum 即可，所以暴力用并查集来搞就行了。
*/

==