contest2 (线段树)

contest2 (线段树),第1张

概述  C - C HDU - 3577 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 j   C - C HDU - 3577 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
13 61 61 63 41 51 22 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,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 root ){    if(tree[root].l >= l && tree[root].r <= r)    {        tree[root].lazy_tag += 1 ;//注意这里的lazy一定要+= 不能=1因为之前就有累计也要下传        tree[root].val += 1 ;        return ;    }    if(tree[root].lazy_tag)        pushdown(root);    int mID = (tree[root].l + tree[root].r) / 2 ;    if(l <= mID)        update(l,root * 2 );    if(r > mID)        update(l,root * 2 + 1);    tree[root].val = max(tree[root * 2].val,tree[root*2+1].val) ;}int  query(int l,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[root].l + tree[root].r) / 2 ;    //有return就不会向上回溯,返回的就是所需的区间值    if(r <= mID)        return query(l,root * 2);//如果查询区间在左边,往左走    else if(mID < l)    {        return query(l,root * 2 + 1);//如果查询区间在右边,往右走    }    else        return max(query(l,root * 2),query(mID + 1,root * 2+1));//如果区间两边都有交集,往值大的一边走}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)//题目有坑,左闭区间,右开区间            {                cout << i + 1 <<" ";                update(a[i],b[i] - 1,1) ;            }        }        cout << endl ;        cout << endl ;    }    return 0;}
总结

以上是内存溢出为你收集整理的contest2 (线段树)全部内容,希望文章能够帮你解决contest2 (线段树)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1020338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存