# **浅谈差分【复习】**

https://www.luogu.org/problem/P5026

code ：

``````#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int aa[2100000],bb[2100000];
#define isdigit(x) ((x)>='0'&&(x)<='9')
int a=0,flag=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-')flag=-1;
c=getchar();
}
while(isdigit(c)){
a=(a<<1)+(a<<3)+c-48;
c=getchar();
}
return a*flag;
}
int main(){
int *a=aa+1000000,*b=bb+1000000;
for(int i=1;i<=n;++i){
a[x-3*v+1]++;
a[x-2*v+1]-=2;
a[x+1]+=2;
a[x+2*v+1]-=2;
a[x+3*v+1]++;
}
for(int i=-40000;i<=m+40000;++i)a[i]+=a[i-1],b[i]+=b[i-1]+a[i];
for(int i=1;i<=m;++i)printf("%d ",b[i]);
return 0;
}``````

https://www.luogu.org/problem/P4623

code by wzxbeliever：

``````#include<bits/stdc++.h>
#define ll long long
#define il inline
#define ri register int
#define lowbit(x) x&(-x)
using namespace std;
const int maxn=1e5+5;
const int maxx=1e6+5;
int n,m,a,b,c,d,e,f,val;
int X[maxx],Y[maxx];
char ch1,ch2;
il void add(int x,int p){while(x<=maxx)X[x]+=p,x+=lowbit(x);}
il int query(int x){int res=0;while(x)res+=X[x],x-=lowbit(x);return res;}
il void add2(int x,int p){while(x<=maxx)Y[x]+=p,x+=lowbit(x);}
il int query2(int x){int res=0;while(x)res+=Y[x],x-=lowbit(x);return res;}
int main(){
scanf("%d",&n);
for(ri i=1;i<=n;i++){
int Lmin,Rmin,Lmax,Rmax;
Lmax=Rmax=0;Lmin=Rmin=maxx;
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
a++,b++,c++,d++,e++,f++;
Lmin=min(a,min(c,e));
Rmin=min(b,min(d,f));
Lmax=max(a,max(c,e));
Rmax=max(b,max(d,f));
Lmin++;Rmin++;
}
scanf("%d",&m);
while(m--){
cin>>ch1>>ch2>>val;val++;
if(ch1=='x')printf("%d\n",query(val));
else printf("%d\n",query2(val));
}
return 0;
}
``````

https://www.luogu.org/problem/P2680

LCA+树上差分+二分

code by std：

