时间限制 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. npm node sass 安装报错

    报错为 不能找到python2.7,记得曾经已经安装过python,结果npm install cnpm install npm install node-sass 各种不行,结果在cmd 输入pyt ...

  2. 小程序swiper效果高宽设置(微信小程序交流群:604788754)

    swiper的宽和高一定要设置在swiper上面.swiper-item默认继承swiper的宽和高.swiper-item容器里面的宽和高没有继承他的父节点宽和高,需要从新设置. 不明白之处,可以咨 ...

  3. 线性回归之决定系数(coefficient of determination)

    1. Sum Of Squares Due To Error 对于第i个观察点, 真实数据的Yi与估算出来的Yi-head的之间的差称为第i个residual, SSE 就是所有观察点的residua ...

  4. 对大学生学习Linux系统的七项实用建议

    你现在的工作是你所渴望的理想工作吗?或者说这只是你整个职业生涯中的一段插曲?虽然我们每个人都不一定能够说出自己所想的是什么,但是我们心里其实跟明镜似的.相信许多人对于自己喜好的工作投入精力不会有问题, ...

  5. 改变Cube的Shader下的Alpha值,实现Cube若隐若现的效果。

    private float rotaSpeed = 5f; private float timer = 1; private bool flag = true; private float delay ...

  6. day 09 初识函数

    今日主要学习了 一. 什么是函数二. 函数定义, 函数名, 函数体以及函数的调?三. 函数的返回值四. 函数的参数 一, 什么是函数               如果找不到合适的函数名称 ,用 fu ...

  7. Web API之路由浅谈

    Web API的路由,是指明接口地址的方向,是照亮获取数据路上的灯塔,其重要性不言而喻. 本篇文章以vs2015为例,一步步说明路由的创建及使用,其中包括默认路由.自定义路由和特性路由. 一.默认路由 ...

  8. 十九. Python基础(19)--异常

    十九. Python基础(19)--异常 1 ● 捕获异常 if VS异常处理: if是预防异常出现, 异常处理是处理异常出现 异常处理一般格式: try:     <............. ...

  9. CSS特性

    css的特性 css具有两大特性:继承性和层叠性 1.继承性 指的是子元素继承父元素的样式,但没有所有的样式都可以继承(那样就太可怕了) 所以具有继承性的属性主要分为三大类 a.文本属性 font-s ...

  10. TortoiseGit 的下载与安装

    一.下载 访问https://tortoisegit.org/ 二.安装 然后就next,install 配置参考:2. TortoiseGit安装与配置