P2184 Greedy Continent Topic
In fact, it's easy to understand how many different intervals there are in an interval.
Then think about it and find out:
1. As long as an interval begins at the left of a node I, the interval is included in the interval 1~i.
2. As long as the tail of an interval is on the left side of a node J, then this interval must not belong to all the intervals after J
It is not difficult to think of two tree-like arrays for maintenance:
First: How many intervals begin before maintaining node I
The second: How many intervals end before maintaining node J
It is not difficult to prove that taking SUM[I]-SUM[J] is the number of mines in the middle of i~j (manual simulation of a wave will be clear)
#include <iostream>#include<cstdlib>#include<cstdio>#include<cmath>#include<cstring>#include<iomanip>#include<algorithm>#include<stack>#include<queue>#defineLST Long Long#defineRG Register#defineN 100050using namespacestd; intn,m; LST ans; intTou[n],wei[n];//The beginning of the number of intervals in front of the TOU, hereinafter referred to as the head tree-like array//Wei the tail of the number of intervals in front, hereinafter referred to as the trailing tree-like array//similar to prefixes andInlineintRead ()//read-in optimization{RGints=0, m=1; RGCharCh=GetChar (); while(ch!='-'&& (ch<'0'|| Ch>'9')) ch=GetChar (); if(ch=='-') m=-1, ch=GetChar (); while(ch>='0'&&ch<='9') s= (s<<3) + (s<<1) +ch-'0', ch=GetChar (); returns*m; } //The following is a tree-like array of boardsInlineintLowbit (RGintKk//Lowbit { returnkk& (-KK); } InlinevoidAdd_tou (RGintKk//array of headers added to the tree array { while(kk<=N) {++TOU[KK]; KK+=lowbit (KK); }} inlinevoidAdd_wei (RGintKk//joins the trailing array of the tree-like array { while(kk<=N) {++WEI[KK]; KK+=lowbit (KK); }} inlineintSum_tou (RGintKk//the start of the number of intervals before the compute node{RGints=0; while(kk>0) {s+=TOU[KK]; KK-=lowbit (KK); } returns; } InlineintSum_wei (RGintKk//the end of the number of intervals before the compute node{RGints=0; while(kk>0) {s+=WEI[KK]; KK-=lowbit (KK); } returns; } intMain () {n=read (), M=read ();//Read in for(RGintI=1; i<=m;++i) {RGintsign=read (); RGintX=read (), Y=read ();//Read in if(sign==1) {Add_tou (x);//Adding a tree-like array to the headAdd_wei (y);//Add a trailing tree-like array } Else{ans=sum_tou (y)-sum_wei (x1);//use the proven rules to knot problemsprintf"%d\n", ans); } } return 0; }
Through this problem, we can find that most of the tree-like array of topics can be used in line tree, but there are also line segment tree is not good maintenance of the topic, which requires a flexible use of the tree array of techniques (although there are also line tree in the puzzle) I think my code is pretty good, but the variable is a little ugly, but good
luoguP2184 greedy Continental puzzle (tree-like array)