比赛 |
2025.3.6 |
评测结果 |
AAAAWWAAWA |
题目名称 |
WHZ 的序列 |
最终得分 |
70 |
用户昵称 |
123 |
运行时间 |
2.269 s |
代码语言 |
C++ |
内存使用 |
59.37 MiB |
提交时间 |
2025-03-06 20:50:28 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=800010;
struct node {
int l,r;
long long ret=0,flag=0;
} tree[N][3];
int n,q;
long long a[N];
void pushup(int p)
{
tree[p][0].ret=tree[p*2][0].ret+tree[p*2+1][0].ret;
tree[p][1].ret=tree[p*2][1].ret+tree[p*2+1][1].ret;
}
void pushdown(int p)
{
tree[p*2][0].flag+=tree[p][0].flag;
tree[p*2+1][0].flag+=tree[p][0].flag;
tree[p*2][1].flag+=tree[p][1].flag;
tree[p*2+1][1].flag+=tree[p][1].flag;
tree[p*2][0].ret+=(tree[p*2][0].r-tree[p*2][0].l+1)*tree[p][0].flag;
tree[p*2+1][0].ret+=(tree[p*2+1][0].r-tree[p*2+1][0].l+1)*tree[p][0].flag;
tree[p*2][1].ret+=(tree[p*2][1].r-tree[p*2][1].l+1)*tree[p][1].flag;
tree[p*2+1][1].ret+=(tree[p*2+1][1].r-tree[p*2+1][1].l+1)*tree[p][1].flag;
tree[p][0].flag=0;
tree[p][1].flag=0;
}
void bulid(int p,int l,int r)
{
tree[p][0].l=tree[p][1].l=l,tree[p][0].r=tree[p][1].r=r;
if (l==r)
{
tree[p][1].ret=a[l*2-1];
tree[p][0].ret=a[l*2];
return ;
}
int mid=(l+r)/2;
bulid(p*2,l,mid),bulid(p*2+1,mid+1,r);
pushup(p);
}
void update(int p,int l,int r,long long d,int flag)
{
if (l<=tree[p][flag].l && tree[p][flag].r<=r)
{
tree[p][flag].ret+=(tree[p][flag].r-tree[p][flag].l+1)*d;
tree[p][flag].flag+=d;
return ;
}
pushdown(p);
int mid=(tree[p][flag].l+tree[p][flag].r)/2;
if (l<=mid) update(p*2,l,r,d,flag);
if (mid<r) update(p*2+1,l,r,d,flag);
pushup(p);
}
long long query(int p,int l,int r,int flag)
{
if (l<=tree[p][flag].l && tree[p][flag].r<=r)
{
return tree[p][flag].ret;
}
pushdown(p);
int mid=(tree[p][flag].l+tree[p][flag].r)/2;
long long cnt=0;
if (l<=mid) cnt+=query(p*2,l,r,flag);
if (mid<r) cnt+=query(p*2+1,l,r,flag);
pushup(p);
return cnt;
}
int main(){
freopen("whz_sequence.in","r",stdin);
freopen("whz_sequence.out","w",stdout);
cin>>n;
for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
bulid(1,1,(n+1)/2);
cin>>q;
while (q--)
{
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if (op==1)
{
long long d;
scanf("%lld",&d);
if (l%2==r%2)
{
update(1,(l-1)/2+1,(r-1)/2+1,d,r%2),update(1,l/2+1,(r-2)/2+1,d,1-r%2);
}
else
{
update(1,(l-1)/2+1,(r-2)/2+1,d,l%2),update(1,l/2+1,(r-1)/2+1,d,1-l%2);
}
}
else
{
if (l%2==r%2)
{
printf("%lld\n",query(1,(l-1)/2+1,(r-1)/2+1,r%2)-query(1,l/2+1,(r-2)/2+1,1-r%2));
}
else
{
printf("%lld\n",query(1,(l-1)/2+1,(r-2)/2+1,l%2)-query(1,l/2+1,(r-1)/2+1,1-l%2));
}
}
}
}