Ticket sales system

Source: Internet
Author: User

247. Ticket Sales System

★★☆Input file:railway.inOutput file:railway.outSimple comparison
Time Limit: 1 s memory limit: 128 MIB

[Problem description]

A certain train passes through C cities with 1-C city numbers in sequence. There are a total of S seats on the train. The Railway Bureau stipulates that only one ticket can be sold, that is, all passengers on the train have seats. The ticket sales system is executed by a computer. Each ticket sales request contains three parameters, which are represented by O, D, and N respectively. O is the start station, D is the destination station, and N is the ticket count. The ticket sales system determines whether the ticket sales application is accepted or not accepted, this ticket is accepted only when there are N or more empty seats on the train from o to D. Please write a program to implement this Automatic Ticketing System.

[Input format]

The first line contains three integers C, S, and r separated by spaces, where 1 ≤ C ≤ 60000, L ≤ S ≤ 60000,1 ≤ r ≤ 60000. C indicates the number of cities, s indicates the number of seats on the train, and r indicates the total number of ticket sales applications. In the next R row, each ticket is requested. It is represented by three integers (O, D, and N) separated by spaces. O is the start station, D is the destination station, and N is the number of ticket stations, among them, 1 ≤ d ≤ C, 1 ≤ o ≤ C, and all ticket sales applications are provided from morning till night Based on the application time.

[Output format]

There are a total of R rows output. Each row outputs "yes" or "no", indicating that the current ticket sales application is accepted or not accepted.

[Input and output sample]

Input:

4 6 4

1 4 2

1 3 2

2 4 3

1 2 3

Output:

Yes

Yes

No

No

The lazy flag of the Line Segment tree .. The key is to handle the mark. When to delegate. When to play back. When updating the value of an interval, the total number of such intervals has been added. Therefore, you only need to add the subtree of this interval in the next operation. At the same time, we need to update Father's Day.

The following is the AC code:

#include<cstdio>#include<algorithm>using namespace std;const int maxn = 60000+10;struct node{    int l,r;    int tot;    int lazy;}T[maxn<<2];int s;void build(int id,int l,int r){    T[id].l=l; T[id].r=r; T[id].tot=0;T[id].lazy=0;    if(l==r) return;    int m=(l+r)>>1;    build(id<<1,l,m); build(id<<1|1,m+1,r);}void update(int id,int l,int r,int n){     if(T[id].l==l&&T[id].r==r){         T[id].lazy+=n;  T[id].tot+=n;  return ;     }      if(T[id].lazy!=0){        T[id<<1].tot+=T[id].lazy; T[id<<1|1].tot+=T[id].lazy;        T[id<<1].lazy+=T[id].lazy; T[id<<1|1].lazy+=T[id].lazy;        T[id].lazy=0;     }     int m=(T[id].l+T[id].r)>>1;     if(m>=r)   update(id<<1,l,r,n);     else  if( l>m)    update(id<<1|1,l,r,n);     else{       update(id<<1,l,m,n);       update(id<<1|1,m+1,r,n);     }     T[id].tot=max(T[id<<1].tot,T[id<<1|1].tot);}int query(int id,int l,int r){    if(T[id].l==l&&T[id].r==r) return T[id].tot;     if(T[id].lazy!=0){        T[id<<1].tot+=T[id].lazy; T[id<<1|1].tot+=T[id].lazy;        T[id<<1].lazy+=T[id].lazy; T[id<<1|1].lazy+=T[id].lazy;        T[id].lazy=0;    }    int m=(T[id].l+T[id].r)>>1;    if(m>=r) return  query(id<<1,l,r);    else  if( l>m)  return  query(id<<1|1,l,r);    else{       return   max(query(id<<1,l,m),query(id<<1|1,m+1,r));    }}int main(){    freopen("railway.in","r",stdin);    freopen("railway.out","w",stdout);    int c,r;    int left,right,n;    scanf("%d%d%d",&c,&s,&r);    build(1,1,c+1);    for(int i=0;i<r;i++){        scanf("%d%d%d",&left,&right,&n);  right--;        int sum=query(1,left,right);        if(s-sum>=n)   update(1,left,right,n),     puts("YES");        else                                     puts("NO");    }    return 0;}

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.