hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】

https://hihocoder.com/problemset/problem/1586

线段树操作,原来题并不难。。。。。

要求乘积最小只要求区间内最大值、最小值和绝对值小的数,再判断min,max正负就行了。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 #define lson l,m,rt<<1
  7 #define rson m+1,r,rt<<1|1
  8 using namespace std;
  9 typedef long long ll;
 10 const int maxn=(1<<17)+2000;
 11 int Max[maxn<<2],Min[maxn<<2],Mid[maxn<<2];
 12
 13 int mid(int a,int b)
 14 {
 15     if(abs(a)<abs(b)) return a;
 16     else return b;
 17 }
 18
 19 void PushUpMax(int rt)
 20 {
 21     Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
 22 }
 23
 24 void PushUpMin(int rt)
 25 {
 26     Min[rt]=min(Min[rt<<1],Min[rt<<1|1]);
 27 }
 28
 29 void PushUpMid(int rt)
 30 {
 31     Mid[rt]=mid(Mid[rt<<1],Mid[rt<<1|1]);
 32 }
 33
 34
 35 void Build(int l,int r,int rt)
 36 {
 37     if(l==r){
 38         scanf("%d",&Max[rt]);
 39         Min[rt]=Mid[rt]=Max[rt];
 40         return;
 41     }
 42     int m=(l+r)>>1;
 43     Build(lson);
 44     Build(rson);
 45     PushUpMax(rt);
 46     PushUpMin(rt);
 47     PushUpMid(rt);
 48 }
 49
 50 void Update(int pos,int val,int l,int r,int rt)
 51 {
 52     if(l==r){
 53         Mid[rt]=Min[rt]=Max[rt]=val;
 54         return;
 55     }
 56     int m=(l+r)>>1;
 57     if(pos<=m) Update(pos,val,lson);
 58     else Update(pos,val,rson);
 59     PushUpMax(rt);
 60     PushUpMin(rt);
 61     PushUpMid(rt);
 62 }
 63
 64 int QueryMax(int L,int R,int l,int r,int rt)
 65 {
 66     if(L<=l&&r<=R){
 67         return Max[rt];
 68     }
 69     int m=(l+r)>>1;
 70     int res=-maxn-5000;
 71     if(L<=m) res=max(res,QueryMax(L,R,lson));
 72     if(R>m) res=max(res,QueryMax(L,R,rson));
 73     return res;
 74 }
 75
 76 int QueryMin(int L,int R,int l,int r,int rt)
 77 {
 78     if(L<=l&&r<=R){
 79         return Min[rt];
 80     }
 81     int m=(l+r)>>1;
 82     int res=maxn+5000;
 83     if(L<=m) res=min(res,QueryMin(L,R,lson));
 84     if(R>m) res=min(res,QueryMin(L,R,rson));
 85     return res;
 86 }
 87
 88 int QueryMid(int L,int R,int l,int r,int rt)
 89 {
 90     if(L<=l&&r<=R){
 91         return Mid[rt];
 92     }
 93     int m=(l+r)>>1;
 94     int res=maxn;
 95     if(L<=m) res=mid(res,QueryMid(L,R,lson));
 96     if(R>m) res=mid(res,QueryMid(L,R,rson));
 97     return res;
 98 }
 99
100 int main()
101 {
102     int T,n,m,k,a,b;
103     scanf("%d",&T);
104     while(T--)
105     {
106         scanf("%d",&n);
107         n=1<<n;
108         Build(1,n,1);
109         scanf("%d",&m);
110         while(m--)
111         {
112             scanf("%d%d%d",&k,&a,&b);
113             a++;
114             if(k==1){
115                 b++;
116                 ll t1=(ll)QueryMax(a,b,1,n,1);
117                 ll t2=(ll)QueryMin(a,b,1,n,1);
118                 ll t3=(ll)QueryMid(a,b,1,n,1);
119                 if((t1>=0)&&(t2<=0))
120                     printf("%lld\n",t1*t2);
121                 else if(t1<0||t2>0)
122                     printf("%lld\n",t3*t3);
123             }
124             else Update(a,b,1,n,1);
125         }
126     }
127     return 0;
128 }
时间: 2024-10-25 21:31:44

hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】的相关文章

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题目9 : Minimum

时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. Output Minx,y∈[l,r] {ax?ay}. 2. Let ax=y. 输入 The first line is an integer T, indicating the number of test cases. (

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 i题 Minimum(线段树)

描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. Output Minx,y∈[l,r] {ax?ay}. 2. Let ax=y. 输入 The first line is an integer T, indicating the number of test cases. (1≤T≤10). For each test case: The fi

hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

#1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, -, n-1. He plans to spend m consecutive days(2 ≤ m ≤ n)in Beijing. Durin

hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a circle bouncing in a rectangle. As shown in the figure below, the rectangle is divided into N×M grids, and the circle fits exactly one grid. The bouncing

ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛

题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, -, n-1. He plans to spend m consecutive days(2 ≤ m ≤ n)in Beijing. During

hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1391 : Countries 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are two antagonistic countries, country A and country B. They are in a war, and keep launching missiles towards each other. It is known that country A will launch N missiles. The i-th miss

hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people could not tolerate the pollution in the city any more, and started a long-lasting protest. Eventually, the municipal government made up its mind to deal w

hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1392 : War Chess 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Rainbow loves to play kinds of War Chess games. There are many famous War Chess games such as "Biography of Cao Cao", "Anecdotes of Wagang Mountain", etc. In this problem, let's c

ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络练习赛 题目4 : Beautiful String

We say a string is beautiful if it has the equal amount of 3 or more continuous letters (in increasing order.) Here are some example of valid beautiful strings: "abc", "cde", "aabbcc", "aaabbbccc". Here are some exa