Tyvjoj Topic P1473 Tree outside the Gate 3 (segment tree interval staining type number does not cover)

Source: Internet
Author: User

P1473The tree outside the gate 3 time: 1000ms/Space: 131072kib/java class Name: Main Description There are many trees outside the gate, there are apple trees, banana trees, there will be throwing stones, there can be eaten to replenish the physical ...
Now the school decides to plant a tree at some point in a certain period, to ensure that no two segments of the same tree are present at any one time, there are two operations:
K=1, read into l,r represents a tree planted between the l~r
k=2, read into L,r to ask how many kinds of trees can be seen between l~r
(l,r>0)
Input format the first line n,m indicates that the total road length is n, a total of M operations
Next m behavior m operations
Output format for each k=2 output an answer test sample 1 input
5 4
1 1 3
2 2 5
1 2 4
2 3 5
Output
1
2
Note scope: 20% data Guarantee, n,m<=100
60% data guarantee, N <=1000,m<=50000
100% Data Guarantee, n,m<=50000
Note: Trees can overlap, such as the number 1th can be planted in a variety of trees so that the total number of operations minus the sany between the operands, with two segment tree, update a B, a "0.a-1" are added 1, a "b+1 n" are added 1, The operand that does not work at the time of the query is the second single-point query for A and B's first single-point query and AC code
Record number S1306202 evaluation status accepted title P1473 outside the gate of the tree 3 commit time 2015-08-31 00:02:13 code language C + + consumption time 589 MS consumption memory 2024 KiB

#include <stdio.h> #include <string.h>int n,m;struct s{int l,r;} node[50050<<2];void Build (int l,int r,int tr) {node[tr].l=node[tr].r=0;if (l==r) return;int mid= (l+r) >>1; Build (l,mid,tr<<1); build (mid+1,r,tr<<1|1);} void Insertl (int l,int r,int l,int r,int tr) {if (l<=l&&r<=r) {Node[tr].l++;return;} int mid= (L+R) >>1;if (l<=mid) Insertl (l,r,l,mid,tr<<1), if (R>mid) Insertl (l,r,mid+1,r,tr<<1| 1);} void Insertr (int l,int r,int l,int r,int tr) {if (l<=l&&r<=r) {Node[tr].r++;return;} int mid= (L+R) >>1;if (l<=mid) insertr (l,r,l,mid,tr<<1), if (R>mid) INSERTR (l,r,mid+1,r,tr<<1| 1);} int queryl (int pos,int l,int r,int tr) {if (l==r) return node[tr].l;int mid= (l+r) >>1;if (pos<=mid) {return node[tr ].l+queryl (pos,l,mid,tr<<1);} Elsereturn Node[tr].l+queryl (pos,mid+1,r,tr<<1|1);} int queryr (int pos,int l,int r,int tr) {if (l==r) return node[tr].r;int mid= (l+r) >>1;if (pos<=mid) {return node[tr ].r+queryr(pos,l,mid,tr<<1);} Elsereturn Node[tr].r+queryr (pos,mid+1,r,tr<<1|1);} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {build (0,n,1); int Num=0;while (m--) {int op,a,b;scanf ("%d% d%d ", &op,&a,&b), if (op==1) {Insertl (0,a-1,0,n,1); Insertr (b+1,n,0,n,1); num++;} Else{a=queryr (a,0,n,1); B=queryl (b,0,n,1);p rintf ("%d\n", Num-a-b);}}}


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

Tyvjoj Topic P1473 Tree outside the Gate 3 (segment tree interval staining type number does not cover)

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.