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
|
#include <algorithm> #include <cmath> #include <cstdarg> #include <cstdio> #include <cstring> #include <iostream> #include <typeinfo> #define debug(x) cerr << #x << " = " << x << endl #define debugf(...) fprintf(stderr, __VA_ARGS__) template <class T> bool cmax(T& a, T b) { return b > a ? (a = b, 1) : 0; } template <class T> bool cmin(T& a, T b) { return b < a ? (a = b, 1) : 0; } void read(char* s) { scanf("%s", s); } void read(char& c) { scanf("%c", &c); } template <class T> void read(T& a) { a = 0; char c = getchar(); int f = 0; while (!isdigit(c)) { f ^= c == '-', c = getchar(); } while (isdigit(c)) { a = a * 10 + (c ^ 48), c = getchar(); } a *= f ? -1 : 1; } struct Fastin { template <class T> Fastin& operator>>(T& x) { read(x); return *this; } } fastin; using namespace std;
#define int long long const int MAXN = 2e6 + 10; const int inf = 0x3f3f3f3f3f3f3f3f;
struct Segment { int val; int minv; int maxv; int tag; int fil; Segment() { val = 0; minv = inf; maxv = -inf; tag = 0; fil = 0; } } t[MAXN];
int a[MAXN], n, q;
#define ls ((p) << 1) #define rs (((p) << 1) | 1) #define mid ((l + r) >> 1) #define val(p) t[p].val #define tag(p) t[p].tag #define minv(p) t[p].minv #define maxv(p) t[p].maxv #define fil(p) t[p].fil
void pushup(int p) { val(p) = val(ls) + val(rs); minv(p) = min(minv(ls), minv(rs)); maxv(p) = max(maxv(ls), maxv(rs)); }
void fill(int p, int l, int r, int v) { minv(p) = maxv(p) = fil(p) = v; tag(p) = 0; val(p) = (r - l + 1) * v; }
inline void add(int p, int l, int r, int v) { tag(p) += v; val(p) += v * (r - l + 1); minv(p) += v; maxv(p) += v; }
void pushdown(int p, int l, int r) { if (fil(p) != inf) { fill(ls, l, mid, fil(p)); fill(rs, mid + 1, r, fil(p)); fil(p) = inf; } if (tag(p)) { add(ls, l, mid, tag(p)); add(rs, mid + 1, r, tag(p)); tag(p) = 0; } }
void build_tree(int p, int l, int r) { fil(p) = inf; if (l == r) { val(p) = a[l]; minv(p) = a[l]; maxv(p) = a[l]; } else { build_tree(ls, l, mid); build_tree(rs, mid + 1, r); pushup(p); } }
void modify(int, int, int, int, int, int);
void div(int p, int l, int r, int x, int y, int c) { if (x <= l && r <= y) { int delta_min = (int)(floor(1.0 * minv(p) / c)) - minv(p); int delta_max = (int)(floor(1.0 * maxv(p) / c)) - maxv(p); if (delta_min == delta_max) {
modify(p, l, r, l, r, delta_min); return; } if (minv(p) + delta_min == maxv(p) + delta_max) { fill(p, l, r, minv(p) + delta_min); return; } } pushdown(p, l, r); if (mid >= x) div(ls, l, mid, x, y, c); if (mid < y) div(rs, mid + 1, r, x, y, c); pushup(p); }
void modify(int p, int l, int r, int x, int y, int c) { if (x <= l && r <= y) { add(p, l, r, c); return; } pushdown(p, l, r); if (mid >= x) modify(ls, l, mid, x, y, c); if (mid < y) modify(rs, mid + 1, r, x, y, c); pushup(p); }
int query_sum(int p, int l, int r, int x, int y) { pushdown(p, l, r); if (x <= l && r <= y) { return val(p); } int res = 0; if (mid >= x) res += query_sum(ls, l, mid, x, y); if (mid < y) res += query_sum(rs, mid + 1, r, x, y); return res; }
int query_min(int p, int l, int r, int x, int y) { pushdown(p, l, r); if (x <= l && r <= y) { return minv(p); } int res = inf; if (mid >= x) cmin(res, query_min(ls, l, mid, x, y)); if (mid < y) cmin(res, query_min(rs, mid + 1, r, x, y)); return res; }
void print_tree(int p, int l, int r) { debugf("#%lld\t[%lld, %lld]: ", p, l, r); debugf("%lld / %lld, sum %lld, tag %lld, fil %s\n", minv(p), maxv(p), val(p), tag(p), fil(p) == inf ? "inf" : to_string(fil(p)).c_str()); if (l != r) print_tree(ls, l, mid), print_tree(rs, mid + 1, r); }
void print_array(int p, int l, int r) { if (l == r) return; pushdown(p, l, r); print_array(ls, l, mid); print_array(rs, mid + 1, r); }
#undef ls #undef rs #undef mid #undef val #undef tag #undef minv
signed main() { fastin >> n >> q; for (int i = 1; i <= n; i++) { fastin >> a[i]; } build_tree(1, 1, n); for (int i = 1, op, x, y, c; i <= q; i++) { fastin >> op; switch (op) { case 1: fastin >> x >> y >> c; x++, y++; debugf("---- modifing (%lld, %lld, +%lld) ----\n", x, y, c); modify(1, 1, n, x, y, c); print_tree(1, 1, n); break; case 2: fastin >> x >> y >> c; x++, y++; debugf("---- diving (%lld, %lld, %lld) ----\n", x, y, c); div(1, 1, n, x, y, c); print_tree(1, 1, n); break; case 3: fastin >> x >> y; x++, y++; debugf("---- min (%lld, %lld) ----\n", x, y); printf("%lld\n", query_min(1, 1, n, x, y)); print_tree(1, 1, n); break; case 4: fastin >> x >> y; x++, y++; debugf("---- sum (%lld, %lld) ----\n", x, y); printf("%lld\n", query_sum(1, 1, n, x, y)); print_tree(1, 1, n); break; } } return 0; }
|