[线段树][读入优化][玄学加速]借教室 noip

借教室

首先,这是一道水题,线段树直接敲起




但是!!!!!!

如果,你只是简单地敲了一份线段树,你会发现,你会卡两个至少两个点!!!

因为读入会被卡!

于是,你可以使用getchar()来进行玄学加速……

vijos上加速近1000ms!!!!!以下是 借教室 AC代码

#include <cstdio>
const int maxind = 1000001;
int sum=-1;
int a[maxind]={0};
struct nodes{
    int flag;
    int min;
    int max;
    nodes(void){
        this->flag=0;
        this->min=0;
        this->max=0;
    }
};
nodes sumsTree[maxind * 4 + 10]; 
inline void read(int&x){  //玄学加速的开始
    char ch=getchar();
    x=0;
    while (ch>'9'||ch<'0')
        ch=getchar();
    while (ch<='9'&&ch>='0'){
        x*=10;
        x+=ch-'0';
        ch=getchar();
    }
    return ;
}
inline int min(int a,int b){
    return (a<b)?a:b;
}
inline int max(int a,int b){
    return (a>b)?a:b;
}
inline void pushdown(int node){
    const int lc=(node<<1),rc=(node<<1)|1;
    int flag=sumsTree[node].flag;
    sumsTree[lc].flag+=flag;
    sumsTree[rc].flag+=flag;
    sumsTree[lc].max+=flag;
    sumsTree[lc].min+=flag; 
    sumsTree[rc].max+=flag;
    sumsTree[rc].min+=flag;
    return ;
} 
void build(int node, int begin, int end)    
{   
    if (begin == end){
        sumsTree[node].max = a[begin];
        sumsTree[node].min = a[begin];
    }
    else  
    {     
        int t=(begin+end)>>1;
        build((node<<1), begin, t);  
        build(node*2|1, t+1, end);
        sumsTree[node].min=min(sumsTree[(node<<1)].min,sumsTree[(node<<1)|1].min);
        sumsTree[node].max=max(sumsTree[(node<<1)].max,sumsTree[(node<<1)|1].max);
    }
}  
void add(int node,int begin,int end,const int left,const int right,const int num){
    if (begin>right||end<left)
        return ;
    int t=(begin+end)>>1;
    const int lc=(node<<1),rc=(node<<1)|1;
    nodes& cur=sumsTree[node];
    if (begin>=left&&end<=right)
    {
        cur.flag+=num;
        cur.max+=num;
        cur.min+=num;
        return ;
    }
    pushdown(node);
    cur.flag=0;
    add(lc,begin,t,left,right,num);
    add(rc,t+1,end,left,right,num);
    cur.max=max(sumsTree[lc].max,sumsTree[rc].max);
    cur.min=min(sumsTree[lc].min,sumsTree[rc].min);
    return ;
}
int query(int node,int begin,int end,const int left,const int right,const int num){
    if (begin>right||end<left)
        return 0;
    int t=(begin+end)>>1;
    const int lc=(node<<1),rc=(node<<1)|1;
    nodes &cur=sumsTree[node]; 
    if (begin>=left&&end<=right)
    {
        if(cur.max<num) return 0;
        if (cur.min>=num) {return end-begin+1;}
        if (begin!=end){
            pushdown(node);
            return query(lc,begin,t,left,right,num)+query(rc,t+1,end,left,right,num);
        }

    }
    pushdown(node);
    cur.flag=0;
    return query(lc,begin,t,left,right,num)+query(rc,t+1,end,left,right,num);
}
int main(void)  
{
    int n,m;
    read(n);read(m);
//  scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
        read(a[i]);
    build(1,1,n);
    for (int i=1;i<=m;i++)
    {
        char ch=getchar();
        int from,to,ds;
        while (ch!='Q'&&ch!='A')
            ch=getchar();
        read(from);read(to);read(ds);
        if (ch=='Q'){
            add(1,1,n,from,to,ds);
        }
//      else {
            printf("%d\n",query(1,1,n,from,to,ds));
//      }
    }
    return 0;  
}   

因为很重要所以贴两遍

inline void read(int&x){  //玄学加速的开始
    char ch=getchar();
    x=0;
    while (ch>'9'||ch<'0')
        ch=getchar();
    while (ch<='9'&&ch>='0'){
        x*=10;
        x+=ch-'0';
        ch=getchar();
    }
    return ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值