I Hate It(线段树基础)
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45287 Accepted Submission(s): 17773
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
在每个测试的第一行,有两个正整数 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。
Huge input,the C function scanf() will work better than cin
/*times memy
889ms 8584k
by orc
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = ;
int n,m;
struct node{
int lt,rt;
int mx;
}tree[ * N];
int a[N];
void build(int l,int r,int v)
{
tree[v].lt = l;
tree[v].rt = r;
if(l == r) {tree[v].mx = a[l];return;}
int m = (l + r) >> ;
build(l,m, * v);
build(m + ,r, * v + );
tree[v].mx = max(tree[ * v].mx,tree[ * v + ].mx);
}
void update(int v,int k,int val)//直接暴力更新
{
if(tree[v].lt == tree[v].rt)
{
if(tree[v].lt == k)
tree[v].mx = val;
return;
}
int m = (tree[v].lt + tree[v].rt) >> ;
if(k <= m) update(v * ,k,val);//更新左子树
else update(v * + ,k,val);//更新右子树
tree[v].mx = max(tree[v << ].mx,tree[(v << ) + ].mx);
//else tree[v >> 1].mx = max(tree[v].mx,tree[v + 1].mx);
}
int query(int v,int l,int r)
{
if(l == tree[v].lt && r == tree[v].rt) return tree[v].mx;
int m = (tree[v].lt + tree[v].rt) >> ;
if(r <= m) query(v * ,l,r);
else if(l > m) query(v * + ,l ,r);
else{
return max(query(v * ,l,m),query(v * + ,m + ,r));
}
}
void solve()
{
char C;
int A ,B;
for(int i = ; i <= n; ++i)
cin >> a[i];
build(,n,);
while(m--)
{
cin >> C;
cin >> A >> B;
if(C == 'Q')
{
cout << query(,A,B) <<endl;
continue;
}
else update(,A,B);
}
}
int main()
{
ios::sync_with_stdio();
while(cin >> n >> m) solve();
return ;
}
I Hate It(线段树基础)的更多相关文章
- Poj 3246 Balanced Lineup(线段树基础)
依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...
- P1198 [JSOI2008]最大数(线段树基础)
P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: ...
- HDU 1754 I Hate It(线段树基础应用)
基础线段树 #include<iostream> #include<cstdio> #include<cstring> using namespace std; # ...
- 线段树基础模板&&扫描线
线段树的单点更新+区间求和 hdu1166敌兵布阵 Input 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N<=),表示敌人有N个工兵营地 ,接下来有N个正整数,第i个正整 ...
- POJ 2777 线段树基础题
题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...
- hdu 1698 Just a Hook(线段树基础)
成段更新的线段树,加入了延时标记............ 线段树这种东西细节上的理解因人而异,还是要自己深入理解......慢慢来 #include <iostream> #include ...
- hdu 1166 敌兵布阵(线段树基础题)
学习线段树~~~~~~~~~~~~要好好理解 此题是单点更新的线段树,考虑基本的询问,更新. #include <iostream> #include <algorithm> ...
- HDU 6155 Subsequence Count(矩阵乘法+线段树+基础DP)
题意 给定一个长度为 \(n\) 的 \(01\) 串,完成 \(m\) 种操作--操作分两种翻转 \([l,r]\) 区间中的元素.求区间 \([l,r]\) 有多少个不同的子序列. \(1 \le ...
- hdu 1754 I Hate It 线段树基础题
Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求, ...
- [JSOI2008]最大数(线段树基础)
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: L 不超过当前数列的长度.(L > ...
随机推荐
- php处理图片实现
<?php include("SimpleImage.php");//图片处理类在下面 $url="http://f3.v.veimg.cn/meadincms/1 ...
- supersr--去除tableViewHeader的粘性
这段代码能去除tableViewHeader的粘性 const static NSInteger kSectionHeaderHeight = 30; - (void)scrollViewDidScr ...
- stdafx.h的作用
// stdafx.h : include file for standard system include files,// or project specific include files th ...
- Eclipse CDT launch failed.Binary not found in Linux/Ubuntu
转自:http://blog.csdn.net/abcjennifer/article/details/7573916 Linux下出现launch failed.Binary not found的解 ...
- python基础——使用__slots__
python基础——使用__slots__ 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性.先定义class: cla ...
- Mex文件在VS2010中调试方法
http://www.cnblogs.com/lukylu/p/4042306.html matlab里面无法单步调试mex函数,故需转到VS上面调试,这里采用VS2010. 参考网上很多人写的方法但 ...
- js 删除确定
"<td><a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除么?')\"> ...
- sys.sysprocesses视图的使用小结
Sys.SysProcesses 系统表是一个很重要的系统视图,主要用来定位与解决Sql Server的阻塞和死锁包含正在 SQL Server 实例上运行的进程的相关信息.这些进程可以是客户端进程或 ...
- python中多线程与非线程的执行性能对比
此对比说明了一件事: 如果是IO型应用,多线程有优势, 如果是CPU计算型应用,多线程没必要,还有实现锁呢. #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- GENERATED_UCLASS_BODY 和 GENERATED_BODY 区别
the GENERATED_BODY() macro allows the class to build without having a constructor defined. If you ne ...