E - I Hate It(基础线段树)
E - I Hate It
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
Sample Input
Sample Output
5
6
5
9
Hint
Huge input,the C function scanf() will work better than cin
//线段树的应用,一般做法即可。
#include <stdio.h>
#include <string.h> struct
{
int l;
int r;
int num;
}shu[]; int ax[]; int max(int x,int y)
{
return x>y?x:y;
} void Init (int left,int right,int k)
{
shu[k].l=left;
shu[k].r=right;
if(left==right)
{
//scanf("%d",&shu[k].num);
shu[k].num=ax[left];
return;
}
int mid=(left+right)>>;
Init(left,mid,*k);
Init(mid+,right,k*+);
shu[k].num=max(shu[*k].num,shu[*k+].num);
} void insert(int num,int n,int k)
{
if (shu[k].l==shu[k].r&&shu[k].l==n)//说明到叶节点了
{
shu[k].num=num;
return;
}
int mid=(shu[k].l+shu[k].r)/;
if (n<=mid) insert(num,n,*k);
else insert(num,n,*k+); shu[k].num=max(shu[*k].num,shu[*k+].num);
} int query(int left,int right,int k)
{
if (shu[k].l==left&&shu[k].r==right)//到叶节点或正好某个区间
return shu[k].num; int mid=(shu[k].l+shu[k].r)/;
if (right<=mid) return query(left,right,*k);
if (left>mid) return query(left,right,*k+);//去右节点
if(left<=mid&&right>mid)
return max(query(left,mid,*k),query(mid+,right,*k+));
return ;
} int main()
{
int Q;
int all_p,a,b;
char comend;
int i;
while (scanf("%d%d",&all_p,&Q)!=EOF)
{
for ( i=;i<=all_p;i++)
scanf("%d",&ax[i]); Init(,all_p,);//输入并且构造树
while (Q--)
{
getchar();//特别注意,没有会超时,坑了我3小时,cao
scanf("%c%d%d",&comend,&a,&b); if (comend=='Q') printf("%d\n",query(a,b,));
if (comend=='U') insert(b,a,);
}
}
return ;
}
E - I Hate It(基础线段树)的更多相关文章
- hdu 1754(基础线段树) I Hate It
http://acm.hdu.edu.cn/showproblem.php?pid=1754 数据比较大,暴力会超时,所以明显是线段树,普通的线段树,结构体中多开一个值sum储存每个子区间的最大成绩, ...
- hdu1754 基础线段树
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- bzoj 1012 基础线段树
原题传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 今儿一天状态不好,都没怎么刷题..快下课了,刷道水题.... 裸的线段树 /*** ...
- Poj 3246 Balanced Lineup(线段树基础)
依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...
- HDU 1754 I Hate It(线段树基础应用)
基础线段树 #include<iostream> #include<cstdio> #include<cstring> using namespace std; # ...
- 线段树学习笔记(基础&进阶)(一) | P3372 【模板】线段树 1 题解
什么是线段树 线段树是一棵二叉树,每个结点存储需维护的信息,一般用于处理区间最值.区间和等问题. 线段树的用处 对编号连续的一些点进行修改或者统计操作,修改和统计的复杂度都是 O(log n). 基础 ...
- SGU 311. Ice-cream Tycoon(线段树)
311. Ice-cream Tycoon Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standar ...
- 2018.07.23 hdu5828 Rikka with Sequence(线段树)
传送门 这道题维护区间加,区间开根,区间求和. 线段树常规操作. 首先回忆两道简单得多的线段树. 第一个:区间覆盖,区间加,区间求和. 第二个:区间开根,区间求和. 这两个是名副其实的常规操作. 但这 ...
- 维护gcd的线段树 补发一波。。。
基础线段树(辣鸡的不行) 发现自己线段树除了会维护加法和乘法就啥也不会了QWQ太菜了 瞎写了一个维护gcd的 首先,gcd(x,y)= gcd(x,y-x) 并且很容易推广到n个数,所以我们可以把原数 ...
随机推荐
- C语言面向对象编程(五):单链表实现(转)
这里实现的单链表,可以存储任意数据类型,支持增.删.改.查找.插入等基本操作.(本文提供的是完整代码,可能有些长.) 下面是头文件: #ifndef SLIST_H #define SLIST_H # ...
- hadoop english
for the same 同样previously 之前地overlay v. 覆盖; 镀金variable expansion 变量替换processed for 处理 entry(entries) ...
- spring事务管理源码解析--加了@Transactional注解后Spring究竟为我们做了哪些事情?
大家都知道事务管理是基于AOP的,对AOP还不了解的请自行百度. 实现一个事务需要以下几步:1.获取数据库连接 2.执行数据库操作 3.如果2步骤发生异常就回滚,否则就提交 4.释放资源. 然后 ...
- 自己写浏览器和webserver的分析!
自己写浏览器和webserver 在android写一个浏览器 editText:输入网址ip:port/login.html.提交 把域名解析成ip 产生请求行 get login.html /r/ ...
- jQuery.Validate常用的一些规则
// 手机号码验证 jQuery.validator.addMethod("mobile", function(value, element) { var length = val ...
- eclipse日志
注意包别引入错: import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger log = ...
- Atitit。Tree文件解析器的原理流程与设计实现 java c# php js
Atitit.Tree文件解析器的原理流程与设计实现 java c# php js 1. 解析原理与流程1 1.1. 判断目录 ,表示服 dirFlagChar = "└├─&quo ...
- JsCal( JS Calendar)
http://www.dynarch.com/projects/calendar Doc: http://www.dynarch.com/jscal/ This is the documentatio ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- oracle命令登录数据库
sqlplus aa/aa@jdbc:oracle:thin:@10.200.130.111:1521:nbact 或者 sqlplus aa/aa@nbact