ZOJ 3279 Ants(线段树)

Ants


Time Limit: 2 Seconds     
Memory Limit: 32768 KB



echo is a curious and clever girl, and she is addicted to the ants recently.

She knows that the ants are divided into many levels depends on ability, also, she finds the number of each level will change.

Now, she will give two kinds of operations as follow :

First, "p a b", the number of ants in level a change to
b
.

Second, "q x", it means if the ant‘s ability is rank xth in all ants, what level will it in?

Input

There are multi-cases, and you should use EOF to check whether it is in the end of the input. The first line is an integer
n, means the number of level. (1 <= n <= 100000). The second line follows
n integers, the ith integer means the number in level i. The third line is an integer
k, means the total number of operations. Then following k lines, each line will be
"p a b" or "q x", and 1 <= x <= total ants, 1 <= a <=
n, 0 <= b. What‘s more, the total number of ants won‘t exceed 2000000000 in any time.

Output

Output each query in order, one query each line.

Sample Input

3
1 2 3
3
q 2
p 1 2
q 2

Sample Output

2
1

Author: Lin, Yue

Source: ZOJ Monthly, December 2009

给出每个等级的数目,后面m个操作,q查询排名x在第几个等级,p a b,把a等级的数目改为b

ac代码

#include<stdio.h>
#include<string.h>
int node[100010<<2];
void build(int l,int r,int tr)
{
	if(l==r)
	{
		scanf("%d",&node[tr]);
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,tr<<1);
	build(mid+1,r,tr<<1|1);
	node[tr]=node[tr<<1]+node[tr<<1|1];
}
void update(int pos,int val,int l,int r,int tr)
{
	if(l==r)
	{
		node[tr]=val;
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
		update(pos,val,l,mid,tr<<1);
	else
		update(pos,val,mid+1,r,tr<<1|1);
	node[tr]=node[tr<<1]+node[tr<<1|1];
}
int query(int val,int l,int r,int tr)
{
	if(l==r)
	{
		return l;
	}
	int mid=(l+r)>>1;
	if(val<=node[tr<<1])
		query(val,l,mid,tr<<1);
	else
		query(val-node[tr<<1],mid+1,r,tr<<1|1);
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i;
		memset(node,0,sizeof(node));
		build(1,n,1);
		int m;
		scanf("%d",&m);
		while(m--)
		{
			char s[2];
			scanf("%s",s);
			if(s[0]=='q')
			{
				int x;
				scanf("%d",&x);
				int ans=query(x,1,n,1);
				printf("%d\n",ans);
			}
			else
			{
				int a,b;
				scanf("%d%d",&a,&b);
				update(a,b,1,n,1);
			}
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 09:09:58

ZOJ 3279 Ants(线段树)的相关文章

zoj 3157 Weapon 线段树求逆序对数

题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemId=3128 题意:平面上有n条直线,给出l, r,求出这些直线的交点横坐标在(l, r)范围内的个数. 思路: 首先求出每条直线与直线x = l和直线x = r的交点,如下图. 因为题目要求区间(l, r)是开区间,所以为了避免交点的横坐标刚好是l或者r的情况,可以先把l加上一个很小的值,r减去一个很小的值,如图中的灰线. 然后求出各条直线与两条灰线的交点,首先按与l的交点的

F - Count the Colors ZOJ 1610 (线段树+结点为长度为一的区间+树的遍历)

F - Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1610 Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

ZOJ 3324 Machine(线段树区间合并)

这道题网上很多代码是错误的,由于后台数据水,他们可以AC. 比如这组数据 10 3 p 0 9 r 0 5 r 6 9 输出应该是 0 1 1 所以有的人直接记录该区间是否被覆盖过的方法是错误的 正确方法应该是记录这段区间的最小高度(就是最接近初始位置的高度),和最小高度对应的最长左区间和右区间 开一个sum记录这段区间最小高度的块数,min_v 记录该区间最小高度 cover作为懒惰标记下推该区间的子区间需要被压几次 KinderRiven C Accepted 6776 170 C++ (g

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost

ZOJ 2671 -Cryptography ( 矩阵乘法 + 线段树 )

ZOJ 2671 - Cryptography ( 矩阵乘法 + 线段树 ) 题意: 给定模数r, 个数n, 询问数m 然后是n个矩阵,每次询问,输出矩阵联乘之后的结果. 分析: 矩阵乘法 + 线段树优化 这里线段树只有询问没有更新操作. PS:顺便仰慕一下watashi.... 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using names

ZOJ 3772 Calculate the Function 线段树+矩阵

Calculate the FunctionTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-09) Description You are given a list of numbers A1A2 .. AN and M queries. For the i-th query

ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你被允许选择一个连续的Q个靶子,使得这P把枪所打到的靶子的数目最多,问打到的靶子数目的期望值是多少. 这题通过简单的转化就可以转换成为另一个模型: 如果第a把枪可以打到第b个靶子,那么将其视为二位平面上的一个点(b, a), 问题转化为一个Q * P的矩形最多可以覆盖多少个点.只是有一点需要注意的就是

ZOJ 1859 Matrix Searching(二维线段树)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seconds      Memory Limit: 32768 KB Given an n*n matrix A, whose entries Ai,j are integer numbers ( 1 <= i <= n, 1 <= j <= n ). An operation FIND t

ZOJ 3633 Alice&#39;s present(线段树)

As a doll master, Alice owns a wide range of dolls, and each of them has a number tip on it's back, the tip can be treated as a positive integer. (the number can be repeated). One day, Alice hears that her best friend Marisa's birthday is coming , so