HDU4893线段树的简单应用:
//主要优化部分 :若某一段进行过3操作且没进行过1操作,那么再次对该段进行3操作时可以忽略此段
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100005
#define ll root*2
#define rr root*2+1
#define mid (a[root].l+a[root].r)/2
__int64 fi[100];
struct node{
int l, r;
__int64 sum;
int flag3;
}a[N*4];
void build(int l,int r,int root){
a[root].l=l;a[root].r=r;
a[root].sum=0;a[root].flag3=0;
if(l==r) return;
build(l,mid,ll);
build(mid+1,r,rr);
}
__int64 find(__int64 key,int n){
int l=1, r=n;
while(l<=r){
int mm=(l+r)/2;
if(fi[mm]<=key&&mm==n) return fi[mm];
else if(fi[mm]>=key&&mm==1) return fi[mm];
else if(fi[mm]<=key&&fi[mm+1]>=key) {
if(abs(fi[mm]-key)<=abs(fi[mm+1]-key))
return fi[mm];
else return fi[mm+1];
}
else if(fi[mm]>=key&&fi[mm-1]<=key) {
if(abs(fi[mm-1]-key)<=abs(fi[mm]-key))
return fi[mm-1];
else return fi[mm];
}
else if(fi[mm]<key){
l=mm+1;
}
else if(fi[mm]>key){
r=mm-1;
}
}
}
void init(){
fi[0]=1;fi[1]=1;fi[2]=2;
for(int i=3;i<=90;i++){
fi[i]=fi[i-1]+fi[i-2];
}
}
void down(int root){
if(a[root].flag3==1)return;
a[root].flag3=1;
if(a[root].l==a[root].r){
a[root].sum=find(a[root].sum,90);
return ;
}
down(ll);
down(rr);
a[root].sum=a[ll].sum+a[rr].sum;
}
void add(int p,__int64 val,int root){
a[root].flag3=0;
if(a[root].l==p&&a[root].r==p){
a[root].sum+=val;
return;
}
if(p<=mid) add(p,val,ll);
else add(p,val,rr);
a[root].sum=a[ll].sum+a[rr].sum;
a[root].flag3=min(a[ll].flag3,a[rr].flag3);
}
void update(int l,int r,int root){
if(a[root].flag3) return;
if(a[root].l>=l&&a[root].r<=r){
down(root);
return;
}
if(l<=mid) update(l,r,ll);
if(r>mid) update(l,r,rr);
a[root].sum=a[ll].sum+a[rr].sum;
a[root].flag3=min(a[ll].flag3,a[rr].flag3);
}
__int64 query(int l,int r,int root){
if(a[root].l==l&&a[root].r==r)return a[root].sum;
if(r<=mid) return query(l,r,ll);
else if(l>mid) return query(l,r,rr);
else return query(l,mid,ll)+query(mid+1,r,rr);
}
main(){
int x, z, n, m;
int y;
__int64 yy;
init();
while(scanf("%d %d",&n,&m)==2){
build(1,n,1);
while(m--){
scanf("%d%d",&z,&x);
if(z==1){scanf("%I64d",&yy);add(x,yy,1);}
else if(z==2){scanf("%d",&y);printf("%I64d\n",query(x,y,1));}
else {scanf("%d",&y);update(x,y,1);}
}
}
}