标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2783 Accepted Submission(s): 766
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 100005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 int max(int x,int y){return x>y?x:y;} 16 int min(int x,int y){return x<y?x:y;} 17 int abs(int x,int y){return x<0?-x:x;} 18 19 struct node{ 20 int l, r, val; 21 int maxh, minh; 22 bool flag; 23 }a[N*4]; 24 25 int b[N]; 26 int n, m; 27 28 void build(int l,int r,int root){ 29 a[root].l=l; 30 a[root].r=r; 31 a[root].flag=false; 32 if(l==r){ 33 a[root].flag=true; 34 a[root].maxh=a[root].minh=a[root].val=b[l];return; 35 } 36 build(l,mid,ll); 37 build(mid+1,r,rr); 38 a[root].maxh=max(a[ll].maxh,a[rr].maxh); 39 a[root].minh=min(a[ll].minh,a[rr].minh); 40 } 41 42 void update(int l,int r,int val,int root){ 43 if(a[root].flag&&a[root].l!=a[root].r){ 44 a[ll].flag=a[rr].flag=true; 45 a[ll].maxh=a[ll].minh=a[rr].maxh=a[rr].minh=a[ll].val=a[rr].val=a[root].val; 46 a[root].flag=false; 47 } 48 if(a[root].l==l&&a[root].r==r){ 49 a[root].val=val; 50 a[root].flag=true; 51 a[root].maxh=a[root].minh=val; 52 return; 53 } 54 if(l>mid) update(l,r,val,rr); 55 else if(r<=mid) update(l,r,val,ll); 56 else{ 57 update(l,mid,val,ll); 58 update(mid+1,r,val,rr); 59 } 60 if(a[ll].val==a[rr].val&&a[ll].flag&&a[rr].flag){ 61 a[root].flag=true; 62 a[root].val=a[ll].val; 63 } 64 a[root].maxh=max(a[ll].maxh,a[rr].maxh); 65 a[root].minh=min(a[ll].minh,a[rr].minh); 66 } 67 68 int ans; 69 70 int query(int l,int r,int val,int root){ 71 if(a[root].flag&&a[root].val!=val) return 0; 72 if(a[root].flag&&a[root].val==val) return r-l+1; 73 if(a[root].l==a[root].r) return 0; 74 if(a[root].maxh<val||a[root].minh>val) return 0; 75 if(l>mid) return query(l,r,val,rr); 76 else if(r<=mid) return query(l,r,val,ll); 77 else return query(l,mid,val,ll)+query(mid+1,r,val,rr); 78 } 79 80 main() 81 { 82 int x, y, z, w; 83 int i, j, k; 84 while(scanf("%d %d",&n,&m)==2){ 85 for(i=0;i<n;i++) scanf("%d",&b[i]); 86 build(0,n-1,1); 87 while(m--){ 88 scanf("%d %d %d %d",&x,&y,&z,&w); 89 if(x==1){ 90 update(y,z,w,1); 91 } 92 else{ 93 int ans=query(y,z,w,1); 94 printf("%d\n",ans); 95 } 96 } 97 } 98 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4379607.html