UVA 12436-rip Van Winkle ' s Code
Topic links
Test instructions: Interval modifies one add arithmetic progression, one sets the interval to a value, then asks the interval and
Idea: The key lies in the place of the arithmetic progression, each node of the segment tree adds a first item and a tolerance, because arithmetic progression plus a arithmetic progression or a arithmetic progression, can be maintained by this nature, note that the set operation will overwrite the operation of arithmetic progression
Code:
#include <cstdio> #include <cstring> #define Lson (x) ((x<<1) +1) #define Rson (x) ((x<<1) +2) typedef long LONG ll;const int N = 250005;int n;struct Node {ll L, R, A1, D, C, Val;int setc;} node[n * 4];void build (ll l , ll r, int x = 0) {node[x].l = l; node[x].r = r;node[x].a1 = NODE[X].D = Node[x].val = Node[x].setc = 0;if (L = = r) retur N;ll mid = (L + R)/2;build (L, Mid, Lson (x)); Build (mid + 1, R, Rson (x));} void pushup (int x) {node[x].val = Node[lson (x)].val + node[rson (x)].val;} void pushdown (int x) {if (node[x].setc) {Node[lson (x)].c = Node[rson (x)].c = Node[x].c;node[lson (x)].val = (Node[lson (x)] . R-node[lson (x)].L + 1) * Node[x].c;node[rson (x)].val = (Node[rson (x)].r-node[rson (x)].L + 1) * Node[x].c;node[lson (x) ].SETC = Node[rson (x)].setc = 1;node[lson (x)].a1 = Node[lson (x)].d = Node[rson (x)].a1 = Node[rson (x)].d = 0;node[x].setc = 0;} Node[lson (x)].a1 + = node[x].a1; Node[lson (x)].d + = Node[x].d;ll L = node[x].l, r = node[x].r;ll mid = (L + r)/2;ll amid = node[X].A1 + node[x].d * (mid-l + 1); ll len1 = (mid-l + 1), Len2 = (r-mid); Node[lson (x)].val + = node[x].a1 * len1 + len1 * (LEN1-1)/2 * Node[x].d;node[rson (x)].a1 + amid;node[rson (x)].d + = Node[x].d;node[rson (x)].val + = amid * len2 + len2 * (len2-1)/2 * node[x].d;node[x].a1 = NODE[X].D = 0;} void A (ll L, LL R, ll d, int x = 0) {if (node[x].l >= l && node[x].r <= R) {LL st = node[x].l-l + 1;if (d = =-1) st = r-node[x].l + 1;node[x].a1 + = st;node[x].d + d;ll len = node[x].r-node[x].l + 1;node[x].val + = St * Len + Len * (len-1)/2 * D;return;} Pushdown (x); ll mid = (NODE[X].L + NODE[X].R)/2;if (L <= mid) A (L, R, D, Lson (x)), if (R > Mid) A (L, R, D, Rson (x)); Pushup (x);} void C (ll L, LL R, ll C, int x = 0) {if (node[x].l >= l && node[x].r <= r) {node[x].setc = 1;NODE[X].C = C;n Ode[x].val = (NODE[X].R-NODE[X].L + 1) * c;node[x].a1 = NODE[X].D = 0;return;} Pushdown (x); ll mid = (NODE[X].L + NODE[X].R)/2;if (L <= mid) C (L, R, C, Lson (x))if (R > Mid) C (L, R, C, Rson (x));p ushup (x);} ll S (ll L, ll r, int x = 0) {if (node[x].l >= l && node[x].r <= R) return Node[x].val;pushdown (x); ll mid = (n ODE[X].L + node[x].r)/2;ll ans = 0;if (L <= mid) ans + = s (l, R, Lson (x)); if (R > Mid) ans + = s (l, R, Rson (x));p Ush Up (x); return ans;} int main () {while (~SCANF ("%d", &n)) {build (1, 250000); ll A, B, C;char q[2];while (n--) {scanf ("%s%lld%lld", Q, &a , &b), if (q[0] = = ' C ') scanf ("%lld", &c), if (q[0] = = ' A ') A (a, B, 1), if (q[0] = = ' B ') A (a, B,-1); if (q[0] = = ' C ') C (A, B, c); if (q[0] = = ' s ') printf ("%lld\n", S (A, b));}} return 0;}
UVA 12436-rip Van Winkle ' s Code (line tree)