Gohldg @ 2023-08-07 10:36:05
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
int a[500001];
int b[500001];
int c[500001];
int t[2][500001];
int n,m;
int lowbit(int x){
return (x&(-x));
}
int add(int x,int y,int k){
for(int i=x;i<=n;i+=lowbit(i))
t[k][i]+=y;
}
int sum(int k,int x){
int num=0;
for(int i=x;i>=1;i-=lowbit(i))
num+=t[k][i];
return num;
}
void upd(int l,int r,int x){
add(0,l,x);
add(0,r+1,-x);
add(1,l,l*x);
add(1,r+1,-(r+1)*x);
}
int ask(int n){
int a=sum(0,n);
int b=sum(1,n);
return (n+1)*a-b;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i]-a[i-1];
c[i]=c[i-1]+b[i];
t[0][i]=c[i]-c[i-lowbit(i)];
}
for(int i=1;i<=n;i++){
c[i]=c[i-1]+i*b[i];
t[1][i]=c[i]-c[i-lowbit(i)];
}
for(int i=1;i<=m;i++){
int d;
cin>>d;
if(d==1){
int x,y,k;
cin>>x>>y>>k;
upd(x,y,k);
}else{
int x,y;
cin>>x>>y;
cout <<ask(y)-ask(x-1)<<endl;
}
}
return 0;
}
by dthythxth_Huge_Brain @ 2023-08-07 10:38:15
@Gohldg 数组开小了,改大一点,就会发现,你TLE了
by Gohldg @ 2023-08-07 13:54:01
@Huge_Brain 还是RE
by dthythxth_Huge_Brain @ 2023-08-07 14:20:34
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
int a[500111];
int b[500111];
int c[500111];
int t[200][500111];
int n,m;
int lowbit(int x){
return (x&(-x));
}
int add(int x,int y,int k){
for(int i=x;i<=n;i+=lowbit(i))
t[k][i]+=y;
}
int sum(int k,int x){
int num=0;
for(int i=x;i>=1;i-=lowbit(i))
num+=t[k][i];
return num;
}
void upd(int l,int r,int x){
add(0,l,x);
add(0,r+1,-x);
add(1,l,l*x);
add(1,r+1,-(r+1)*x);
}
int ask(int n){
int a=sum(0,n);
int b=sum(1,n);
return (n+1)*a-b;
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i]-a[i-1];
c[i]=c[i-1]+b[i];
t[0][i]=c[i]-c[i-lowbit(i)];
}
for(int i=1;i<=n;i++){
c[i]=c[i-1]+i*b[i];
t[1][i]=c[i]-c[i-lowbit(i)];
}
for(int i=1;i<=m;i++){
int d;
cin>>d;
if(d==1){
int x,y,k;
cin>>x>>y>>k;
upd(x,y,k);
}else{
int x,y;
cin>>x>>y;
cout <<ask(y)-ask(x-1)<<endl;
}
}
return 0;
}
这样TLE
by Gohldg @ 2023-08-07 14:28:22
@Huge_Brain 虽然是自己找出来的还是感谢,已AC
by Gohldg @ 2023-08-07 14:29:00
@Huge_Brain 其实暗藏红WA