Vijos P1448 校门外的树

P448 校门外的树

题目链接-P448 校门外的树
在这里插入图片描述
在这里插入图片描述
解题思路
树 状 数 组 + 括 号 序 列 树状数组+括号序列 +

  • 开两个树状数组分别维护左括号和右括号即可
  • 我们可以把所有线段的端点看成括号序列,即 把插入的线段 [ l , r ] [l,r] [l,r]看成在坐标 l l l处的( r r r处的)
  • 我们可以发现,最后的答案等于查询区间 [ L , R ] [L,R] [L,R]之间(的个数减去)的个数
  • 不懂树状数组原理的可以先去学一下
  • 具体操作见代码

附上代码

#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cmath>
#include<string>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=5e4+5;
typedef long long ll;
typedef pair<int,int> PII;
int L[N],R[N],n;
int lowbit(int x){
	return x&-x;
}
void add(int x,int d,int sum[]){//修改
	while(x<=n){
		sum[x]+=d;
		x+=lowbit(x);
	}
}
int query(int x,int sum[]){//查询
	int res=0;
	while(x>0){
		res+=sum[x];
		x-=lowbit(x);
	}
	return res;
}
signed main(){
	
	int m;
	scanf("%d%d",&n,&m);
	while(m--){
		int op,l,r;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1){
			add(l,1,L);
			add(r,1,R);
		}
		else
			printf("%d\n",query(r,L)-query(l-1,R));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值