时间限制 1 Second  内存限制 512 Mb

题目描述

HPU601球王争霸赛即将举行,ACMER纷纷参加.

现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜.

为保证比赛公平,我们定义比赛规则:

第一轮:[1, 2]pk,然后[3, 4]pk...最后[2i − 1, 2i]pk.

第二轮:[1, 2]W inner同[3, 4]W inner进行pk,然后[5, 6]W inner同[7, 8]W inner...

······

第m轮:最后一个W inner.

现在有q次询问,每次询问(u, v),第u轮第v个胜出者的编号?

输入

第一行一个正整数T,代表有T组测试数据. (1 ≤ T ≤ 10)

每组数据第一行输入正整数n, q,接下来n个正整数ai,表示实力值.(1 ≤ n, ai ≤ 2^16)(1 ≤ q ≤ 10)

接下来q行每行两个整数(u, v),询问第u轮第v个胜出者的编号.(0 ≤ u ≤ 16),(1 ≤ v ≤ 2^16)

输入保证每位选手的实力值ai都不相同,且询问合法

输入样例

1
2 2
2 1
0 2
1 1

输出样例

2
1

思路

线段树查询区间内的最大值,第n轮的第v个胜出者所在的区间为,因为每个人的实力值都不相同,所以可以用map标记每个人的实力值所对应的位置,然后套线段树模板就可以了

(数据比较水,比赛的时候因为没有线段树板子,行神好像是用暴力过去了,我忘了代码长啥样了,所以只有超级长的线段树的代码,行神的暴力代码好像只有五十行左右)

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=3e5+10;
const int mod=1e9+7;
using namespace std;
int a[maxn];
int father[maxn];
int MAX;
struct wzy
{
int left,right;
int value;
}node[maxn<<2];
void build(int i,int left,int right)
{
node[i].left=left;
node[i].right=right;
node[i].value=0;
if(left==right)
{
father[left]=i;
return;
}
build(i<<1,left,(right+left)/2);
build((i<<1)+1,(right+left)/2+1,right);
}
void update(int ri)
{
if(ri==1)
return ;
int fi=ri/2;
int l=node[fi<<1].value;
int r=node[(fi<<1)+1].value;
node[fi].value=max(l,r);
update(ri/2);
}
void query(int i,int l,int r)
{
if(node[i].left==l&&node[i].right==r)
{
MAX=max(MAX,node[i].value);
return ;
}
i=i<<1;
if(l<=node[i].right)
{
if(r<=node[i].right)
query(i,l,r);
else
query(i,l,node[i].right);
}
i+=1;
if(r>=node[i].left)
{
if(l>=node[i].left)
query(i,l,r);
else
query(i,node[i].left,r);
}
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int t;
int n,q;
cin>>t;
while(t--)
{
map<int,int>mp;
cin>>n>>q;
MAX=0;//别忘清零......
build(1,1,n);
for(int i=1;i<=n;i++)
{
cin>>a[i];
mp[a[i]]=i;
node[father[i]].value=a[i];
update(father[i]);
}
int x,y;
while(q--)
{
MAX=0;
cin>>x>>y;
int r=(1<<x)*y;
int l=r-(1<<x)+1;
query(1,l,r);
cout<<mp[MAX]<<endl;
}
}
return 0;
}

HPU组队赛J:Ball King(线段树)的更多相关文章

  1. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

    题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...

  5. hdu 1556 Color the ball (技巧 || 线段树)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. Color the ball(线段树)

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  7. hdu1556 Color the ball 简单线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 简单的线段树的应用 直接贴代码了: 代码: #include<iostream> # ...

  8. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  9. 牛客网 中南林业科技大学第十一届程序设计大赛J题 二分+线段树

    https://www.nowcoder.com/acm/contest/124#question 题意  找第一个不小于K的数的下标,然后对它前一个数加一 解析   我们可以维护一个最大值数组  1 ...

随机推荐

  1. 码云git使用二(从码云git服务器上下载到本地)

    假如我们现在已经把项目添加到码云git服务器了. 我们现在需要通过studio工具把码云git服务器上的某个项目下载到本,并且运行. 1.打开码云网页,找到对应项目的git路径. 2.打开studio ...

  2. python3.7 安装

    python3.7 安装 下载安装 cd /usr/localwget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgztar -xvf ...

  3. MFC CDHtmlDialog 加载本地资源

    步骤:1.资源视图 项目右击选择资源添加,自定义添加新类型 如:JS(会增加JS文件夹)2. 选择1新建的文件夹右击 添加资源 导入 选择js文件引入3. 在资源文件Resource.h文件夹能找到资 ...

  4. 部署Linux项目

    部署Linux项目   1● 下载软件 ftp       安装 2● 创建连接 3● java项目   gunzip –c *.gz tar –xzf *.gz       rm –rf rm -r ...

  5. static与全局变量区别

    作用域不同: 全局变量是不显式用static修饰的全局变量,但全局变量默认是动态的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过extern 全局变量名的声明,就可以使用全局变量 ...

  6. Uva LV 2995 Image Is Everything 模拟,坐标映射,视图映射 难度: 1

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. bzoj2045

    题解: 莫比乌斯反演经典题目 直接套公式好了 代码: #include<bits/stdc++.h> using namespace std; ; typedef long long ll ...

  8. HTML(三)选择器--复杂选择器

    1.父子选择器/派生选择器 <div calss="wrapper"> <span calss="box">123</span&g ...

  9. 4.4 C++虚析构函数

    参考:http://www.weixueyuan.net/view/6373.html 总结: 构造函数是不能声明为虚函数的,析构函数可以被声明为虚函数. 将基类的析构函数声明为虚函数之后,派生类的析 ...

  10. 通过泛型获得继承类的类原型getGenericSuperclass

    首先贴上代码 package com; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * ...