contest2 (segment tree)

 
Chinese always have the railway tickets problem because of its' huge amount of passangers and stations. Now goverment need you to develop a new tickets query system.
One train can just take k passangers. And each passanger can just buy one ticket from station a to station b. Each train cannot take more passangers any time. The one who buy the ticket earlier which can be sold will always get the ticket.

InputThe input contains servel test cases. The first line is the case number. In each test case:
The first line contains just one number k( 1 ≤ k ≤ 1000 ) and Q( 1 ≤ Q ≤ 100000 )
The following lines, each line contains two integers a and b, ( 1 ≤ a < b ≤ 1000000 ), indicate a query.
Huge Input, scanf recommanded.OutputFor each test case, output three lines:
Output the case number in the first line.
If the ith query can be satisfied, output i. i starting from 1. output an blank-space after each number.
Output a blank line after each test case.Sample Input
1
3 6
1 6
1 6
3 4
1 5
1 2
2 4
Sample Output
Case 1:
1 2 3 5 

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#define rep(i , n) for(int i = 0 ; i < (n) ; i++)
using namespace std;
const int N = 1100009 ;
int ans = 0 , flag = 1;
int a[1000009] , b[1000009];

struct Node{
    int l , r , val , lazy_tag ;
}tree[N * 4];

void build(int l , int r , int root)
{
    tree[root].l = l , tree[root].r= r ;
    tree[root].val = 0 , tree[root].lazy_tag = 0;
    if(l == r)
        return ;
    int mid = (l + r) / 2 ;
    build(l , mid , root * 2);
    build(mid + 1 , r , root * 2+1);
}

void pushdown(int root)
{
    tree[root * 2].lazy_tag += tree[root].lazy_tag ;
    tree[root * 2].val += tree[root].lazy_tag ;
    tree[root * 2 + 1].lazy_tag += tree[root].lazy_tag ;
    tree[root * 2 + 1].val += tree[root].lazy_tag;
    tree[root].lazy_tag = 0;
}
void update(int l , int R & lt, int the root) 
{ 
    IF (Tree [the root] .L> = L && Tree [the root] .r <= R & lt) 
    { 
        Tree [the root] .lazy_tag + = . 1 ; // note here must be lazy + = = 1 because there can not be accumulated before the transmission should 
        Tree [the root] .val + = 1 ;
         return ; 
    } 
    IF (Tree [the root] .lazy_tag) 
        pushdown (the root); 
    int MID = (Tree [the root] .L + Tree [the root] .r) / 2 ;
     IF (L <= MID) 
        Update (L, R & lt, the root * 2 );
     IF (R & lt> MID)
        update(l , r , root * 2 + 1);
    tree[root].val = max(tree[root * 2].val , tree[root*2+1].val) ;
}

int  query(int l , int r , int root )
{
    if(tree[root].l >= l && tree[root].r <= r)
    {
        cout << tree[root].val <<endl ;
        return tree[root].val ;
    }
    if(tree[root].lazy_tag)
        pushdown(root);
    int= MID (Tree [the root] .L + Tree [the root] .r) / 2 ;
     // there will not return back upward, is required to return the value of the interval 
    IF (R & lt <= MID)
         return Query (L, R & lt, the root * 2 ); // If the query interval in the left, left to go 
    the else  IF (MID < L) 
    { 
        return query (L, R & lt, the root * 2 + . 1 ); // If the query interval on the right, right take 
    }
     the else 
        return max (Query (L, MID, the root * 2 ), Query (MID + . 1 , R & lt, the root * 2 + . 1 )); // if intersection interval on both sides, to a large value while walking
}



int main()
{
    int n ;
    cin >> n ;
    int cnt = 0 ;
    while(n--)
    {
        cnt++ ;
        int p , q , maxx = 0;
        scanf("%d%d" , &p , &q);
        for(int i = 0 ; i < q ; i ++)
        {
            scanf("%d%d" , &a[i] , &b[i]);
            if(B [I]> Maxx) 
                Maxx = B [I]; 
        } 
        Build ( . 1 , Maxx, . 1 ); 
        COUT << " Case " << << CNT " : " << endl; 
        REP (I, Q) 
        { 
            IF (Query (A [I], B [I] - . 1 , . 1 ) <P) // Title pit, closed interval left and right open interval 
            { 
                COUT << I + . 1 << "  " ; 
                Update (A [ i],b[i] - 1 , 1) ;
            }
        }
        cout << endl ;
        cout << endl ;
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11259359.html