比赛 |
2025.3.6 |
评测结果 |
AAAAAAAAAA |
题目名称 |
WHZ 的序列 |
最终得分 |
100 |
用户昵称 |
dream |
运行时间 |
3.255 s |
代码语言 |
C++ |
内存使用 |
16.41 MiB |
提交时间 |
2025-03-06 20:04:44 |
显示代码纯文本
#include<bits/stdc++.h>
#define ls p*2
#define rs p*2+1
using namespace std;
typedef long long ul;
const int N=200005;
struct node{
ul l,r,s1,s2,add;
}tr[N*4];
int n,m;
int a[N];
void pushup(int p){
int ll=tr[ls].r-tr[ls].l+1;
if(!(ll&1)){
tr[p].s1=tr[ls].s1+tr[rs].s1;
tr[p].s2=tr[ls].s2+tr[rs].s2;
}
else{
tr[p].s1=tr[ls].s1+tr[rs].s2;
tr[p].s2=tr[ls].s2+tr[rs].s1;
}
}
void build(int p,int l,int r){
tr[p]={l,r,0,0,0};
if(l==r){
tr[p].s1=a[l];
tr[p].s2=-a[l];
return;
}
int mid=(l+r)/2;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(p);
}
void pushdown(int p){
tr[ls].add+=tr[p].add;
tr[rs].add+=tr[p].add;
int ll=tr[ls].r-tr[ls].l+1,rl=tr[rs].r-tr[rs].l+1;
if(ll&1){
tr[ls].s1+=tr[p].add;
tr[ls].s2-=tr[p].add;
}
if(rl&1){
tr[rs].s1+=tr[p].add;
tr[rs].s2-=tr[p].add;
}
tr[p].add=0;
}
void update(int p,int l,int r,ul v){
if(l<=tr[p].l&&tr[p].r<=r){
int ll=tr[p].r-tr[p].l+1;
tr[p].add+=v;
if(ll&1){
tr[p].s1+=v;
tr[p].s2-=v;
}
return;
}
pushdown(p);
int mid=(tr[p].l+tr[p].r)/2;
if(l<=mid){
update(ls,l,r,v);
}
if(r>mid){
update(rs,l,r,v);
}
pushup(p);
}
ul query(int p,int l,int r){
if(l<=tr[p].l&&tr[p].r<=r){
int ll=tr[p].l-1-l+1;
if(!(ll&1)){
return tr[p].s1;
}
else{
return tr[p].s2;
}
}
pushdown(p);
int mid=(tr[p].l+tr[p].r)/2;
ul sum=0;
if(l<=mid){
sum+=query(ls,l,r);
}
if(r>mid){
sum+=query(rs,l,r);
}
return sum;
}
int main(){
freopen("whz_sequence.in","r",stdin);
freopen("whz_sequence.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
cin>>m;
while(m--){
ul op,x,y,v;
cin>>op>>x>>y;
if(op==1){
cin>>v;
update(1,x,y,v);
}
else{
cout<<query(1,x,y)<<"\n";
}
}
return 0;
}