ZOJ 3279-Ants(线段树)
传送门: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
题意:
给你每个等级蚂蚁的数量 有两种操作:
(1)rank x的蚂蚁的等级是多少
(2)将等级为a的数量更改为b
题解:
还是一个简单的线段树,处理区间要仔细(调了一晚上bug-.-)
代码:
#include <stdio.h> #include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(); \ cout.tie(); using namespace std; typedef long long ll; const ll inf = 0x3f3f3f3f; ; const ll mod = 1000000007LL; << ; ll dat[N]; int num; int tree_node; void init(int n) { tree_node = ; while(tree_node < n) tree_node <<= ; _for(i, , tree_node * -) dat[i] = ; } void update(int pos, int date) { pos += tree_node - ; dat[pos] = date; ) { pos >>= ; dat[pos] = dat[pos<<] + dat[pos<<|]; } } int query(int rank, int k) { ) return k; ]) ); else ], k<<|); } void solve() { ll x,p,q; char str; _for(i, , num) { scanf("%lld",&x); update(i,x); } scanf("%lld",&x); ; i <= x; i++) { getchar(); scanf("%c",&str); if(str=='q') { scanf("%lld",&p); ]) { printf("%d\n",num); } else printf() - tree_node + ); } else { scanf("%lld%lld",&p,&q); update(p,q); } } } int main() { while(scanf("%d",&num)!=EOF) { init(num); solve(); } ; }
ZOJ 3279-Ants(线段树)的更多相关文章
- ZOJ - 1610 经典线段树染色问题
这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...
- zoj 3349 dp + 线段树优化
题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...
- 线段树区间染色 ZOJ 1610
Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
- zoj 3511 Cake Robbery(线段树)
problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...
- ZOJ 1859 Matrix Searching(二维线段树)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
随机推荐
- Leetcode 295. 数据流的中位数
1.题目要求 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个 ...
- LCD显示屏原理与应用
1.什么是LCD? (1)LCD(Liquid Crystal Display)俗称液晶.(2)液晶是一种材料,液晶这种材料具有一种特点:可以在电信号的驱动下液晶分子进行旋转,旋转时会影响透光性,因此 ...
- Nginx简介及使用Nginx实现负载均衡的原理【通俗易懂,言简意赅】【转】
Nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 直接作为http server(代替apache,对PHP需要FastCGI处理器支持): 另外一个功能就是作为反向代理服 ...
- lombok 配置使用以及优势
maven依赖 <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> ...
- mysql 高级应用
1.MySQL like 模糊查询 例如:select * from emp where name like '张%'; 2.1MySQL UNION 操作符 SELECT country FROM ...
- 【Foreign】Walk [暴力]
Walk Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 1 2 3 1 3 9 Sa ...
- 【BZOJ】1601: [Usaco2008 Oct]灌水
[算法]最小生成树 [题解] 想到网络流,但是好像不能处理流量和费用的关系. 想到最短路,但好像不能处理重复选边的问题. 每条边只需要选一次,每个点就要遍历到,可以想到最小生成树. 建超级源向每个点连 ...
- 【CF558E】 A Simple Task (权值线段树)
题目链接 用权值线段树维护每个字母在\([l,r]\)出现的次数,每次修改把每个字母在区间的出现次数记下来,然后清空这段区间,再按顺序插进去就好了. 时间复杂度\(O(n\log n*26)\) (好 ...
- JVM在遇到OOM(OutOfMemoryError)时生成Dump文件
方法一: 命令:jmap -dump:format=b,file=heap.bin file:保存路径及文件名pid:进程编号(windows通过任务管理器查看,linux通过ps aux查看) du ...
- 【Python学习笔记】异常处理try-except
Python异常处理 我们一般使用try-except语句来进行异常处理. 使用except Exception as err可以统一捕捉所有异常,而也可以分开处理单个异常. # 分开捕捉单个异常 t ...