#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+10;
struct node{
int sum,mi,ma;
}tree[maxn*4];
int tag[maxn*4],a[maxn];
void push_down(int q,int L,int R){
if(!tag[q])return ;
tag[q*2]+=tag[q];
tag[q*2+1]+=tag[q];
int mid = (L+R)/2;
tree[q*2].sum+=(L-mid+1)*tag[q];
tree[q*2+1].sum+=(R-mid)*tag[q];
tree[q*2].mi+=tag[q];
tree[q*2].ma+=tag[q];
tree[q*2+1].mi+=tag[q];
tree[q*2+1].ma+=tag[q];
tag[q] = 0;
return ;
}
void push_up(int q){
tree[q].sum = tree[q*2].sum+tree[q*2+1].sum;
tree[q].mi = min(tree[q*2].mi,tree[q*2+1].mi);
tree[q].ma = max(tree[q*2].ma,tree[q*2+1].ma);
}
int querymin(int q,int L,int R,int l,int r){
if(L>=l && R<=r)return tree[q].mi;
push_down(q,L,R);
int mid = (L+R)/2,res = INT_MAX;
if(l<=mid)res = min(res,querymin(q*2,L,mid,l,r));
if(r>mid)res = min(res,querymin(q*2+1,mid+1,R,l,r));
return res;
}
int querysum(int q,int L,int R,int l,int r){
if(L>=l && R<=r)return tree[q].sum;
push_down(q,L,R);
int mid = (L+R)/2,res = 0;
if(l<=mid)res += querysum(q*2,L,mid,l,r);
if(r>mid)res += querysum(q*2+1,mid+1,R,l,r);
return res;
}
void update(int q,int L,int R,int l,int r,int x){
if(L>=l && R<=r){
if(x>=0){
tree[q].sum+=(R-L+1)*x;
tree[q].ma+=x;
tree[q].mi+=x;
tag[q]+=x;
return ;
}else{
if(tree[q].ma<=-x){
tree[q].sum = -x*(R-L+1)-tree[q].sum;
swap(tree[q].ma,tree[q].mi);
tree[q].ma = -x-tree[q].ma;
tree[q].mi = -x-tree[q].mi;
return ;
}
if(tree[q].mi>=-x){
tree[q].sum+=(R-L+1)*x;
tree[q].ma+=x;
tree[q].mi = max(0,tree[q].mi+x);
return ;
}
}
}
push_down(q,L,R);
int mid = (L+R)/2;
if(l<mid)update(q*2,L,mid,l,r,x);
if(r>mid)update(q*2+1,mid+1,R,l,r,x);
push_up(q);
return ;
}
void build(int q,int l,int r){
if(l == r){
tree[q].sum = a[l];
tree[q].mi = a[l];
tree[q].ma = a[l];
return ;
}
int mid = (l+r)/2;
build(q*2,l,mid);
build(q*2+1,mid+1,r);
push_up(q);
return ;
}
int main(){
int n,m;
cin>>n>>m;
for(int i = 1;i<=n;i++)cin>>a[i];
build(1,1,n);
int op,l,r,x;
while(m--){
cin>>op;
if(op == 0){
cin>>l>>r>>x;
update(1,1,n,l,r,-x);
}else if(op == 1){
cin>>l>>r>>x;
update(1,1,n,l,r,x);
}else{
cin>>l>>r;
cout<<querysum(1,1,n,l,r)<<" "<<querymin(1,1,n,l,r)<<'\n';
}
}
return 0;
}
/*
5 5
4 2 1 7 3
0 2 4 5
2 1 4
1 1 3 2
0 1 5 7
2 3 4
*/#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+100;
struct node{
int l,r,id;
}q[maxn];
long long block,sum,ans[maxn],cnt[maxn*5],a[maxn];
bool cmp(node a,node b){
if((a.l-1)/block == (b.l-1)/block)return a.r<b.r;
return a.l<b.l;
}
void add(int x){
sum-=cnt[x]*cnt[x]*x;
cnt[x]++;
sum+=cnt[x]*cnt[x]*x;
}
void del(int x){
sum-=cnt[x]*cnt[x]*x;
cnt[x]--;
sum+=cnt[x]*cnt[x]*x;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
block = sqrt(n);
for(int i = 1;i<=n;i++)cin>>a[i];
for(int i = 1;i<=m;i++)cin>>q[i].l>>q[i].r,q[i].id = i;
sort(q+1,q+1+m,cmp);
int l = 0,r = 1;
for(int i = 1;i<=m;i++){
while(l>q[i].l)l--,add(l);
while(r<q[i].r)r++,add(r);
while(l<q[i].l)del(l),l++;
while(r>q[i].r)del(r),r--;
ans[q[i].id] = sum;
}
for(int i = 1;i<=m;i++){
cout<<ans[i]<<'\n';
}
return 0;
}
/*
3 2
1 2 1
1 2
1 3
*/#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+10;
struct tree{
int pow,a;
}sum[maxn*4];
int tag[maxn*4],s[maxn],a[maxn],b[maxn];
int n,q;
void push_down(int q,int l,int r){
if(tag[q]){
tag[q*2]+=tag[q];
tag[q*2+1]+=tag[q];
int mid = (l+r)/2;
sum[q*2].pow += tag[q]*(s[mid]-s[l-1]);
sum[q*2].a += tag[q]*(s[n]*(mid-l+1)-(s[mid]-s[l-1]));
sum[q*2+1].pow += tag[q]*(s[r]-s[mid]);
sum[q*2].a += tag[q]*(s[n]*(r-mid)-(s[r]-s[mid]));
tag[q] = 0;
}
}
void push_up(int q){
sum[q].pow = sum[q*2].pow+sum[q*2+1].pow;
sum[q].a = sum[q*2].a+sum[q*2+1].a;
}
void build(int q,int l,int r){
if(l == r){
sum[q].pow = b[l]*a[l];
sum[q].a = b[l]*(s[n]-a[l]);
return ;
}
int mid = (l+r)/2;
build(q*2,l,mid);
build(q*2+1,mid+1,r);
push_up(q);
return ;
}
void Update(int q,int L,int R,int l,int r,int k){
if(L>=l && R<=r){
tag[q]+=k;
sum[q].pow += k*(s[R]-s[L-1]);
sum[q].a += k*(s[n]*(R-L+1)-(s[R]-s[L-1]));
return ;
}
int mid = (L+R)/2;
push_down(q,L,R);
if(l<=mid)Update(q*2,L,mid,l,r,k);
if(r>mid)Update(q*2+1,mid+1,R,l,r,k);
push_up(q);
return ;
}
void update(int q,int L,int R,int l,int r,int k){
if(L>=l && R<=r){
Update(1,1,n,L,n,k);
return ;
}
int mid = (L+R)/2;
if(l<=mid)update(q*2,L,mid,l,r,k);
if(r>mid)update(q*2+1,mid+1,R,l,r,k);
return ;
}
int querypow(int q,int L,int R,int l,int r){
if(L>=l && R<=r)return sum[q].pow;
int mid = (L+R)/2,res = 0;
push_down(q,L,R);
if(l<=mid)res+=querypow(q*2,L,mid,l,r);
if(r>mid)res+=querypow(q*2+1,mid+1,R,l,r);
return res;
}
int query(int q,int L,int R,int l,int r){
if(L>=l && R<=r)return sum[q].a;
int mid = (L+R)/2,res = 0;
push_down(q,L,R);
if(l<=mid)res+=query(q*2,L,mid,l,r);
if(r>mid)res+=query(q*2+1,mid+1,R,l,r);
return res;
}
int main(){
cin>>n>>q;
for(int i = 1;i<=n;i++){
cin>>a[i];
a[i]+=a[i-1];
s[i] = s[i-1]+a[i];
}
for(int i = 1;i<=n;i++){
cin>>b[i];
b[i]+=b[i-1];
}
build(1,1,n);
int l,r,k;
while(q--){
cin>>l>>r>>k;
update(1,1,n,l,r,k);
cout<<n*querypow(1,1,n,1,n)-query(1,1,n,1,n)<<'\n';
}
return 0;
}
/*
6 6
1 1 4 5 1 4
1 9 1 9 8 1
1 6 0
1 1 4
1 5 4
4 6 2
1 6 -3
1 6 -20
*/1395046
sid CZCcBnlUtPGEWSm1LvkPFpGyZlNad8we
sid.sig 4EsFCUrLsyNKRsNSqIyiI1N04dA其政闷闷,
其民淳淳;
其政察察,
其民缺缺。
祸兮,
福之所倚,
福兮,
祸之所伏。
孰知其极:
其无正邪?
正复为奇,
善复为妖。
人之迷,
其日固久。
是以圣人
方而不割,
廉而不刿,
直而不肆,
光而不耀。https://v.wjx.cn/vm/hKVqO8u.aspx#
画板 vjudge.net/contest/840474 2058ssui 建图
qq号 3756792319 qwq
https://www.luogu.com.cn/discuss/1318737
【图论算法】强连通分量 900 20260721