Title Description
DCrusher has a number of columns, the initial values are 0, N times operation, each time the number of columns [a, b) in this section, all smaller than k number to k, he wants to know the number of columns N operations all elements and. He would also like to play other games, so leave it to you to solve this problem.
answer
This problem because a, b is too large it is not directly maintain tree line and persistence tree line.
After careful study of this question we found this problem do not have online! Offline!
Because it is smaller than the number k is replaced by k, is asking the final answer, it is easy to think that after all the k input sort descending enumeration.
Each simply not turn over in the [a, b) to the interval number k can be, because the turn over number is larger than a certain value is changed current k.
We now consider how to maintain the tree line.
In each recording two values nodes, a representative of the range now how much value is to turn over, a lazy mark.
Change is like looking for a normal change interval, an interval if it finds one, update the answer, $ ans + = (length of the interval - the interval to turn over a number of how much) \ times the current k $.
years + = (r - l + 1 - r [p] .val) * k;
After the interval is set to turn over a few how long, lazy branded mark.
if (x <= l && r <= y) { ans += (r - l + 1 - tr[p].val) * k; tr[p].val = r - l + 1; tr[p].tag = 1; return; }
pushdown operation is also very similar.
void push_down(int p, int l, int r) { if (tr[p].tag) { tr[p].tag = 0; int mid = (l + r) >> 1; if (!tr[p].lson) tr[p].lson = ++tot; tr[tr[p].lson].tag = 1; tr[tr[p].lson].val = mid - l + 1; if (!tr[p].rson) tr[p].rson = ++tot; tr[tr[p].rson].tag = 1; tr[tr[p].rson].val = r - mid; } }
Then we happily A of this question.
Your child is not too naive.
We have found a, b or too big, so be dynamic open point.
Well, this time do not tease you. . .
. 1 #include <the iostream> 2 #include <cstdio> . 3 #include <algorithm> . 4 the using namespace STD; . 5 struct Segment { . 6 int LSON, rson; // left child, right child . 7 int Val; // number of turn over number . 8 int tag; // lazy numerals . 9 } TR [ 1000010 ]; 10 int RT, TOT; . 11 int n-; 12 is int ANS; 13 is struct Node { 14 int a, b; 15 int k; 16 }ASUKA[40010]; 17 void push_down(int p, int l, int r) { 18 if (tr[p].tag) { 19 tr[p].tag = 0; 20 int mid = (l + r) >> 1; 21 if (!tr[p].lson) tr[p].lson = ++tot; 22 tr[tr[p].lson].tag = 1; 23 tr[tr[p].lson].val = mid - l + 1; 24 if (!tr[p].rson) tr[p].rson = ++tot; 25 tr[tr[p].rson].tag = 1; 26 tr[tr[p].rson].val = r - mid; 27 } 28 } 29 void work(int &p, int l, int r, int x, int y, int k) { 30 if (!p) { 31 p = ++tot; 32 } 33 if (x <= l && r <= y) { 34 ans += (r - l + 1 - tr[p].val) * k; 35 tr[p].val = r - l + 1; 36 tr[p].tag = 1; 37 return; 38 } 39 push_down(p, l, r); 40 int mid = (l + r) >> 1; 41 if (x <= mid) work(tr[p].lson, l, mid, x, y, k); 42 if (y > mid) work(tr[p].rson, mid + 1, r, x, y, k); 43 tr[p].val = tr[tr[p].lson].val + tr[tr[p].rson].val; 44 } 45 bool cmp(node x, node y) { 46 return x.k > y.k; 47 } 48 int main() { 49 cin >> n; 50 for (int i = 1; i <= n; i++) { 51 cin >> ASUKA[i].a >> ASUKA[i].b >> ASUKA[i].k; 52 } 53 sort(ASUKA + 1, ASUKA + n + 1, cmp); 54 for (int i = 1; i <= n; i++) { 55 work(rt, 1, 1000000000, ASUKA [i] .a, ASUKA [i] .b - 1 , ASUKA [i] .k); 56 } 57 cout << ans; 58 return 0 ; 59 }
...
...
...
Copy the code certainly not A!
Because the above code did not open longlong ((⊙o⊙) ...).
. 1 #include <the iostream> 2 #include <cstdio> . 3 #include <algorithm> . 4 the using namespace STD; . 5 typedef Long Long LL; . 6 struct Segment { . 7 int LSON, rson; // left child, right child . 8 LL Val; // the number of turn over number . 9 int tag; // lazy numerals 10 } TR [ 1000010 ]; . 11 int RT, TOT; 12 is int n-; 13 is LL ANS; 14 struct node{ 15 int a, b; 16 ll k; 17 }ASUKA[40010]; 18 void push_down(int p, int l, int r) { 19 if (tr[p].tag) { 20 tr[p].tag = 0; 21 int mid = (l + r) >> 1; 22 if (!tr[p].lson) tr[p].lson = ++tot; 23 tr[tr[p].lson].tag = 1; 24 tr[tr[p].lson].val = mid - l + 1; 25 if (!tr[p].rson) tr[p].rson = ++tot; 26 tr[tr[p].rson].tag = 1; 27 tr[tr[p].rson].val = r - mid; 28 } 29 } 30 void work(int &p, int l, int r, int x, int y, ll k) { 31 if (!p) { 32 p = ++tot; 33 } 34 if (x <= l && r <= y) { 35 ans += (r - l + 1 - tr[p].val) * k; 36 tr[p].val = r - l + 1; 37 tr[p].tag = 1; 38 return; 39 } 40 push_down(p, l, r); 41 int mid = (l + r) >> 1; 42 if (x <= mid) work(tr[p].lson, l, mid, x, y, k); 43 if (y > mid) work(tr[p].rson, mid + 1, r, x, y, k); 44 tr[p].val = tr[tr[p].lson].val + tr[tr[p].rson].val; 45 } 46 bool cmp(node x, node y) { 47 return x.k > y.k; 48 } 49 int main() { 50 cin >> n; 51 for (int i = 1; i <= n; i++) { 52 cin >> ASUKA[i].a >> ASUKA[i].b >> ASUKA[i].k; 53 } 54 sort(ASUKA + 1, ASUKA + n + 1, cmp); 55 for (int i = 1; i <= n; i++) { 56 work(rt, 1, 1000000000, Asuka [i] .a, Asuka [i] .b - 1 , Asuka [i] .K); 57 } 58 cout << ans; 59 return 0 ; 60 }