题意:给你一个数列,起始值为0 ,区间增加或减少,但是每次的最大改变值是这个区间的最值到临界值的范围。单值查询和区间询问。
解题思路:线段树。
解题代码:
1 // File Name: h.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月16日 星期一 15时22分06秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include<stack> 11 #include<bitset> 12 #include<algorithm> 13 #include<functional> 14 #include<numeric> 15 #include<utility> 16 #include<sstream> 17 #include<iostream> 18 #include<iomanip> 19 #include<cstdio> 20 #include<cmath> 21 #include<cstdlib> 22 #include<cstring> 23 #include<ctime> 24 #define LL long long 25 #define maxn 100001 26 using namespace std; 27 struct node{ 28 int l , r,m, v, lazy,mx,mi ; 29 }tree[maxn*4]; 30 int mp[4590000]; 31 int mpt; 32 int mpa[maxn]; 33 int n,m,q; 34 char str[100]; 35 struct que{ 36 int status,l,r,v; 37 }qu[maxn]; 38 int L(int x) 39 { 40 return 2* x; 41 } 42 int R(int x) 43 { 44 return 2 * x +1; 45 } 46 void build(int c, int l ,int r) 47 { 48 tree[c].l = l ; 49 tree[c].r = r; 50 tree[c].m = (tree[c].l + tree[c].r)/2; 51 if(l == r) 52 { 53 return; 54 } 55 build(L(c),l,tree[c].m); 56 build(R(c),tree[c].m+1,r); 57 } 58 int mx; 59 int mi; 60 void push_down(int c) 61 { 62 tree[L(c)].lazy += tree[c].lazy; 63 tree[R(c)].lazy += tree[c].lazy; 64 tree[L(c)].mi += tree[c].lazy; 65 tree[L(c)].mx += tree[c].lazy; 66 tree[R(c)].mi += tree[c].lazy; 67 tree[R(c)].mx += tree[c].lazy; 68 tree[c].lazy = 0 ; 69 } 70 void push_up(int c) 71 { 72 tree[c].mi = min(tree[L(c)].mi,tree[R(c)].mi); 73 tree[c].mx = max(tree[L(c)].mx,tree[R(c)].mx); 74 } 75 void query(int c, int l , int r) 76 { 77 if(l <= tree[c].l && r >= tree[c].r) 78 { 79 mi = min(mi,tree[c].mi); 80 mx = max(mx,tree[c].mx); 81 return ; 82 } 83 push_down(c); 84 if(l <= tree[c].m) 85 query(L(c),l,r); 86 if(r > tree[c].m) 87 query(R(c),l,r); 88 push_up(c); 89 } 90 void update(int c, int l ,int r,int v) 91 { 92 //printf("update ! c= %d l = %d r = %d tree[c].l = %d tree[c].r = %d v = %d \n",c,l,r,tree[c].l ,tree[c].r,v); 93 if(l <= tree[c].l && r >= tree[c].r) 94 { 95 // printf("update ! c= %d v = %d \n",c,v); 96 tree[c].mi +=v; 97 tree[c].mx +=v; 98 tree[c].lazy += v; 99 return; 100 } 101 push_down(c); 102 if(l <= tree[c].m) 103 update(L(c),l,r,v); 104 if(r > tree[c].m) 105 update(R(c),l,r,v); 106 push_up(c); 107 } 108 int main(){ 109 //freopen("large-crafted2.in","r",stdin); 110 //freopen("out","w",stdout); 111 while(scanf("%d %d %d",&n,&m,&q)!= EOF) 112 { 113 mpt = 0 ; 114 int ta, tb; 115 for(int i = 1; i <= q;i ++) 116 { 117 scanf("%s",str) ; 118 if(str[0] == ‘s‘) 119 { 120 qu[i].status = 1; 121 scanf("%d",&qu[i].l); 122 mpt ++ ; 123 mpa[mpt] = qu[i].l ; 124 }else if(str[0] == ‘c‘) 125 { 126 qu[i].status = 2; 127 scanf("%d %d",&qu[i].l ,&qu[i].v); 128 mpt ++ ; 129 mpa[mpt] = qu[i].l ; 130 131 }else{ 132 qu[i].status = 3; 133 scanf("%d %d %d",&qu[i].l,&qu[i].r,&qu[i].v); 134 mpt ++ ; 135 mpa[mpt] = qu[i].l ; 136 mpt ++ ; 137 mpa[mpt] = qu[i].r ; 138 } 139 } 140 sort(mpa +1 ,mpa + mpt + 1); 141 int tt = 0 ; 142 for(int i = 1;i <= mpt ;i ++) 143 { 144 if(i != 1 && mpa[i] == mpa[i-1]) 145 continue; 146 mp[mpa[i]] = ++tt ; 147 } 148 build(1,1,tt); 149 int tmp ,tmpa,tmpb; 150 for(int i = 1;i <= q;i ++) 151 { 152 mi = 1e9; 153 mx = 0 ; 154 if(qu[i].status == 1) 155 { 156 tmp = mp[qu[i].l]; 157 query(1,tmp,tmp); 158 printf("%d\n",mx); 159 }else 160 { 161 tmpa = mp[qu[i].l]; 162 tmpb = tmpa; 163 164 if(qu[i].status == 3) 165 tmpb = mp[qu[i].r]; 166 query(1,tmpa,tmpb); 167 //printf("%d %d**%d %d %d\n",tmpa,tmpb,mi,mx,min(m-mx,qu[i].v)); 168 if(qu[i].v > 0){ 169 update(1,tmpa,tmpb,min(m-mx,qu[i].v)) ; 170 printf("%d\n",min(m-mx,qu[i].v)); 171 }else{ 172 update(1,tmpa,tmpb,max(-mi,qu[i].v)) ; 173 printf("%d\n",max(-mi,qu[i].v)); 174 } 175 } 176 } 177 } 178 return 0; 179 }
时间: 2024-10-12 17:28:16