FZU-1921+线段树
简单的线段树。
记录MinVal 和 相应的ID即可
/*
线段树
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = ;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-;
#define L(x) (x<<1)
#define R(x) (x<<1|1) struct Tree{
int l,r,id,val;
}tree[ maxn<< ];
int a[ maxn ]; void build( int L,int R,int n ){
tree[ n ].l = L;
tree[ n ].r = R;
if( L==R ){
tree[ n ].id = L;
tree[ n ].val = a[ L ];
return ;
}
//tree[ n ].val = 0;
int mid = (L+R)/;
build( L,mid,L(n) );
build( mid+,R,R(n) );
if( tree[ L(n) ].val<tree[ R(n) ].val ) {
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else if( tree[ L(n) ].val==tree[ R(n) ].val ){
if( tree[L(n)].id<tree[R(n)].id ){
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
} void update1( int val,int L,int R,int n ){
if( L==R ){
tree[ n ].val += val;
return ;
}
int mid = (L+R)/;
if( tree[n].id<=mid ) update1( val,L,mid,L(n) );
else update1( val,mid+,R,R(n) );
if( tree[ L(n) ].val<tree[ R(n) ].val ) {
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else if( tree[ L(n) ].val==tree[ R(n) ].val ){
if( tree[L(n)].id<tree[R(n)].id ){
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
} void update2( int id,int val,int L,int R,int n ){
if( L==R ){
tree[ n ].val += val;
return ;
}
int mid = (L+R)/;
if( id<=mid ) update2( id,val,L,mid,L(n) );
else update2( id,val,mid+,R,R(n) );
if( tree[ L(n) ].val<tree[ R(n) ].val ) {
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else if( tree[ L(n) ].val==tree[ R(n) ].val ){
if( tree[L(n)].id<tree[R(n)].id ){
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
} void query( int L,int R,int n ){
if( L==R ) return ;
int mid = (L+R)/;
query( L,mid,L(n) );
query( mid+,R,R(n) );
if( tree[ L(n) ].val<tree[ R(n) ].val ) {
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else if( tree[ L(n) ].val==tree[ R(n) ].val ){
if( tree[L(n)].id<tree[R(n)].id ){
tree[ n ].val = tree[ L(n) ].val;
tree[ n ].id = tree[ L(n) ].id;
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
}
else {
tree[ n ].val = tree[ R(n) ].val;
tree[ n ].id = tree[ R(n) ].id;
}
} int main(){
int T;
int Case = ;
scanf("%d",&T);
while( T-- ){
int n;
scanf("%d",&n);
for( int i=;i<=n;i++ )
scanf("%d",&a[i]);
build( ,n, );
int m;
scanf("%d",&m);
int x,y;
while( m-- ){
scanf("%d%d",&x,&y);
if( x== ) update1( y,,n, );
else update2( x,y,,n, );
}
query( ,n, );
printf("Case %d: %d %d\n",Case++,tree[].id,tree[].val);
}
return ;
}
FZU-1921+线段树的更多相关文章
- FZU 2171 线段树 区间更新求和
很模板的题 在建树的时候输入 求和后更新 #include<stdio.h> #include<string.h> #include<algorithm> #inc ...
- FZU 2171(线段树的延迟标记)
题意:容易理解. 分析:时隔很久,再一次写了一道线段树的代码,之前线段树的题也做了不少,包括各种延迟标记,但是在组队分任务之后,我们队的线段树就交给了另外一个队友在搞, 然后我就一直没去碰线段树的题了 ...
- FZU 2105 (线段树)
Problem 2105 Digits Count Problem Description Given N integers A={A[0],A[1],...,A[N-1]}. Here we h ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- FZU 1921——栀子花开——————【线段树单点更新】
栀子花开 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- FZU 2105 Digits Count(按位维护线段树)
[题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...
- FZu Problem 2236 第十四个目标 (线段树 + dp)
题目链接: FZu Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...
- FZU 2105 Digits Count(线段树)
Problem 2105 Digits Count Accept: 302 Submit: 1477 Time Limit: 10000 mSec Memory Limit : 262144 KB P ...
随机推荐
- iOS - 字典(NSDictionary)
1. 字典类型的常用处理 //---------------不可变字典 //1.字典的创建 NSArray *array1 = [NSArray arrayWithObjects:@"zha ...
- OC8_NSData
// // main.m // OC8_NSData // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zhangxue ...
- 引用类型之Array类型
Array类型 ECMAScript数组与其它语言数组一样,都是数据的有序列表.但是ECMAScript数组的每一项可以保存任何类型的数据.而且,ECMAScript数组是可以动态调整的. 1.创建和 ...
- SpringMVC接收多个对象
问题背景: 要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:将Json对象序列化成Json字符串提 ...
- css笔记——inline-block以及空白字符处理
html <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- 前端性能优化工具--DOM Monster
当我们开发web应用的时候,性能是一个永远不能回避的问题.其实对于DOM的性能调试也是一个不可或缺的过程.使用DOM monster你只需要添加到你的”书签中“,在任何需要调试的页面点击这个书签,它就 ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- STAD Parameters
STAD related parameters: 1. stat/file -- define the file name of the actual STAD content – defaul ...
- 解析php file_exists无效的解决办法
php中file_exists无效的解决办法. 方法1 :据官方手册上描述若php教程的safe mode相关的设置过于苛刻,就会出现这样的情形:尽管文件真实存在也被误报,认为文件不存在. 由于服务器 ...
- VLAN和Trunk
Vlan实验题: 如图所示 解答过程 (一)相同vlan之间的设备全连通 1. 在SW1和SW2上分别创建vlan2和vlan3, 命令如下 SW1# vlan database SW1(vlan)# ...