线段树h1754
来源:互联网 发布:淘宝兼职被骗了怎么办 编辑:程序博客网 时间:2024/06/04 08:00
应为数组开小了导致的结果居然是超时
线段树,有建立,修改,查找
//
// main.cpp
// h1754
//
// Created by Mr.Xue on 17/3/2.
// Copyright © 2017年 Mr.Xue. All rights reserved.
//
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespacestd;
int a[400050];
struct node
{
int l,r,value;
}list[1000010];
void buildTree(int i)
{
if(list[i].l==list[i].r)
{
list[i].value=a[list[i].l];
// printf("i=%d l=%d %d\n",i,list[i].l,list[i].value);
return;
}
int mid=(list[i].r+list[i].l)/2;
list[2*i].l=list[i].l;
list[2*i].r=mid;
buildTree(2*i);
list[2*i+1].l=mid+1;
list[2*i+1].r=list[i].r;
buildTree(2*i+1);
if(list[i*2].value>list[i*2+1].value)
list[i].value=list[i*2].value;
else
list[i].value=list[i*2+1].value;
}
void replace(int i,int idex,int value)
{
if(list[i].r==list[i].l&&list[i].r==idex)
{
list[i].value=value;
a[idex]=value;
return;
}
int mid=(list[i].r+list[i].l)/2;
if(mid>=idex)
replace(i*2,idex,value);
else
replace(i*2+1,idex,value);
if(list[i*2].value>list[i*2+1].value)
list[i].value=list[i*2].value;
else
list[i].value=list[i*2+1].value;
}
int find(int i,int left,int right)
{
if(list[i].l>=left&&list[i].r<=right)
{
//printf(")
returnlist[i].value;
}
int mid=(list[i].r+list[i].l)/2;
int lr=-1,ll=-1;
if(mid>=left)
ll=find(i*2,left,right);
if(mid+1<=right)
lr=find(i*2+1,left,right);
return ll>lr?ll:lr;
}
int main()
{
int n,m,x,y;
char ch;
while(scanf("%d",&n)!=EOF)
{
scanf("%d",&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
list[1].l=1;
list[1].r=n;
buildTree(1);
//printf("(%d)\n",list[1].value);
for(int i=1;i<=m;i++)
{
getchar();
scanf("%c %d %d",&ch,&x,&y);
if(ch=='U')
replace(1,x,y);
if(ch=='Q')
printf("%d\n",find(1,x,y));
}
}
return0;
}
/*
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
*/
- 线段树h1754
- HDOJ H1754 I Hate It(结构体和数组模拟线段树)
- 线段树?线段树!
- 线段树?线段树!
- 线段_线段树
- 线段_线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- RNN中输出端的sample采样
- 如何在andorid工程中嵌入开发者盈利广告
- Jquery——Day9(选项卡)
- Linux文件管理
- Java学习篇之源文件声明
- 线段树h1754
- 常用框架、库
- ssh远程连接桌面版ubuntu,连接经验
- time 命令
- python机器学习库scikit-learn:SVR的基本应用
- string库其实没什么用
- Spring(二、实际操作)
- 8个hybrid app开发工具
- USACO 2.4 The Tamworth Two<简单图问题>