2017ICPC北京赛区网络赛 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 first line contains an integer k (0 ≤ k ≤ 17).

The following line contains 2k integers, a0, a1, …, a2^k-1 (-2k ≤ ai < 2k).

The next line contains a integer  (1 ≤ Q < 2k), indicating the number of queries. Then next Q lines, each line is one of:

1. 1 l r: Output Minx,y∈[l,r]{ax?ay}. (0 ≤ l ≤ r < 2k)

2. 2 x y: Let ax=y. (0 ≤ x < 2k, -2k ≤ y < 2k)

输出

For each query 1, output a line contains an integer, indicating the answer.

样例输入

1
3
1 1 2 2 1 1 2 2
5
1 0 7
1 1 2
2 1 2
2 2 2
1 1 2

样例输出

1
1
4

分析得出,一区间的乘积最小值,只有三种情况1.最大值的平方(最大值为负数)2.最小值的平方(最小值为正数)3.最大值与最小值的乘积(最大值是正,最小值为负)得到此结果后,只需改一下线段树的模板,将每段区间的最大最小值保存下来即可。
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const long long Max=140000;
 7 int n;
 8 struct node
 9 {
10     long long b,s;
11 }Tree[Max<<2];
12 long long bb,ss;
13 void build(int k,int l,int r)//建线段树,k表示子节点坐标
14 {
15     if(l == r)
16     {
17         scanf("%lld",&Tree[k].b);
18         Tree[k].s=Tree[k].b;
19     }
20     else
21     {
22         int mid = (l+r)/2;
23         build(k*2,l,mid);
24         build(k*2+1,mid+1,r);
25         Tree[k].b=max(Tree[k*2].b, Tree[k*2+1].b);
26         Tree[k].s=min(Tree[k*2].s, Tree[k*2+1].s);
27     }
28 }
29 void query(int a,int b,int k,int l,int r)//a,b是当前查询区间,k是当前的根节点,l,r是要求查询区间
30 {
31     if(a >= l && b <= r)
32     {
33         bb=max(bb,Tree[k].b);
34         ss=min(ss,Tree[k].s);
35         //return min(min(Tree[k].b*Tree[k].b,Tree[k].b*Tree[k].s),Tree[k].s*Tree[k].s);
36         return;
37     }
38     else
39     {
40         //long long ans = Max*Max;
41         int mid = (a+b)/2;
42         if(l <= mid)query(a,mid,k*2,l,r);
43         if(r > mid) query(mid+1,b,k*2+1,l,r);
44         return;
45         //return ans;
46     }
47 }
48 void update(int l,int r,int k,int pos,long long v)//l,r是查询区间,k是当前根节点,pos是查询位置
49 {
50     if(l == r)
51     {
52         Tree[k].b=v;
53         Tree[k].s=v;
54     }
55     else{
56         int mid = (l+r)/2;
57         if(pos <= mid) update(l,mid,k*2,pos,v);
58         if(pos > mid) update(mid+1,r,k*2+1,pos,v);
59         Tree[k].b=max(Tree[k*2].b, Tree[k*2+1].b);
60         Tree[k].s=min(Tree[k*2].s, Tree[k*2+1].s);
61     }
62 }
63
64 int main()
65 {
66     int T,l,r,pos;
67     long long val;
68     int k,c,order;
69     scanf("%d",&T);
70     for(int t=1;t<=T;t++)
71     {
72
73         scanf("%d",&k);
74         n=pow(2.0,k);
75         build(1,1,n);
76         scanf("%d",&c);
77         while(c--)
78         {
79             scanf("%d",&order);
80             if(order==1)
81             {
82                 scanf("%d%d",&l,&r);
83                 bb=-Max;ss=Max;
84                 long long tmpans;
85                 query(1,n,1,l+1,r+1);
86                 tmpans= min(min(bb*bb,bb*ss),ss*ss);
87                 printf("%lld\n",tmpans);
88             }
89             else if(order==2)//点更新
90             {
91                 scanf("%d%lld",&pos,&val);
92                 update(1,n,1,pos+1,val);
93             }
94         }
95     }
96     return 0;
97 }
时间: 2024-10-22 04:23:38

2017ICPC北京赛区网络赛 Minimum(数学+线段树)的相关文章

2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)

描述 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 these m days, he intends to use the first day and another day to vis

hdu 4031 2011成都赛区网络赛A题 线段树 ***

就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种  -_-! 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

2015 ACM-ICPC 北京赛区 网络赛 部分 题解

由于博主水平及智商不足,所以暂时只能放出下列的题目的题解. A B D F G H 需要其他题解请自行前往 http://talk.icpc-camp.org/ 题目地址:hihocoder 1227-1236 A.  The Cats' Feeding Spots 题意:给出M个点,求以其中一个点为圆心,最小半径的圆,使得这个圆里恰好有N个点. (半径一定要是整数,且点不能恰好在圆上). 方法:暴力,稍微注意一下精度什么的就好了,1A. 1 #include <bits/stdc++.h>

Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries on the string.There are two types of queries:1. Flipping the bits (i.e., changing all 1 to 0 and 0 to 1) between l and r (inclusive).2. Counting the

hdu 4046 2011北京赛区网络赛G 线段树 ***

还带这么做的,卧槽,15分钟就被A了的题,居然没搞出来 若某位是1,则前两个为wb,这位就是w 1 #include<cstdio> 2 #include<cstring> 3 #define lson l,m,rt<<1 4 #define rson m+1,r,rt<<1|1 5 using namespace std; 6 const int maxn=50010; 7 int n,m,sum[maxn<<2]; 8 char str[ma

hdu 4041 2011北京赛区网络赛F 组合数+斯特林数 ***

插板法基础知识 斯特林数见百科 1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #define LL long long 6 #define eps 1e-7 7 #define MOD 1000000007 8 using namespace std; 9 int c[2001][2001]={1},stir2[1005][1005]={1};

2015 ACM-ICPC国际大学生程序设计竞赛北京赛区网络赛 1002 Mission Impossible 6

题目链接: #1228 : Mission Impossible 6 解题思路: 认真读题,细心模拟,注意细节,就没有什么咯!写这个题解就是想记录一下rope的用法,以后忘记方便复习. rope(块状链表)属于SGI STL的一部分,不属于ISO C++标准库,但libstdc++-v3也包含了扩展,在头文件#include<ext/rope>,using namespace __gnu_cxx命名空间中.可以在很短的时间内实现快速的插入.删除和查找字符串. rope.size()    返回

hdu 4068 福州赛区网络赛A 数学 ***

a1/sum 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3