A - 卿学姐与公主(线段树+单点更新+区间极值)
A - 卿学姐与公主
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏
在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中。
英勇的卿学姐拔出利刃冲向了拯救公主的道路。
走过了荒野,翻越了高山,跨过了大洋,卿学姐来到了魔王的第一道城关。
在这个城关面前的是魔王的精锐部队,这些士兵成一字排开。
卿学姐的武器每次只能攻击一个士兵,并造成一定伤害,卿学姐想知道某时刻从LL到RR这个区间内,从开始到现在累计受伤最严重的士兵受到的伤害。
最开始每个士兵的受到的伤害都是0
Input
第一行两个整数N,QN,Q表示总共有NN个士兵编号从11到NN,和QQ个操作。
接下来QQ行,每行三个整数,首先输入一个tt,如果tt是11,那么输入p,xp,x,表示卿学姐攻击了pp这个位置的士兵,并造成了xx的伤害。如果tt是22,那么输入L,RL,R,表示卿学姐想知道现在[L,R][L,R]闭区间内,受伤最严重的士兵受到的伤害。
1≤N≤1000001≤N≤100000
1≤Q≤1000001≤Q≤100000
1≤p≤N1≤p≤N
1≤x≤1000001≤x≤100000
1≤L≤R≤N1≤L≤R≤N
Output
对于每个询问,回答相应的值
Sample input and output
| Sample Input | Sample Output |
|---|---|
5 4 |
0 |
Hint
注意可能会爆int哦
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
typedef long long LL;
LL T[N << ];
int M;
void Build(int n){
for(M = ; M <= n + ; M *= );
} void Updata(int n, int V){
for(T[n+=M] += V, n /= ; n; n /= )
T[n] = max(T[n << ], T[n << |]);
} LL Query(int s, int t){
LL ans = ;
for(s=s+M-, t=t+M+; s^t^; s/=, t/=){
if(~s&) ans = max(ans, T[s^]);
if(t&) ans = max(ans, T[t^]);
}
return ans;
} int main(){
int n, q;
scanf("%d %d", &n, &q);
Build( n );
int t, p, l, r, x;
while(q --){
scanf("%d", &t);
if(t == ){
scanf("%d %d", &p, &x);
Updata(p, x);
}else{
scanf("%d %d", &l, &r);
printf("%lld\n", Query(l, r));
}
}
return ;
}
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1 const int N = + ;
typedef long long LL;
LL T[N << ]; void PushUP(int rt){
T[rt] = max(T[rt << ], T[rt << |]);
} void Updata(int p, int c, int l, int r, int rt){
if(l == r){
T[rt] += c;
return ;
}
int m = (l + r) >> ;
if(p <= m) Updata(p, c, lson);
else Updata(p, c, rson);
PushUP( rt );
} LL Query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R){
return T[rt];
}
int m = (l + r) >> ;
LL ret = ;
if(L <= m) ret = max(ret, Query(L, R, lson));
if(R > m) ret = max(ret, Query(L, R, rson));
return ret;
}
int main(){
int n, q;
scanf("%d %d", &n, &q);
int t, p, l, r, x;
while(q --){
scanf("%d", &t);
if(t == ){
scanf("%d %d", &p, &x);
Updata(p, x, , n, );
}else{
scanf("%d %d", &l, &r);
printf("%lld\n", Query(l, r, , n, ));
}
}
return ;
}
A - 卿学姐与公主(线段树+单点更新+区间极值)的更多相关文章
- cdoj 1324 卿学姐与公主 线段树裸题
卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- B - 卿学姐与基本法 (离散化+成段更新+区间求和)
卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
随机推荐
- springmvc/springboot开发restful API
非rest的url写法: 查询 GET /user/query?name=tom 详情 GET /user/getinfo? 创建 POST /user/create?name=tom 修改 POST ...
- MongoDB接口类函数
[接口类定义] [java] view plaincopy /** * 项目名:SpiderCrawler * 文件名:MongoDBDao.java * 描述:TODO(用一句话描述该文件做什么) ...
- hdu 1166 线段树 区间求和 +单点更新 CD模板
题目链接 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Python黑科技 | Python中四种运行其他程序的方式
在Python中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在脚本中直接使用其他脚本或程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32proc ...
- Spring Boot教程(五)调度任务
构建工程 创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @EnableScheduling pu ...
- Oracle删除表空间报ORA01548
由于undo表空间设置了自动增长,导致替换了好几个undo表空间,就想把原先的undo表空间删掉腾出空间 但删的时候报错 SQL> drop tablespace undotbs1 includ ...
- css实现不定宽高的div水平、垂直居中
一共有三个方案: 1,第一种方案主要使用了css3中transform进行元素偏移,效果非常好 这方法功能很强大,也比较灵活,不仅仅局限在实现居中显示. 兼容方面也一样拿IE来做比较,第二种方法IE ...
- android开机引导界面的几种实现
不少应用在设计的时候都会有几个引导界面,这里总结一下几个典型实现: 之前自己做过仅具有一个引导界面的应用,在welcomeActivity中设置一张图片,更复杂的为该图片设置一个渐入渐出的动画,然后利 ...
- Getting CFNetwork SSLHandshake failed (-9806) error
平常个人打测试包一切OK,今天突然不能联网了 How to handle "CFNetwork SSLHandshake failed" in iOS 参考1 Getting CF ...
- Make jQuery throw error when it doesn't match an element
Make jQuery throw error when it doesn't match an element 解答1 You could make a plugin to use to ensur ...