Codevs 1299 Segment Tree interval update query

Source: Internet
Author: User

1299 Cut Fruit

time limit: 1 sspace limit: 128000 KBtopic rank: Master Master SolvingView Run ResultsTitle Description Description

Simply put, a total of n fruit in a row, cut m times, each cut [l,r] interval of all the fruit (may have the fruit is repeated cut), the output of the remaining fruit after each cut

The data has been reassembled without an OLE error

Time limits and data ranges are appropriately modified to avoid excessive data packets and waste of space resources

Enter a description Input Description

The 1th line consists of 2 positive integers, respectively, n,m.

The next m line has two positive integers per row L, R

Output description Output Description

Total output m line, each line output after cutting the amount of fruit left

Sample input Sample Input

10 3

3 5

2 8

1 5

Sample output Sample Output

7

3

2

Data range and Tips Data Size & Hint

30% of data Meet n,m<=5,000

60% of data Meet n,m<=100,000

100% of data Meet 1<=l<=r<=n<=500,000,1<=m<=500,000

Test instructions: N fruit rows in a row m queries each time you delete the fruit in the [l,r] interval and output the amount of the remaining fruit

Troubleshooting: Line Tree processing problem data There are l<0 r>n data

1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^    ^ ^5 o o6 ******************************/7#include <bits/stdc++.h>8#include <iostream>9#include <cstring>Ten#include <cstdio> One#include <map> A#include <algorithm> -#include <queue> - #definell __int64 the using namespacestd; - structnode - { -     intL,r; +     intW; -     intadd; +}tree[2000005]; A voidBuildtree (intRootintLeftintRight ) at { -Tree[root].l=Left ; -Tree[root].r=Right ; -Tree[root].add=0; -     if(left==Right ) -     { intree[root].w=1; -         return ; to     } +     intMid= (left+right) >>1; -Buildtree (root<<1, left,mid); theBuildtree (root<<1|1, mid+1, right); *tree[root].w=tree[root<<1].w+tree[root<<1|1].W; $ }Panax Notoginseng voidPushdown (introot) - { the     if(tree[root].add==0)return; +tree[root<<1].add=1; Atree[root<<1|1].add=1; theTree[root].add=0; +tree[root<<1].w=0; -tree[root<<1|1].w=0; $ } $ voidUpdata (intRootintLeftintRightintc) - { -     if(tree[root].l==left&&tree[root].r==Right ) the     { -Tree[root].add=C;Wuyitree[root].w=0; the         return ; -     } Wu pushdown (root); -     intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; About     if(right<=mid) $     { -Updata (root<<1, left,right,c); -     } -     Else A     { +         if(left>mid) theUpdata (root<<1|1, left,right,c); -         Else $         { theUpdata (root<<1, left,mid,c); theUpdata (root<<1|1, mid+1, right,c); the         } the     } -tree[root].w=tree[root<<1].w+tree[root<<1|1].W; in } the intQueryintRoot,intLeftintRight ) the { About     if(tree[root].l==left&&tree[root].r==Right ) the     { the         returnTREE[ROOT].W; the     } + pushdown (root); -     intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; the     if(mid<=Right )Bayi         returnQuery (root<<1, left,right); the     Else the     { -         if(left>mid) -             returnQuery (root<<1|1, left,right); the         Else the             returnQuery (root<<1, Left,mid) +query (root<<1|1, mid+1, right); the     } the } - intn,m; the intLe,ri; the intMain () the {94      while(SCANF ("%d%d", &n,&m)! =EOF) the     { theBuildtree (1,1, n); the          for(intI=1; i<=m;i++)98         { Aboutscanf"%d%d",&le,&RI); -Updata (1, Max (1, le), Min (N,ri),1);101printf"%d\n", Query (1,1, N));102         }103     }104     return 0; the}

Codevs 1299 Segment Tree interval update 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.