Number of mushrooms
Title Description
A local tyrants There are many kinds of mushroom house (numbered from 1 to n,n<=10000), each house starts with a mushroom, size between 2 to 1000, whenever the free and easy local tyrants watering them, these mushrooms will become the original square times the big, For example, a mushroom size of 2 after local tyrants water will become 4 so big, but because local tyrants live in the Royal Park, each house is only 1000 square meters so large, so each mushroom after 1000 years will not grow up again (for example, a size of 50 mushroom watering, it can only grow to 1000), Local tyrants every day like riding a bike (countryman Envy City people drive to stroll, the town of local tyrants but riding a bicycle, you urban people really will play) to a series of house mushrooms watering, from L to R, and then local tyrants often have to query some room mushrooms a total of how much, good contact sellers, smart you, can help local tyrants this busy? Do not local tyrants a happy to send a house to you casually!
Enter a description
The input is first a T, which indicates a total of T-group data
The next line is an n, which represents local tyrants there are several houses
The next line is n integers, representing the size of mushrooms starting from 1 to n per room (<=1000)
The next line is an integer k (1<=k<=10000), indicating the number of times local tyrants watered and mushrooms,
followed by K-line, three integers per line q,l,r (1<=l<=r<=10000)
Q Indicates the operation of the local tyrants,
Q is 0 when the local tyrants gives l to r the mushrooms in these rooms are watered,
Q is 1 when the local tyrants wants to know the total size of mushrooms in these rooms L to R
Output description
For the nth group of data first output a line "case #n:" and then for each query output an answer, each answer one row
Input sample
1102 3 4 5 6 7 8 9 10 1130 1 101 1 101 1 5
Output sample
Case #1:50590
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace std; const int MAXN = 10000 + 5;int N, T, m;int sum[maxn << 2],col[maxn << 2];bool isupd[maxn << 2];//record his sub-area Whether there are void inline pushup (int rt) {Sum[rt] = Sum[rt << 1] + sum[rt << 1|1) to update if (RT & 1) isupd[(RT + 1)/2] = isupd[rt];//If the child has to be updated, mark the Father node and tell him that the child node has an update else ISUPD[RT/2] = Isupd[r T];} void inline pushdown (int rt) {if (Col[rt]) {col[rt << 1] + = Col[rt]; Col[rt << 1|1] + = Col[rt]; COL[RT] = 0; } isupd[rt << 1] = Isupd[rt]; Isupd[rt << 1|1] = Isupd[rt];} void build (int rt, int l, int r) {Col[rt] = 0; ISUPD[RT] = 0; if (L = = r) {scanf ("%d", &sum[rt]); Return } int mid = (L + r) >> 1; Build (RT << 1, L, mid); Build (Rt << 1|1, Mid + 1, R); Pushup (RT);} void Update (int l, int r, int c, int rt, int l, int r){if (L <= l && R <= R) {Col[rt] + = C; ISUPD[RT] = 1; Return } pushdown (RT); int mid = (L + r) >> 1; if (l <= mid) Update (L, R, C, RT << 1, L, mid); if (R > Mid) Update (L, R, C,rt << 1|1, mid + 1, R); Pushup (RT);} int query (int l, int r, int rt, int l, int r) {if (L = = R) {if (sum[rt] = =) return sum[rt];//must be pruned, otherwise timeout if (Isupd[rt]) {for (int i = 0; i < COL[RT]; i + +) {Sum[rt] = sum[rt] * SUM[RT]; if (Sum[rt] >=) sum[rt] = 1000; } Col[rt] = 0; ISUPD[RT] = 0; } return SUM[RT]; } if (l <= l && R <= r) {int res = SUM[RT]; if (sum[rt] = = (r-l + 1)) return sum[rt];//must be pruned, otherwise timeout if (Isupd[rt]) {///If the child node has an update, then his value will be re-summed to determine R es = 0; int mid = (L + r) >> 1; Pushdown (RT); Res + = query (L, R, RT << 1, L, MID); Res + = query (L, R, RT << 1|1, Mid + 1, R); ISUPD[RT] = 0; } return SUM[RT] = res; } int mid = (L + r) >> 1; int res = 0; if (L <= mid) Res + = query (l, R, RT << 1, L, mid); if (R > Mid) Res + = Query (L, R, RT << 1|1, Mid + 1, R); Pushup (RT); return res;} int main () {int q, L, r, TT = 1; Freopen ("D://imput.txt", "R", stdin); scanf ("%d", &t); while (T--) {scanf ("%d", &n); Build (1, 1, N); scanf ("%d", &m); printf ("Case #%d:\n", TT + +); for (int i = 0; i < m; i + +) {scanf ("%d%d%d", &q, &l, &r); if (q) {printf ("%d\n", query (L, R, 1, 1, n)); } else {update (L, R, 1, 1, 1, N); }}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Acmoj-Number mushroom segment tree interval update