[hiho1586]Minimum

题意:区间内乘积最小值,带修改。
解题关键:线段树裸题,考场上以为x y必须不等,还维护了次小值,尼玛嗨尼玛嗨,划水一整场,心态爆炸。

注意坐标需要+1

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<iostream>
  7 #define inf 0x3f3f3f3f
  8 using namespace std;
  9 typedef long long ll;
 10 const int maxn=131073;
 11 ll a[maxn],tree1[maxn<<2],tree2[maxn<<2];
 12 void pushup(int rt){
 13     tree1[rt]=max(tree1[rt<<1|1],tree1[rt<<1]);
 14     tree2[rt]=min(tree2[rt<<1|1],tree2[rt<<1]);
 15 }
 16 void build(ll rt,ll l,ll r){
 17     if(l==r){
 18         tree1[rt]=tree2[rt]=a[l];
 19         return;
 20     }
 21     ll mid=(l+r)>>1;
 22     build(rt<<1,l,mid);
 23     build(rt<<1|1,mid+1,r);
 24     pushup(rt);
 25 }
 26
 27 void update(ll rt,ll l,ll r,ll p,ll c){
 28     if(l>r) return;
 29     if(l==r){
 30         tree1[rt]=tree2[rt]=c;
 31         return;
 32     }
 33     ll mid=(l+r)>>1;
 34     if(p<=mid) update(rt<<1, l, mid, p, c);
 35     else update(rt<<1|1, mid+1, r, p, c);
 36     pushup(rt);
 37 }
 38
 39 ll query1(ll rt,ll l,ll r,ll tl,ll tr){
 40     if(tl>tr) return -inf;
 41     if(tl<=l&&tr>=r){
 42         return tree1[rt];
 43     }
 44     ll mid=(l+r)>>1,res=-inf;
 45     if(tl<=mid) res=max(res,query1(rt<<1,l,mid,tl,tr));
 46     if(tr>mid) res=max(query1(rt<<1|1,mid+1,r,tl,tr),res);
 47     return res;
 48 }
 49
 50 ll query2(ll rt,ll l,ll r,ll tl,ll tr){
 51     if(tl>tr) return inf;
 52     if(tl<=l&&tr>=r){
 53         return tree2[rt];
 54     }
 55     ll mid=(l+r)>>1,res=inf;
 56     if(tl<=mid) res=min(res,query2(rt<<1,l,mid,tl,tr));
 57     if(tr>mid) res=min(query2(rt<<1|1,mid+1,r,tl,tr),res);
 58     return res;
 59 }
 60
 61 int main(){
 62     ll n,m;
 63     ios::sync_with_stdio(0);
 64     cin.tie(0);
 65     cout.tie(0);
 66     ll t;
 67     cin>>t;
 68     while(t--){
 69         cin>>n;
 70         n=1<<n;
 71         memset(tree1, 0, sizeof tree1);
 72         memset(tree2,0,sizeof tree2);
 73         memset(a,0,sizeof a);
 74         for(int i=1;i<=n;i++) cin>>a[i];
 75         build(1, 1, n);
 76         cin>>m;
 77         for(int i=0;i<m;i++){
 78             int a,b,c;
 79             cin>>a>>b>>c;
 80             if(a==1){
 81                 ll ans1=query1(1,1,n,b+1,c+1);
 82                 ll ans2=query2(1,1,n,b+1,c+1);
 83                 if(ans1>=0&&ans2<0){
 84                     cout<<1ll*ans1*ans2<<"\n";
 85                     continue;
 86                 }
 87                 else if(ans1>=0&&ans2>=0){
 88                     cout<<1ll*ans2*ans2<<"\n";
 89                     continue;
 90                 }
 91                 else{
 92                     cout<<1ll*ans1*ans1<<"\n";
 93                 }
 94             }
 95             else{
 96                 update(1, 1, n, b+1, c);
 97             }
 98         }
 99     }
100
101 }
时间: 2024-10-16 01:29:55

[hiho1586]Minimum的相关文章

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树

HDU 5452 Minimum Cut

Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 315    Accepted Submission(s): 120 Problem Description Given a simple unweighted graph G (an undirected graph containing no loops nor

[LeetCode]Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分

HDU1394 Minimum Inversion Number 线段树+数学

Problem Description The inversion number of a given number sequence a1, a2, -, an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, -, an, if we move the first m >= 0 numbers to the end of the

[LeetCode] 111. Minimum Depth of Binary Tree Java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

[LeetCode] Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

Minimum Subtree

我的错误代码 理递归思路 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root