HDU 1556 line segment tree

Source: Internet
Author: User
/* If the HDU 1556 line segment tree needs to update data in segments, otherwise timeout will occur !~~~~ */# Include <cstdio> # include <cstring> using namespace STD; # define maxn limit 5int ans [maxn]; int K; struct node {int L, R, V ;} node [maxn * 4]; int N; void build (int I, int ls, int RS) {node [I]. L = ls; node [I]. R = RS; node [I]. V = 0; If (LS = RS) return; int M = (LS + RS)> 1; build (I <1, ls, M ); build (I <1) + 1, m + 1, RS); return;} void Update (INT ls, int RS, int I) {If (node [I]. L = LS & node [I]. R = RS) // very important. It is a segment update. If it does not exist, it times out. {node [I ]. V ++; return;} // If (node [I]. L = LS & node [I]. R = RS) // {// int mid = (LS + RS)/2; // Update (LS, mid, I * 2 ); // Update (Mid + 1, RS, I * 2 + 1); // return; //} int M = (node [I]. L + node [I]. r)> 1; if (RS <= m) Update (LS, RS, I <1); else if (LS> m) Update (LS, RS, (I <1) + 1); else {Update (LS, M, I <1); Update (m + 1, RS, (I <1) + 1);} return;} void solve (int I) {// printf ("% d \ n", I); For (Int J = node [I]. l; j <= node [I]. r; j ++) ans [J] + = node [I]. v; If (node [I]. L = node [I]. r) return; solve (I * 2); solve (I * 2 + 1);} int main () {While (scanf ("% d", & N ), n! = 0) {k = 0; build (1, 1, n); memset (ANS, 0, sizeof (ANS); // For (INT I = 1; I <= 10; I ++) // printf ("% d \ n", I, node [I]. l, node [I]. r); int A, B; For (INT I = 1; I <= N; I ++) {scanf ("% d", &, & B); Update (A, B, 1); // For (INT I = 1; I <= 20; I ++) // printf ("% d \ n", I, node [I]. l, node [I]. r, node [I]. v);} // For (INT I = 1; I <= 10; I ++) // printf ("% d \ n ", i, node [I]. l, node [I]. r, node [I]. v); // For (INT I = 0; I <n; I ++) // printf ("% d", ANS [I]); solve (1 ); printf ("% d", ANS [1]); For (INT I = 2; I <= N; I ++) printf ("% d ", ans [I]); // For (INT I = 1; I <= 20; I ++) // printf ("% d \ n", I, node [I]. l, node [I]. r, node [I]. v); // print (1); printf ("\ n ");}}

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.