Zoj3299:fall the Brick (interval update)

Source: Internet
Author: User

Now the God was very angry, so he wants to punish the lazy, greedy humans. He chooses to throw some lines of bricks (just down from the very high Heaven). These days the God lives in a 2D world, so he just throw the bricks in a vertical plane. Each time, the God throws a line of bricks. The width of each brick is 1, and the length would be given.

T__nt was a hero in the world and he was trying his best to save the world. Now he had made m horizontal boards in the air with his magic power to stop the bricks. If one brick falls onto a board, it can not be fall down any more. Notice that, for a line of bricks, consecutive bricks is not connected. So when some bricks touch a board, the others would continues to fall down. Now, T__nt wants to know what many bricks each board holds after the God ' s crazy action. He asks you, a acmer, to the help him.

Input

There is no more then ten cases. There is a blank line between consecutive cases. The first line of all case contains integersN,m(0 <N,m<= 100000), indicating the number of lines of bricks and number of horizontal boards made by T__NT.NLines follow, each contains, integersLi,RI(0 <=Li<RI<= 30000000).LiandRIIs the x-coordinates for the left side and the right side of the line of bricks.mLines follow, each contains three integersAI,Bi, andHi(0 <=AI<Bi<= 30000000; 0 <Hi< 1000000000), which means that board I was at heightHiAnd the extreme points areAIandBi. Assume no boards with same height would overlap with each of the other.

Output

For each case, print m lines. The ith line means the number of bricks on board I at last. Print a blank line after each case.

Sample Input

1 21 81 5 83 7 6

Sample Output

42
Test instructions: There are n blocks of brick from the sky, in order not to be smashed to death, get M block plank, each board brick length of 1 units, give the range of continuous board and plank range and height, ask the last of each plank on a few pieces of board brick
Idea: Because of the number of large, the first to disperse, and then located in the height of the plank and the lower plank has coincident words, high place first block, and then to calculate
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define N 100005#define mod 19999997const int INF = 0x3f3f3f3f; #define EXP 1E-8LL Sum[1<<20],col[1<<20],ans[n] ; int Seg[n*4],len;bool flag[1<<20];int n,m;struct brick{int l,r,lid,rid;} a[n];struct board{int l,r,h,lid,rid,id;} B[n];int CMP (board A,board B)//high plank in front {return a.h>b.h;}    void Push_down (int i,int l,int r) {int mid = (l+r)/2;        if (Col[i]) {Col[ls] + = Col[i];        Col[rs] + = Col[i];        Sum[ls] + = (LL) col[i]* (Seg[mid+1]-seg[l]); Sum[rs] + = (LL) col[i]* (seg[r+1]-seg[mid+1]);    Col[i] = 0;    }}void Build (int i,int l,int r) {Sum[i] = col[i] = 0;    Flag[i] = false;    if (l==r) return;    int mid = (l+r)/2;    Build (Ls,l,mid); Build (Rs,mid+1,r);}        void updata1 (int l,int r,int val,int l,int r,int i) {if (l<=l && r<=r) {col[i]+=val;        sum[i]+=val* (Seg[r+1]-seg[l]);    Return    } push_down (I,l,r);    int mid = (l+r)/2;    if (l<=mid) updata1 (L,R,VAL,L,MID,LS);    if (r>mid) updata1 (L,R,VAL,MID+1,R,RS); Sum[i] = Sum[ls]+sum[rs];}    void updata2 (int l,int r,int l,int r,int i) {if (flag[i]) return;        if (l<=l && r<=r) {Flag[i] = true;        sum[i]=0;    Return    } push_down (I,l,r);    int mid = (l+r)/2;    if (l<=mid) updata2 (L,R,L,MID,LS);    if (r>mid) updata2 (L,R,MID+1,R,RS); Sum[i] = Sum[ls]+sum[rs];}    LL query (int l,int r,int l,int r,int i) {if (Flag[i]) return 0;    if (l<=l && r<=r) {return sum[i]; } push_down (i,l, R);    int mid = (l+r)/2;    LL ret = 0;    if (l<=mid) ret+=query (L,R,L,MID,LS);    if (r>mid) ret+=query (L,R,MID+1,R,RS); return ret;}    int main () {int i,j,k;        W (~scanf ("%d%d", &n,&m)) {len = 0;            Up (i,0,n-1) {scanf ("%d%d", &AMP;A[I].L,&AMP;A[I].R);            seg[len++] = A[I].L;        seg[len++] = A[I].R;            } up (i,0,m-1) {scanf ("%d%d%d", &b[i].l,&b[i].r,&b[i].h);            B[i].id = i;            seg[len++] = B[I].L;        seg[len++] = B[I].R;        }//Sort, de-heavy, then discretized sort (seg,seg+len);        Len = Unique (Seg,seg+len)-seg;            Up (i,0,n-1) {a[i].lid = Lower_bound (SEG,SEG+LEN,A[I].L)-seg;        A[i].rid = Lower_bound (SEG,SEG+LEN,A[I].R)-seg;            } up (i,0,m-1) {b[i].lid = Lower_bound (SEG,SEG+LEN,B[I].L)-seg;        B[i].rid = Lower_bound (SEG,SEG+LEN,B[I].R)-seg;     } build (1,0,len-1);//Achievements   Up (i,0,n-1)//each interval has several tiles to update {updata1 (a[i].lid,a[i].rid-1,1,0,len-1,1);        } sort (b,b+m,cmp);            Up (i,0,m-1)//query each board has a few tiles, and then for the blocked part of the 0 processing {ans[b[i].id] = query (b[i].lid,b[i].rid-1,0,len-1,1);        Updata2 (b[i].lid,b[i].rid-1,0,len-1,1);        } up (i,0,m-1) printf ("%lld\n", Ans[i]);    printf ("\ n"); } return 0;}


Zoj3299:fall the Brick (interval update)

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.