"cogs247" Ticket System "segment tree"

Source: Internet
Author: User

"Problem description"

A train through the city of C, the city number is 1 to C, the train has a seat of S, the Railway Bureau stipulated that tickets sold can only be tickets, that is, all passengers on the car have a seat. Ticketing system is performed by the computer, each ticket application contains three parameters, respectively, with O, D, N, O for the starting station, D for the destination station, N for the ticket number. The ticketing system's decision to accept or not entertain the ticket application is only accepted if there are N or more than one empty seat 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 separated by spaces C, S, and R, where 1≤c≤60000, l≤s≤60000,1≤r≤60000. C is the city number, S is the number of seats on the train, R is the total number of ticket applications. The next R line for each action a ticket application, with three spaces separated by the integer o,d and N, O for the starting station, D for the destination station, N for the number of ticket stations, of which 1≤d≤c,1≤o≤c, all the ticket application according to the time from morning to night given.

"Output Format"

Outputs a total of r rows, each output a "YES" or "no", indicating that the current ticket application is accepted or not entertained.

"Input and Output sample"

Input:


4 6 41 4 21 3 22 4 31 2 3


Output:


Yesyesnono
FIX: Interval change, interval query maximum. Output no when the maximum number of seats is less than the required number of votes. Other output yes.
#include <iostream> #include <cstdio>using namespace Std;int t[1000001],n,d,o,p[1000001],maxx,minn,s,r,c ; int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline void paint (int k,int l,int R,int v) {t[k]+=v;if (t[k]>s) t[k]=s; p[k]+=v;} inline void pushdown (int k,int l,int r) {int mid;mid= (l+r)/2;paint (k*2,l,mid,p[k]);p aint (k*2+1,mid+1,r,p[k]);p [k]=0;}  inline void Add (int k,int l,int r,int ll,int rr,int v) {int mid;if (LL&LT;=L&AMP;&AMP;R&LT;=RR) {paint (k,l,r,v); return;    }mid= (l+r)/2;if (ll<=mid) Add (k*2,l,mid,ll,rr,v), if (rr>mid) Add (k*2+1,mid+1,r,ll,rr,v); T[k]=max (t[k*2],t[k*2+1]);} inline int Qmax (int k,int l,int r,int ll,int rr) {int Mid,maxx ( -1); if (LL&LT;=L&AMP;&AMP;R&LT;=RR) return t[k];p Ushdown (k, L,R), mid= (l+r)/2;if (ll<=mid) Maxx=max (Maxx,qmax (K*2,L,MID,LL,RR)), if (Rr>mid) Maxx=max (Maxx,qmax (k*2+1,mid+    1,R,LL,RR)); Return Maxx;} int main () {freopen ("railway.in", "R", stdin), Freopen ("Railway.out", "w", stdout); C=read (); S=read ();  R=read (); for (int i=1;i<=r;i++) {o=read ();d =read (); N=read (); Maxx=qmax (1,1,c-1,o,d-1); if (s-maxx<n) printf ("no\n");  else {printf ("yes\n"); Add (1,1,c-1,o,d-1,n);} }}


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

"cogs247" Ticket System "segment tree"

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.