HDU 1556 to a continuous ball coloring-line tree-(interval update, single point query)

Source: Internet
Author: User

Test instructions: There are n balls, 1~n,n operations: (A, b), meaning that the interval [a, b] in the ball is painted once, n times after the operation, asked each ball was coated how many times.

Analysis:

Interval update, single point query. General interval update to use LAZY[RT], or the update operation on the line tree is degraded in order to update the violence, will time out.

Code:

#include <iostream> #include <cstdio> #include <algorithm>using namespace std;const int maxn=100000; int n;int tot[maxn*4+10],lazy[maxn*4+10];struct node{int l,r;} tree[maxn*4+10];void pushup (int rt) {tot[rt]=tot[rt<<1]+tot[rt<<1|1];} void pushdown (int rt) {if (lazy[rt]!=0) {if (TREE[RT].L==TREE[RT].R) {Tot[rt]+=lazy[rt];return;} lazy[rt<<1]+=lazy[rt];lazy[rt<<1|1]+=lazy[rt];lazy[rt]=0;}} void creat (int l,int r,int RT) {tree[rt].l=l;tree[rt].r=r;lazy[rt]=0;if (l==r) {Tot[rt]=0;return;} int mid= (L+R) >>1;creat (l,mid,rt<<1); creat (mid+1,r,rt<<1|1);} void upd (int a,int b,int RT) {if (A&LT;=TREE[RT].L&AMP;&AMP;B&GT;=TREE[RT].R) {Lazy[rt]+=1;return;} Pushdown (RT), int mid= (TREE[RT].L+TREE[RT].R) >>1;if (a<=mid) upd (a,b,rt<<1), if (B>mid) upd (a,b,rt &LT;&LT;1|1);} void query (int rt) {pushdown (RT), if (TREE[RT].L==TREE[RT].R) {if (tree[rt].l==1) printf ("%d", Tot[rt]), Else printf ("%d" , Tot[rt]); return;} Query (rt<<1); query (rt<<1|1);} int main () {WHile (scanf ("%d", &n)!=eof) {if (!n) break;creat (1,n,1); for (int i=0;i<n;i++) {int a,b;scanf ("%d%d",&a,& b); upd (a,b,1);} for (int i=1;i<=20;i++) cout<<lazy[i]<< ""; query (1);p rintf ("\ n");}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1556 to a continuous ball coloring-line tree-(interval update, single point query)

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.