【SSL_P1502】校门外的树

校门外的树


Description

校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……
如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:
K=1,读入l,r表示在l~r之间种上的一种树
K=2,读入l,r表示询问l~r之间能见到多少种树
(l,r>0)

Input

第一行n,m表示道路总长为n,共有m个操作
接下来m行为m个操作

Output

对于每个k=2输出一个答案

Sample Input

5 4
1 1 3
2 2 5
1 2 4
2 3 5

Sample Output

1
2

Hint

范围:20%的数据保证,n,m<=100
60%的数据保证,n <=1000,m<=50000
100%的数据保证,n,m<=50000

解题思路

两个树状数组维护(我也不知道为什么,但是不这样干会出现一些奇奇怪怪的问题)

#include<iostream>
#include<cstdio>

int n,m,c[500010],a[500010];

void in(int x)
{
    
    
	for(;x<=n;x+=x&(-x))
		c[x]++;
}

void out(int x)
{
    
    
	for(;x<=n;x+=x&(-x))
		a[x]++;
}

int find(int x)
{
    
    
	int ans=0;
	for(;x;x-=x&(-x))
		ans+=c[x];
	return ans;
}

int findout(int x)
{
    
    
	int ans=0;
	for(;x;x-=x&(-x))
		ans+=a[x];
	return ans;
}

using namespace std;
int main()
{
    
    
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
    
    
		int s,l,r;
		scanf("%d%d%d",&s,&l,&r);
		if(s==1)
			in(l),out(r+1);
		if(s==2)
			cout<<find(r)-findout(l)<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/SSL_guyixin/article/details/108068834