1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| template<class _Tp>class Segment_Tree{ #define N 100005 #define Tree_sum_tag 1 #define Tree_add_tag 1 public: _Tp a[N]; int n; #ifdef Enable_Mod _Tp mod=_Tp(0); #endif private: #define N 100005 #ifdef Tree_sum_tag _Tp sum[N<<2]; #endif #ifdef Tree_max_tag _Tp mx[N<<2]; #endif #ifdef Tree_min_tag _Tp mn[N<<2]; #endif #ifdef Tree_add_tag _Tp taga[N<<2]; #endif #ifdef Tree_mul_tag _Tp tagm[N<<2]; #endif inline int lson(int p){return p<<1;} inline int rson(int p){return (p<<1)|1;} inline void push_up(int p){ #ifdef Tree_sum_tag sum[p]=sum[lson(p)]+sum[rson(p)]; #ifdef Enable_Mod sum[p]%=mod; #endif #endif #ifdef Tree_min_tag mn[p]=min(mn[lson(p)],mn[rson(p)]); #endif #ifdef Tree_max_tag mx[p]=max(mx[lson(p)],mx[rson(p)]); #endif } inline void push_down(int p,int l,int r){ register int mid=l+r>>1; #ifdef Tree_mul_tag tagm[lson(p)]*=tagm[p],tagm[rson(p)]*=tagm[p]; #ifdef Enable_Mod tagm[lson(p)]%=mod,tagm[rson(p)]%=mod; #endif #ifdef Tree_sum_tag sum[lson(p)]*=tagm[p],sum[rson(p)]*=tagm[p]; #ifdef Enable_Mod sum[lson(p)]%=mod,sum[rson(p)]%=mod; #endif #endif #ifdef Tree_add_tag taga[lson(p)]*=tagm[p],taga[rson(p)]*=tagm[p]; #ifdef Enable_Mod taga[lson(p)]%=mod,taga[rson(p)]%=mod; #endif #endif #ifdef Tree_min_tag mn[lson(p)]*=tagm[p],mn[rson(p)]*=tagm[p]; #ifdef Enable_Mod mn[lson(p)]%=mod,mn[rson(p)]%=mod; #endif #endif #ifdef Tree_max_tag mx[lson(p)]*=tagm[p],mx[rson(p)]*=tagm[p]; #ifdef Enable_Mod mx[lson(p)]%=mod,mx[rson(p)]%=mod; #endif #endif #endif #ifdef Tree_add_tag taga[lson(p)]+=taga[p],taga[rson(p)]+=taga[p]; #ifdef Enable_Mod taga[lson(p)]%=mod,taga[rson(p)]%=mod; #endif #ifdef Tree_sum_tag sum[lson(p)]+=(mid-l+1)*taga[p],sum[rson(p)]+=(r-mid)*taga[p]; #ifdef Enable_Mod sum[lson(p)]%=mod,sum[rson(p)]%=mod; #endif #endif #ifdef Tree_max_tag mx[lson(p)]+=taga[p],mx[rson(p)]+=taga[p]; #ifdef Enable_Mod mx[lson(p)]%=mod,mx[rson(p)]%=mod; #endif #endif #ifdef Tree_min_tag mn[lson(p)]+=taga[p],mn[rson(p)]+=taga[p]; #ifdef Enable_Mod mn[lson(p)]%=mod,mn[rson(p)]%=mod; #endif #endif #endif #ifdef Tree_mul_tag tagm[p]=_Tp(1); #endif #ifdef Tree_add_tag taga[p]=_Tp(0); #endif } #ifdef Tree_add_tag inline void _add(int p,int nl,int nr,int l,int r,int k){ if(nl<=l&&nr>=r){ taga[p]+=k; #ifdef Enable_Mod taga[p]%=mod; #endif #ifdef Tree_sum_tag sum[p]+=(r-l+1)*k; #ifdef Enable_Mod sum[p]%=mod; #endif #endif #ifdef Tree_max_tag mx[p]+=k; #ifdef Enable_Mod mx[p]%=mod; #endif #endif #ifdef Tree_min_tag mn[p]+=k; #ifdef Enable_Mod mn[p]%=mod; #endif #endif return; } if(nl>r||nr<l)return ; push_down(p,l,r); register int mid=l+r>>1; _add(lson(p),nl,nr,l,mid,k); _add(rson(p),nl,nr,mid+1,r,k); push_up(p); } #endif #ifdef Tree_mul_tag inline void _mul(int p,int nl,int nr,int l,int r,_Tp k){ if(nl<=l&&nr>=r){ tagm[p]*=k; #ifdef Enable_Mod tagm[p]%=mod; #endif #ifdef Tree_sum_tag sum[p]*=k; #ifdef Enable_Mod sum[p]%=mod; #endif #endif #ifdef Tree_add_tag taga[p]*=k; #ifdef Enable_Mod taga[p]%=mod; #endif #endif #ifdef Tree_max_tag mx[p]*=k; #ifdef Enable_Mod mx[p]%=mod; #endif #endif #ifdef Tree_min_tag mn[p]*=k; #ifdef Enable_Mod mn[p]%=mod; #endif #endif return; } if(nl>r||nr<l)return; push_down(p,l,r); register int mid=l+r>>1; _mul(lson(p),nl,nr,l,mid,k); _mul(rson(p),nl,nr,mid+1,r,k); push_up(p); } #endif #ifdef Tree_sum_tag inline _Tp _ask_sum(int p,int nl,int nr,int l,int r){ if(nl<=l&&nr>=r)return sum[p]; if(nl>r||nr<l)return _Tp(0); push_down(p,l,r); register int mid=l+r>>1; register _Tp res=0; res+=_ask_sum(lson(p),nl,nr,l,mid); #ifdef Enable_Mod res%=mod; #endif res+=_ask_sum(rson(p),nl,nr,mid+1,r); #ifdef Enable_Mod res%=mod; #endif return res; } #endif #ifdef Tree_max_tag inline _Tp _ask_max(int p,int nl,int nr,int l,int r){ if(nl<=l&&nr>=r)return mx[p]; if(nl>r||nr<l)return _Tp(-INF); push_down(p,l,r); register int mid=l+r>>1; register _Tp res=_Tp(-INF); res=max(res,_ask_max(lson(p),nl,nr,l,mid)); res=max(res,_ask_max(rson(p),nl,nr,mid+1,r)); return res; } #endif #ifdef Tree_min_tag inline _Tp _ask_min(int p,int nl,int nr,int l,int r){ if(nl<=l&&nr>=r)return mn[p]; if(nl>r||nr<l)return _Tp(INF); push_down(p,l,r); register int mid=l+r>>1; register _Tp res=_Tp(INF); res=min(res,_ask_min(lson(p),nl,nr,l,mid)); res=min(res,_ask_min(rson(p),nl,nr,mid+1,r)); return res; } #endif inline void _build(int p,int l,int r){ #ifdef Tree_add_tag taga[p]=_Tp(0); #endif #ifdef Tree_mul_tag tagm[p]=_Tp(1); #endif if(l==r){ #ifdef Tree_sum_tag sum[p]=a[l]; #endif #ifdef Tree_min_tag mn[p]=a[l]; #endif #ifdef Tree_max_tag mx[p]=a[l]; #endif #ifdef Tree_min_tag mn[p]=a[l]; #endif return ; } register int mid=l+r>>1; _build(lson(p),l,mid); _build(rson(p),mid+1,r); push_up(p); } public: inline void build(){_build(1,1,n);} #ifdef Tree_add_tag inline void add(int l,int r,int k){_add(1,l,r,1,n,k);} #endif #ifdef Tree_mul_tag inline void mul(int l,int r,int k){_mul(1,l,r,1,n,k);} #endif #ifdef Tree_sum_tag inline _Tp ask_sum(int l,int r){return _ask_sum(1,l,r,1,n);} #endif #ifdef Tree_min_tag inline _Tp ask_min(int l,int r){return _ask_min(1,l,r,1,n);} #endif #ifdef Tree_max_tag inline _Tp ask_max(int l,int r){return _ask_max(1,l,r,1,n);} #endif inline void clear(){ memset(a,0,sizeof(a)); #ifdef Tree_sum_tag memset(sum,0,sizeof(sum)); #endif } }
|