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<=TREE[RT].L&&B>=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 <<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)