``````#include<bits/stdc++.h>
#define IL inline
#define RI register int
#define N  300008
using namespace std;
IL void in(int &x)
{
int f=1;x=0;char s=getchar();
while(s>'9' or s<'0'){if(s=='-')f=-1;s=getchar();}
while(s>='0' and s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
struct node{
int x , y , lca , dis ;
bool operator < (const node & a ) const {
return dis < a.dis ;
}
}query[N];
struct cod{int u,v,w;}edge[N<<1];
int l,r,m;
IL void add(int x,int y,int z)
{
edge[tot].v=y;
edge[tot].w=z;
}
void dfs(int u,int fa , int dis)
{
depth[u]=depth[fa]+1;
f[u][0]=fa ; init[u] = dis;
for(RI i=1;(1<<i)<=depth[u];i++)
f[u][i]=f[f[u][i-1]][i-1];
{
if(edge[i].v==fa)continue;
Dis[edge[i].v]=Dis[u]+edge[i].w;
dfs(edge[i].v,u,edge[i].w);
}
}
IL int lca(int x,int y)
{
if(depth[x]>depth[y])swap(x,y);
for(RI i=20;i>=0;i--)
if(depth[y]-(1<<i)>=depth[x])
y=f[y][i];
if(x==y)return x;
for(RI i=20;i>=0;i--)
{
if(f[x][i]==f[y][i])continue;
x=f[x][i],y=f[y][i];
}
return f[x][0];
}
void dfss(int u,int fa)
{
{
if(edge[i].v==fa)continue;
dfss(edge[i].v,u);
cnt[u]+=cnt[edge[i].v];
}
}
IL bool ok(int x)
{
int num=0,now=0;
for(RI i=1;i<=n;i++)cnt[i]=0;
for(RI i=1;i<=m;i++)
{
if(query[i].dis<=x)continue;
cnt[query[i].x]++;cnt[query[i].y]++;
cnt[query[i].lca]-=2;
num++;
}
dfss(1,0);
for(RI i=1;i<=n;i++)
{
if(cnt[i]==num)now=max(now,init[i]);
}
return query[m].dis-now<=x;
}
int main()
{
in(n),in(m);
for(RI i=1,x,y,z;i<n;i++)
{
in(x),in(y),in(z);
}
dfs(1,0,0);
for(RI i=1,x,y;i<=m;i++)
{
in(x),in(y);
query[i].lca=lca(x,y);
r=max(r,(query[i].dis=Dis[x]+Dis[y]-2*Dis[query[i].lca]));
query[i].x = x,query[i].y = y;
}
sort(query+1,query+m+1);
while(l<=r)
{
int mid=(l+r)>>1;
if(ok(mid))r=mid-1;
else l=mid+1;
}
printf("%d",l);
}``````

## 题目描述

code：

``````#include<bits/stdc++.h>
using namespace std;
struct rec{int nxt,to;bool dead;}e[400002];
int n,m;
int odd[100001],eve[100001],cut[400002],depth[100001],in[100001];
bool vis[100001];
int ans;
void add(int x,int y)
{
e[cnt].to=y;
}
void dfs(int x)
{
vis[x]=1;
{
if(cut[i])continue;
cut[i]=cut[i^1]=1;
if(vis[e[i].to])
{
if((depth[x]-depth[e[i].to])&1){eve[e[i].to]--;eve[x]++;}
else{odd[e[i].to]--;odd[x]++;odd[0]++;}
}
else
{
depth[e[i].to]=depth[x]+1;
in[e[i].to]=i;
dfs(e[i].to);
}
}
}
void dfs(int x,int f)
{
if(in[e[i].to]==i)
{
dfs(e[i].to,x);
odd[x]+=odd[e[i].to];
eve[x]+=eve[e[i].to];
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
}
for(int i=1;i<=n;i++)if(!vis[i])dfs(i);
if(odd[0]==1)ans=1;
for(int i=1;i<=n;i++)if(!in[i])dfs(i,0);
for(int i=1;i<=n;i++)
if(in[i]&&odd[i]==odd[0]&&!eve[i])ans++;
printf("%d",ans);
return 0;
}``````

code by std：

``````#include <cstdio>
#define maxn 200005
#define lowbit(x) (x&(-(x)))
#define LL long long
LL a[maxn];
int n,m;
struct TreeArr{
int a[maxn];
void add(int x,int v){
while(x<=n){
a[x]+=v;
x+=lowbit(x);
}
}
int query(int pos){
int ret=0;
while(pos){
ret+=a[pos];
pos^=lowbit(pos);
}
return ret;
}
} tree;
bool check(int x){
return (a[x]<0&&a[x+1]>0)||(a[x]>0&&a[x+1]<0);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=n;i>=1;i--){
a[i]-=a[i-1];
}
for(int i=1;i<=m;i++){
int op;scanf("%d",&op);
if(op){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
int px=check(x),py=check(y);
int px1=check(x-1),py1=check(y+1);
a[x]+=z;a[y+1]-=z;
int nx=check(x),ny=check(y);
int nx1=check(x-1),ny1=check(y+1);
}
else {
int x,y;scanf("%d%d",&x,&y);
if(x==y)printf("0\n");
else printf("%d\n",tree.query(y-1)-tree.query(x));
}
}
}``````

**浅谈差分【复习】**

(0)
(0)

© 2014 mamicode.com 版权所有 京ICP备13008772号-2