ZOJ 3279 Ants(线段树)
来源:互联网 发布:装企叁度erp软件 编辑:程序博客网 时间:2024/04/28 13:41
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 integern, means the number of level. (1 <= n <= 100000). The second line followsn integers, the ith integer means the number in level i. The third line is an integerk, 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
31 2 33q 2p 1 2q 2
Sample Output
21
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);elseupdate(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);elsequery(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);}}}}
- ZOJ 3279 Ants(线段树)
- ZOJ 3279 Ants(线段树)
- zoj 3279 ants(线段树区间位置查询)
- ZOJ 3279 Ants(线段树单点更新和查询)
- zoj 3279 Ants (线段树单点更新)
- zoj 3279 ants 线段树+单点更新+查询下标
- zoj 3279 Ants
- ZOJ 3279 Ants
- zoj 3279 ants
- ZOJ - 3279 Ants
- zoj3279 ants 线段树
- 线段树单点更新 ants
- zoj--Ants
- ZOJ 3279 Ants 二分树状数组
- zoj 2376 Ants (水题,刚开始不会)
- ZOJ_3279 Ants 线段树+元素位置
- zoj 2376 Ants
- zoj 2376 Ants
- HDU 1996 汉诺塔VI
- js
- APP页面的三种基本实现方式:storyboard,xib,代码实现 与自动布局
- mysql的日期查询操作,查询最近一天,一周,一个月
- jsp实现简单的登录与注销功能
- ZOJ 3279 Ants(线段树)
- 353 3D dungeon【bfs】
- uva10791 Minimum Sum LCM(唯一分解定理)
- linux 装java的两种方法
- 向VS2015添加WTL项目向导(ATL/WTL Application Wizard)
- zoj 3204 Connect them(kruskal算法) 解题报告
- 重复子串问题(四):求两个字符串的最长公共字串
- LeetCode #4 Median of Two Sorted Arrays
- N年前Nginx负载均衡在新浪播客中的应用