Hdu1556 tree Array

Source: Internet
Author: User

 

 

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1556

 

Dyeing: dye [a, B] each time, and then query the number of dyeing times of all 1-N nodes...

I have done this before, but I have not understood the mysteries of reverse use of the makeup array... In fact, it is to reverse the regular query and update operations...

If [a, B] is updated, adding 1 to position a is equivalent to adding 1 to all the places next to this position, then, reducing the position B + 1 by 1 is equivalent to reducing the position after this position by 1. If the position X to be searched next time is behind this range, certainly it will not be added with 1. If X is in the middle of a and B, it must be added with 1...

 

Later, the speechless Daniel said that tree arrays could not be used... Sort the query first, and then add 1 minus 1 according to the above idea... Because there is no intermediate query for this question...

 

The two-dimensional pku2155, the same...

 

 

Code;

 

#include <stdlib.h>#include <conio.h>#include <malloc.h>#include <time.h>#include <string.h>#include <stdio.h>#include <memory.h>#include <vector>#include <string>#include <stack>#include <queue>#include <fstream>#include <cmath>#include <iomanip>#include <time.h>#include <stdio.h>#include <algorithm>#include <cmath>#include <iostream>using namespace std;const int N=100010;int n, m, ans;int a[N], sum[N];vector<int> xx[N], yy[N];int query(int i){int tmp = 0;for(; i>0; i-=i&(-i))tmp += sum[i];return tmp;}void update(int i, int v){for(; i<=n; i+=i&(-i))sum[i] += v;}int main(){int i, j, k, x, y, cas;while(scanf("%d", &n)!=EOF){if(n==0)break;for(i=1; i<=n; i++){sum[i] = 0;}for(i=1; i<=n; i++){scanf("%d%d", &x, &y);update(x, 1);update(y+1, -1);}for(i=1; i<n; i++)printf("%d ", query(i));printf("%d\n", query(i));}return 0;}

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.