p1993 [树状数组]种树

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/84674261

题目

描述 Description
Jzyz的机房的新规定明确说明,机房2016级最强的人要接受惩罚,于是杨树辰就被罚去植树了。我们把jzyz对面的一条路看成一条长度为n的线,晗神给杨树辰指定了一些区间让他种树,美其名曰让他减肥,但是晗神在中间会来检查杨树辰的工作,询问杨树辰一个点上有几棵树,但是杨树辰不仅程序写的好,而且眼神也好,于是他想请你帮忙设计一个程序来帮他完成晗神的检查。
输入格式 Input Format
第一行:n和m。
第2到m+1行:每行开头一个z
Z=1,后跟两个整数L和R表示种树的区间[L,R],0<=L<=R<=n.
Z=2,后跟一个整数x,表示询问的地点的树的个数,0<=x<=n.
输出格式 Output Format
对于每个z==2的答案。
样例输入 Sample Input

15 7
1 2 3
2 2
1 4 12
1 3 10
2 4
1 1 15
2 7

样例输出 Sample Output

1
2
3

时间限制 Time Limitation
1s
注释 Hint
时间限制:
1s
注释:
1<=n,m<=1000000;
来源 Source
ppt例题
题面数据来自 2019届 杨树辰

题解

板子题。

代码

#include<bits/stdc++.h>
using namespace std;
char buf[1<<15],*fs,*ft;
inline char getc() { return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++; }
inline int read()
{
	int x=0,f=1;
	char ch=getc();
	while(!isdigit(ch)) { if(ch=='-')  f=-1; ch=getc(); }
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getc(); }
	return x*f;
}
void put(int x)
{
	if(x==0)
	{
		putchar('0'),putchar('\n');
		return;
	}
	if(x<0) putchar('-'),x=-x;
	int num=0;char ch[16];
	while(x) ch[++num]=x%10+'0',x/=10;
	while(num) putchar(ch[num--]);
	putchar('\n');
}
const int __=1000010;
int n,m;
int tree[__];
int lowbit(int x)
{
	return x & -x;
}
void add(int x,int k)
{
	if(x==0) { tree[0]+=k; return; }
	while (x<=n)
	{
		tree[x]+=k;
		x+=lowbit(x);
	}
}
int search(int x)
{
	int ans=0;
	if(x==0) return tree[0];
	while (x)
	{
		ans+=tree[x];
		x-=lowbit(x);
	}
	return ans+tree[0];
}
int main()
{
	memset(tree,0,sizeof(tree));
	n=read(),m=read();
	for (int i=1; i<=m; ++i)
	{
		int z=read();
		if (z==1)
		{
			int x=read(),y=read();
			add(x,1);
			add(y+1,-1);
		}
		else
		{
				int x=read();
				put(search(x));
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/84674261