cogs--1008. Greedy Continent (Tsinghua Colossus code)--tree-like array

Source: Internet
Author: User
Tags stdin

1008. The Greedy continent

★ Import File: greedisland.in output file: greedisland.out Simple comparison
Time limit: 1 s memory limit: MB

question four: the Greedy continent



"Title description"

In the
face of the ants crazy attack, small FF Tower defense declared failure ... Humans were driven to a bay on the island of greed by ants. Now, the rear of the small FF is the endless sea, the front is a mutant super Ant.  Small FF also has a good future, he does not want to die, so he sent his last batch of reform SCV layout mines to stop ants attack.

The
last line of defense of the small FF is a trench of length n, with small FF having countless mines, and the SCV can bury the same type of mine in the [L, R] zone at a time different from the one previously planted.  Due to the urgency of the situation, the small FF may at some point ask you how many different mines are in the [L ', R '] zone, and he would like you to reply as soon as possible.

"input Format"

The first behavior is two integers n and m; n represents the line length, and M represents the sum of the number of SCV mines and the number of small FF queries.

next there are m lines, three integers per line q,l, R; if q=1 indicates that the SCV has a mine on [L, R] This interval, if q=2 indicates how many mines are in the current [L, R] interval.

"Output format"

for each query of the small FF, output an answer (a single line) that represents the total number of mines in the current interval.



"Input Sample"

5 4

1 1 3

2 2 5

1 2 4

2 3 5

"output Example"

1

2

"Data range"

for 30% data: 0<=n, m<=1000;

for 100% data: 0<=n, m<=10^5.
Code:
One
#include <cstdio>Const intMAXN (100010);intN,m,tot;intt[maxn<<2][2];voidInsertintXintKintleft=1,intRight=n,intp=1){    if(left==Right ) {T[p][k]++; return; }    intMid= (left+right) >>1; if(x<=mid) Insert (x,k,left,mid,p<<1); ElseInsert (x,k,mid+1,right,p<<1|1); t[p][0]=t[p<<1][0]+t[p<<1|1][0]; t[p][1]=t[p<<1][1]+t[p<<1|1][1];}intQueryintXintYintKintleft=1,intRight=n,intp=1){    if(x<=left&&right<=y)returnT[p][k]; intMid= (left+right) >>1, ans=0; if(X<=mid) Ans=ans+query (x,y,k,left,mid,p<<1); if(Y>mid) Ans=ans+query (x,y,k,mid+1,right,p<<1|1); returnans;}intMain () {Freopen ("greedisland.in","R", stdin); Freopen ("Greedisland.out","W", stdout); scanf ("%d%d",&n,&m);  for(intq=1; q<=m;q++){        intTy,x,y; scanf ("%d%d%d",&ty,&x,&y); if(ty==1) {Insert (x,0); Insert (Y,1); Tot++; }        Elseprintf ("%d\n", Tot-query (1, X-1,1)-query (y+1N0)); }    return 0;}

Both

#include <cstdio>Const intMAXN (100010);intN,m,tot;intt[maxn<<2],tag[maxn<<2],mark[maxn<<2];voidIncrease (intPintk) {T[p]+=K; TAG[P]+=K; MARK[P]+=K;}voidPush_down (intp) {    int& d=Tag[p]; Increase (P<<1, D); Increase (P<<1|1, D); D=0;}voidInsertintXintYintleft=1,intRight=n,intp=1){    if(x<=left&&right<=y) {Increase (P,1); return;    } push_down (P); intMid= (left+right) >>1; if(x<=mid) Insert (x,y,left,mid,p<<1); if(y>mid) Insert (x,y,mid+1,right,p<<1|1); T[P]++; if(X<=mid&&y>mid) mark[p]++;}intQueryintXintYintleft=1,intRight=n,intp=1){    if(x<=left&&right<=y)returnT[p];    Push_down (P); intMid= (left+right) >>1, ans=0; if(X<=mid) Ans=ans+query (x,y,left,mid,p<<1); if(Y>mid) Ans=ans+query (x,y,mid+1,right,p<<1|1); if(X<=mid&&y>mid) ans-=Mark[p]; returnans;}intMain () {Freopen ("greedisland.in","R", stdin); Freopen ("Greedisland.out","W", stdout); scanf ("%d%d",&n,&m);  for(intq=1; q<=m;q++){        intTy,x,y; scanf ("%d%d%d",&ty,&x,&y); if(ty==1) Insert (x, y); Elseprintf ("%d\n", query (x, y)); }    return 0;}

cogs--1008. Greedy Continent (Tsinghua Colossus code)--tree-like array

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.