# 【Codechef-Hard】Chef and Churu 分块

## Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}

#define MAXN 100010
#define LL unsigned long long

int N,M,val[MAXN],pl[MAXN],pr[MAXN];

struct Block{
int l,r,num[MAXN]; LL ans,sum[510];
}b[510];
int belong[MAXN],rk[MAXN],Bn,Bs;
LL Sum[MAXN];

inline void Getsum(int x)
{
LL t=0;
for (int i=b[x].l; i<=b[x].r; i++) {
t+=val[i];
b[x].sum[rk[i]]=t;
}
b[x].sum[Bs]=t;
for (int i=x; i<=Bn; i++) Sum[i]=Sum[i-1]+b[i].sum[Bs];
}

inline LL Query(int x,int y)
{
int bx=belong[x],by=belong[y];
if (bx==by)
return b[bx].sum[rk[y]]-b[bx].sum[rk[x]-1];
else
if (bx+1==by)
return b[by].sum[rk[y]]-b[bx].sum[rk[x]-1]+b[bx].sum[Bs];
else
return b[by].sum[rk[y]]+Sum[by-1]-Sum[bx-1]-b[bx].sum[rk[x]-1];
}

inline void Getnum(int x)
{
for (int i=b[x].l; i<=b[x].r; i++) {
b[x].num[pl[i]]++; b[x].num[pr[i]+1]--;
b[x].ans+=Query(pl[i],pr[i]);
}
for (int i=1; i<=N; i++) b[x].num[i]+=b[x].num[i-1];
}

int main()
{
for (int i=1; i<=N; i++) val[i]=read();

Bs=int(sqrt(N));
for (int i=1; i<=N; i++) {
if ((i-1)%Bs==0) Bn++,b[Bn].l=i;
belong[i]=Bn; rk[i]=(i-1)%Bs+1;
if ((i-1)%Bs==Bs-1 || i==N) b[Bn].r=i;
}

for (int i=1; i<=Bn; i++) Getsum(i);
for (int i=1; i<=Bn; i++) Getnum(i);

while (M--) {
if (opt==1) {
z=y-val[x];
val[x]=y;
Getsum(belong[x]);
for (int i=1; i<=Bn; i++) b[i].ans+=(LL)b[i].num[x]*z;
} else {
int bx=belong[x],by=belong[y];
LL ans=0;
if (bx==by || bx+1==by)
for (int i=x; i<=y; i++) ans+=Query(pl[i],pr[i]);
else {
for (int i=bx+1; i<=by-1; i++) ans+=b[i].ans;
for (int i=x; i<b[bx+1].l; i++) ans+=Query(pl[i],pr[i]);
for (int i=b[by-1].r+1; i<=y; i++) ans+=Query(pl[i],pr[i]);
}
printf("%llu\n",ans);
}
}

return 0;
}


——It's a lonely path. Don't make it any lonelier than it has to be.
posted @ 2017-03-07 14:57  DaD3zZ  阅读(392)  评论(0编辑  收藏  举报