COGS 1266 Borrow Classroom _ segment Tree

Source: Internet
Author: User
Tags stdin
1266. [NOIP2012] Borrow the classroom ★★☆ input file: classrooms.in output file: classrooms.out simple comparison
Time limit: 1 s memory limit: 128 MB "topic description"

During college, it is often necessary to rent a classroom. Big to the Department to organize activities, small to study group self-study discussion, all need to apply to the school to borrow the classroom. The size of the classroom function is different, borrow the classroom person's identity is different, borrow the formalities of the classroom is not the same.

We naturally hope to solve this problem by programming in the face of the massive rental classroom information.

We need to deal with the next n days of borrowing classroom information, which the first day of the school has RI classrooms available for rent. A total of M orders, each order with three positive integer description, respectively, DJ,SJ,TJ, said that a tenant needs from the SJ days to TJ days rented classrooms (including the first SJ days and the first TJ days), daily need to rent a DJ classroom.

We assume that the tenant has no requirement for the size and location of the classroom. For each order, we only need to provide a daily DJ classrooms, and they are specific classrooms, whether daily is the same classroom is not considered.

The principle of borrowing the classroom is first come first served, that is to say, we have to order each order in order to allocate the classroom. If you encounter an order that cannot be fully met during the assignment, you will need to stop the classroom assignment and notify the current applicant to modify the order. The inability to meet here means that there are at least one day remaining in the classroom, from SJ Day to TJ Day, with less than a DJ.

Now we need to know if there will be an order that is not fully satisfied. If so, which applicant should be notified to change the order. "Input Format"

The first line contains two positive integer n,m, representing the number of days and orders.

The second row contains n positive integers, of which the number of I is RI, which indicates the number of classrooms available for the day I can rent.

The next line is M, each containing three positive integer DJ,SJ,TJ, indicating the number of leases, the start of the loan, and the end of the first day.

A space is used to separate the two digits adjacent to each row. The number of days and orders are numbered with integers starting with 1. "Output Format"

If all orders are satisfied, the output is only one row, containing an integer 0. Otherwise (the order cannot be fully satisfied) outputs two lines, the first line outputs a negative integer-1, and the second line outputs the applicant number for which the order is to be modified. "Sample Input"

4 3 2 5 4 3 2 1 3 3 2- 
4 
"Sample Output"
-1
2
"Input and Output sample description"

After the 1th order was satisfied, the remaining classroom numbers for 4 days were 0,3,2,3. The 2nd order requires 3 classrooms per day on the 2nd day to the 4th day, while the number of classrooms remaining in 3rd days is 2 and therefore cannot be met. Assign stop, notify 2nd applicant to modify order. "Data Range"

For 10% of the data, there are 1≤n,m≤10;

For 30% of the data, there are 1≤n,m≤1000;

For 70% of the data, there are 1≤n,m≤10^5;

For 100% of the data, there are 1≤n,m≤10^6,0≤ri,dj≤10^9,1≤sj≤tj≤n.






First, you can simulate the exam can be 45 points .... I was betel pepper juice vegetableschinese on the simulation.


Now we can see that this is a very bare segment tree (naked but not a positive solution)

Maintain minimum values

Query Minv[1]

Interval update

You can get 85/90/95 points in that year.

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int lim=1000011;
int minv[lim<<2],addv[lim<<2];
int m,n,x,y,w;

int a,b,c;

void pushup (int o) {minv[o]=min (minv[o<<1],minv[o<<1|1]);}
        void build (int o,int l,int R) {if (l==r) {scanf ("%d", &minv[o]);
    Return
    int m= (L+R) >>1;
    Build (O&LT;&LT;1,L,M);
    Build (O<<1|1,m+1,r);
Pushup (o);
    } void pushdown (int o) {addv[o<<1]+=addv[o];
    addv[o<<1|1]+=addv[o];
    minv[o<<1]+=addv[o];
    minv[o<<1|1]+=addv[o];
addv[o]=0;
        } void Add (int o,int l,int R) {if (l>=x&&r<=y) {addv[o]+=w;
        Minv[o]+=w;
    Return
    } pushdown (o);
    int m= (L+R) >>1;
    if (x<=m) Add (o<<1,l,m);
    if (y>m) Add (o<<1|1,m+1,r);
Pushup (o);
    int main () {freopen ("classrooms.in", "R", stdin); Freopen ("Classrooms.out", "w", stdout); ScanF ("%d%d", &m,&n);
    Build (1,1,M);
        for (a=1;a<=n;a++) {scanf ("%d%d%d", &w,&x,&y);
        W=-w;
        Add (1,1,m);
            if (minv[1]<0) {cout<<-1<< ' \ n ' <<a<< ';
        return 0;
    }} cout<<0<<endl;
return 0;
 }




But the positive solution is two points ... Two points, two points ...

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
int s[1000011]; struct Self{int x,y,w;}
G[1000011];
int m,n,a,b,c,d;
int L,r,mid,now;

int ans;
    void work (int l,int r) {int A;
        if (R>now) for (a=now+1;a<=r;a++) {S[G[A].X]-=G[A].W;
    S[G[A].Y+1]+=G[A].W;
        else for (a=r+1;a<=now;a++) {S[G[A].X]+=G[A].W;
    S[G[A].Y+1]-=G[A].W;
} now=r;
    BOOL OK () {int ret=0,a;
        for (a=1;a<=m;a++) {ret+=s[a];
    if (ret<0) return false;
return true;
    int main () {freopen ("classrooms.in", "R", stdin);
    Freopen ("Classrooms.out", "w", stdout);
    scanf ("%d%d", &m,&n);
    b=c=0;
        for (a=1;a<=m;a++) {scanf ("%d", &c);
        S[a]=c-b;
    B=c;
    
    for (a=1;a<=n;a++) scanf ("%d%d%d", &g[a].w,&g[a].x,&g[a].y);
    L=1;r=m;ans=-1;
     while (l<=r) {mid= (l+r) >>1;   Work (1,MID);
        
        if (ok ()) l=mid+1;
            else {r=mid-1;
        Ans=mid;
    } if (Ans==-1) cout<<0<<endl;
else Cout<<-1<<endl<<ans<<endl;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.