线段树(区间修改/查询)

#include<iostream>
#include<iomanip>
#include<cstring>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#include<string>
#include<memory>
using namespace std;
 

const int k=100006;

struct Node{

        int p,q;

}tree[4*k];

int n,t,a,b;

void xg(int l,int r,int root){

    if(b<l || r<a)    return;

    if(l>=a && r<=b){

        tree[root].maxx++;
        tree[root].delta++;

        return;

      }

      int mid=(l+r)/2,q=tree[root].q;
      tree[root*2].p+=q;

      tree[root*2].q+=q;

      tree[root*2+1].p+=q;

      tree[root*2+1].q+=q;

      tree[root].q=0;

      xg(l,mid,root*2);
      xg(mid+1,r,root*2+1);
      tree[root].p=max( tree[root*2].p, tree[root*2+1].p);
      return;

}

int ss(int l,int r,int root){

         if(l>b || r<a)    return(-99999999);

         if(l>=a && r<=b)    return(tree[root].p);

 

         int mid=(l+r)/2,delta=tree[root].q;

        tree[root*2].p+=q;
tree[root*2].q+=q;
        tree[root*2+1].p+=q;

tree[root*2+1].q+=q;

        tree[root].q=0;

        return(max( ss(l,mid,root*2), ss(mid+1,r,root*2+1)));

}

int main(){

     freopen("3.in","r",stdin);

     freopen("3.out","w",stdout);

 

     memset(tree,0,sizeof(tree));

     scanf("%d",&n);

     for(int i=1;i<=n;i++){

           scanf("%d%d%d",&t,&a,&b);

          if(t==1)    xg(1,n,1);

          if(t==2)  printf("%d\n",ss(1,n,1));

}

 

     return 0;

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值