听说这是线段树的裸题??(看来我也就能搞个求和什么的了2333)
1 #include<bits/stdc++.h> 2 #define INF 0x7fffffff 3 #define LL long long 4 #define N 100005 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 LL s[N<<3],del[N<<3],mark[N<<3]; 14 void build(int k, int l, int r) 15 { 16 if (l==r) 17 { 18 mark[k]=l; 19 return; 20 } 21 int mid=(l+r)>>1; 22 build(k<<1,l,mid); build(k<<1|1,mid+1,r); 23 } 24 LL query(int k, int l, int r, int x, int y) 25 { 26 if (y<l || x>r) return 0; 27 if (x<=l && r<=y) return s[k]; 28 int mid=l+r>>1; 29 LL L=query(k<<1,l,mid,x,y); 30 LL R=query(k<<1|1,mid+1,r,x,y); 31 return L+R+max(0,min(r,y)-max(l,x)+1)*del[k]; 32 } 33 void clear(int k, int l, int r, int val) 34 { 35 if (mark[k]) 36 { 37 del[k]+=abs(mark[k]-val); 38 s[k]+=1LL*(r-l+1)*abs(mark[k]-val); 39 mark[k]=0; 40 } 41 else 42 { 43 if (l==r) return; 44 int mid=l+r>>1; 45 clear(k<<1,l,mid,val); 46 clear(k<<1|1,mid+1,r,val); 47 s[k]=s[k<<1]+s[k<<1|1]+1LL*(r-l+1)*del[k]; 48 } 49 } 50 void change(int k, int l, int r, int x, int y, int val) 51 { 52 if (y<l || x>r) return; 53 if (x<=l && r<=y) 54 { 55 clear(k,l,r,val); 56 mark[k]=val; 57 return; 58 } 59 int mid=l+r>>1; 60 if (mark[k]) 61 { 62 mark[k<<1]=mark[k<<1|1]=mark[k]; 63 mark[k]=0; 64 } 65 change(k<<1,l,mid,x,y,val); 66 change(k<<1|1,mid+1,r,x,y,val); 67 s[k]=s[k<<1]+s[k<<1|1]+1LL*(r-l+1)*del[k]; 68 } 69 int main() 70 { 71 int n=ra(),m=ra(); 72 build(1,1,n); 73 while (m--) 74 { 75 int type=ra(); 76 if (type==1) 77 { 78 int x=ra(),y=ra(),v=ra(); 79 change(1,1,n,x,y,v); 80 } 81 else 82 { 83 int x=ra(),y=ra(); 84 printf("%I64d ",query(1,1,n,x,y)); 85 } 86 } 87 return 0; 88 }