• Fast Arrangement (线段树,延迟标志)


    个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦。

    按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都会更新,但是

    递归思想到了这里还是会回去,所以在程序末尾进行往上更新就好了,同时,在查询的时候有延迟标志时要下放,

    但是注意此时不会影响父节点的值,因为在更新延迟标志的时候就已经回溯把父节点更新了。

    题目:

    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
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<iomanip>
      6 #include<algorithm>
      7 using namespace std;
      8 #define inf 1<<29
      9 #define nu 4000005
     10 #define maxnum 1000000
     11 int n,k;
     12 int maxelem;
     13 int book[100005];
     14 int flag=0;
     15 typedef struct
     16 {
     17     int left,right;
     18     int sum;
     19     int jf;
     20     int mid(){
     21         return (left+right)/2;
     22     }
     23 
     24 }Tree;
     25 Tree tree[nu];
     26 void buildtree(int root,int l,int r){
     27       tree[root].left=l,tree[root].right=r;
     28       tree[root].sum=tree[root].jf=0;
     29       if(l!=r){
     30         buildtree(root*2+1,l,(l+r)/2);
     31         buildtree(root*2+2,(l+r)/2+1,r);
     32       }
     33 }
     34 void updown(int root)
     35 {
     36 
     37         tree[root*2+1].jf+=tree[root].jf;
     38         tree[root*2+2].jf+=tree[root].jf;
     39          tree[root*2+1].sum+=tree[root].jf;
     40         tree[root*2+2].sum+=tree[root].jf;
     41     tree[root].jf=0;
     42 }
     43 void upset(int root)
     44 {
     45     tree[root].sum=max(tree[root*2+1].sum,tree[root*2+2].sum);
     46 }
     47 int checktree(int root,int l,int r)
     48 {
     49     if(tree[root].left==l&&tree[root].right==r)
     50     {
     51         return tree[root].sum;
     52     }
     53     int mid=tree[root].mid();
     54     if(tree[root].jf) updown(root);
     55     if(r<=mid)
     56         return checktree(root*2+1,l,r);
     57     else if(l>mid)
     58         return checktree(root*2+2,l,r);
     59     else
     60     {
     61         return max(checktree(root*2+1,l,mid),checktree(root*2+2,mid+1,r));
     62     }
     63 }
     64 void inserttree(int root,int l,int r){
     65     if(tree[root].left==l&&tree[root].right==r)
     66     {
     67         tree[root].jf+=1;
     68         tree[root].sum+=1;
     69         return ;
     70     }
     71     int mid=tree[root].mid();
     72     if(tree[root].jf) updown(root);
     73     if(r<=mid)
     74         inserttree(root*2+1,l,r);
     75     else if(l>mid)
     76         inserttree(root*2+2,l,r);
     77         else
     78         {
     79             inserttree(root*2+1,l,mid);
     80             inserttree(root*2+2,mid+1,r);
     81         }
     82         upset(root);
     83 }
     84 int main()
     85 {
     86     int t,j;
     87     scanf("%d",&t);
     88     for(j=1;j<=t;j++){
     89     scanf("%d%d",&k,&n);
     90     buildtree(0,1,maxnum);
     91     flag=0;
     92     memset(book,0,sizeof(book));
     93     for(int i=1;i<=n;i++){
     94         int a,b;
     95         scanf("%d%d",&a,&b);
     96         b--;
     97         if(checktree(0,a,b)<k){
     98             book[flag++]=i;
     99             inserttree(0,a,b);
    100         }
    101     }
    102     printf("Case %d:
    ",j);
    103     for(int p=0;p<flag;p++)
    104         {
    105             printf("%d ",book[p]);
    106         }
    107         printf("
    
    ");
    108     }
    109     return 0;
    110 }


  • 相关阅读:
    redis五大基本类型---String
    记录LVS搭建,DR模式,永久生效
    zabbix5.0安装,配置邮件报警,钉钉报警
    在zabbix-server的web界面添加agent主机
    部署 zabbix的客户端 zabbix-agent
    Linux 系统查看服务器SN序列号以及服务器型号
    记录在Linux系统使用vsftpd服务
    软件工程第三次实验
    软件工程第二次实验
    软件工程第一次实验
  • 原文地址:https://www.cnblogs.com/blvt/p/7895308.html
Copyright © 2020-2023  润新知