Http://acm.hdu.edu.cn/showproblem.php? PID = 2227 DP + line segment tree

Source: Internet
Author: User

/* <Br/> * what should I say? After writing this question for several days, I feel that the DP + line segment tree has always had a level and cannot be broken through, always struggling in the boundless swamp <br/> * This is a serious setback <br/> * The maintenance of the interval is always difficult to associate with DP, only interval maintenance is required. However, if DP is involved, no problem occurs .. A word, for fear <br/> * it may be a result of struggle. Gradually, we should learn how to analyze the problem and how to view wrong <br/> * at the beginning of coding. I did not understand the question at all, take it for granted that the question is a tragedy. <br/> * The question is the sum of all sequences in a sequence that are not decreasing <br/> */<br /># include <iostream> <br/> # include <cstdio> <br/> # include <algorithm> <br/> # include <cstring> <br/> # define l int <br/> # define LL (X) (x) <1) <br/> # define RR (x) <1 | 1) <br/> using namespace STD; <br/> const int n = 100005; <br/> const int mod = 1000000007; <br/> struct seg_tree {<br/> int L, R; <br/> L sum; <br/> int Mid () {<br/> return (L + r)> 1; <br/>}< br/>} tree [3 * n]; <br/> int POS [N]; <br/> int DF1 [N]; <br/> int df2 [N]; <br/> l dp [N]; <br/> inline void build (int l, int R, int node) {<br/> tree [node]. L = L; <br/> tree [node]. R = r; <br/> tree [node]. sum = 0; <br/> If (L = r) <br/> return; <br/> int mid = (L + r)> 1; <br/> build (L, mid, LL (node); <br/> build (Mid + 1, R, RR (node )); <br/>}< br/> inline void update (INT dx, int node, int sum) {<br/> If (tree [node]. L = tree [node]. R & tree [node]. L = DX) {<br/> tree [node]. sum = (tree [node]. sum + sum) % MOD; // No mod wrong is added here, And a range must be within the MOD range <br/> return; <br/>}< br/>/* <br/> If (tree [node]. sum! =-1) {<br/> tree [LL (node)]. sum = tree [RR (node)]. sum = tree [node]. SUM; <br/> tree [node]. sum =-1; <br/>}< br/> */<br/> int mid = tree [node]. mid (); <br/> If (DX <= mid) <br/> Update (dx, LL (node), sum ); <br/> else <br/> Update (dx, RR (node), sum); <br/>/* <br/> If (r <= mid) {<br/> Update (L, R, LL (node), sum); <br/>}else if (L> mid) {<br/> Update (L, r, RR (node), sum); <br/>}else {<br/> Update (L, mid, LL (No De), sum); <br/> Update (Mid + 1, R, RR (node), sum ); <br/>}< br/> */<br/> tree [node]. sum = (tree [LL (node)]. sum + tree [RR (node)]. sum) % MOD; // interval maintenance <br/>}< br/> inline int query (int l, int R, int node) {<br/> If (L <= tree [node]. L & tree [node]. r <= r) {<br/> return tree [node]. SUM; <br/>}< br/>/* <br/> If (tree [node]. sum! =-1) {<br/> tree [LL (node)]. sum = tree [RR (node)]. sum = tree [node]. SUM; <br/> tree [node]. sum =-1; <br/>}< br/> */<br/> int mid = tree [node]. mid (); <br/> If (r <= mid) {<br/> return query (L, R, LL (node )); <br/>} else if (L> mid) {<br/> return query (L, R, RR (node )); <br/>} else {<br/> return (query (L, mid, LL (node) + query (Mid + 1, R, RR (node ))) % MOD; <br/>}< br/> inline int Bin (int x, int Len) {// Binary <br/> int low = 0; <br/> int high = len-1; <br/> while (low <= high) {<br/> int mid = (low + high)> 1; <br/> If (Pos [Mid] = x) <br/> return mid; <br/> If (Pos [Mid] <X) {<br/> low = Mid + 1; <br/>}else {<br/> high = mid-1; <br/>}< br/> return-1; <br/>}< br/> int main () {<br/> int N; <br/> while (scanf ("% d", & N )! = EOF) {<br/> for (INT I = 0; I <n; I ++) {<br/> scanf ("% d ", & DF1 [I]); <br/> df2 [I] = DF1 [I]; <br/>}< br/> sort (df2, df2 + n ); <br/> int Len = 0; <br/> for (INT I = 0; I <n; I ++) {<br/> if (I = 0 | df2 [I]! = Df2 [I-1]) <br/> POS [Len ++] = df2 [I]; <br/>}// discretization <br/> build (0, len-1, 1); <br/> // memset (DP, 0, sizeof (DP); <br/> for (INT I = 0; I <N; I ++) {<br/> int r = Bin (DF1 [I], Len); <br/> // printf ("R :: % d DF1 [I]: % d/N ", R, DF1 [I]); <br/> L sum = (query (0, R, 1) + 1) % MOD; <br/> // printf ("sum: % d/N", sum ); <br/>/* <br/> If (sum = 0) {<br/> sum = 1; <br/> DP [R] = (DP [R] + sum) % MOD; <br/> Update (R, 1, 1); <br/> continue; <br/>}< br/> */<br/> // DP [R] = (DP [R] + sum) % MOD; <br/> Update (R, 1, sum); <br/>}< br/> printf ("% d/N", query (0, len-1, 1); <br/>/* // This is too troublesome... In fact, it is to calculate the sum of a range <br/> L sum = 0; <br/> for (INT I = 0; I <Len; I ++) {<br/> // printf ("I: % d DP [I]: % d/N", I, DP [I]); <br/> sum = (sum + dp [I]) % MOD; <br/>}< br/> printf ("% d/N", sum ); <br/> */<br/>}< br/> // 9 8 5 32 1 5 6 2 1 <br/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.