2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询
D - 秋实大哥与战争
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/contest/show/59
Description
男儿何不带吴钩,收取关山五十州。
征战天下是秋实大哥一生的梦想,所以今天他又在练习一个对战游戏。
秋实大哥命令所有士兵从左到右排成了一行来抵挡敌人的攻击。
敌方每一次会攻击一个士兵,这个士兵就会阵亡,整个阵列就会从这个位置断开;同时有的时候已阵亡的士兵会受人赢气息感染而复活。
秋实大哥想知道某一时刻某一个士兵所在的阵列的长度是多少。
Input
接下来m行,每一行是以下三种事件之一:
0 x : 表示x位置的士兵受到攻击阵亡
1 x : 表示x位置的士兵受人赢气息感染复活
2 x : 秋实大哥想知道第x个士兵所在阵列的长度
1≤n,m≤100000,1≤x≤n。
Output
Sample Input
2 2
0 3
2 2
Sample Output
2
HINT
题意
题解:
set或者线段树都行,线段树记录从左边延展的长度,记录从右边延展的长度,和区间最大长度
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 150001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} struct node
{
int l,r;
int len;
int lm,rm,mm;
};
node a[maxn*]; void build(int l,int r,int x)
{
a[x].l=l,a[x].r=r;
a[x].lm=a[x].rm=a[x].len=a[x].mm=r-l+;
if(l!=r)
{
int mid=(l+r)>>;
build(l,mid,x<<);
build(mid+,r,x<<|);
}
}
void updata(int i,int t,int x)
{
if(a[i].l == a[i].r)
{
a[i].lm = a[i].rm = a[i].mm = x;
return ;
}
int mid = (a[i].l+a[i].r)>>;
if(t<=mid)
updata(*i,t,x);
else
updata(*i+,t,x);
a[i].lm = a[*i].lm;
a[i].rm = a[*i+].rm;
a[i].mm = max(max(a[*i].mm,a[*i+].mm),a[*i].rm+a[*i+].lm);
if(a[*i].lm == a[*i].r-a[*i].l+)
a[i].lm += a[*i+].lm;
if(a[*i+].rm == a[*i+].r-a[*i+].l+)
a[i].rm += a[*i].rm;
}
int query(int x,int p)
{
if(a[x].l==a[x].r||a[x].mm==||a[x].mm==a[x].len)
return a[x].mm;
int mid=(a[x].l+a[x].r)>>;
if(p<=mid)
{
if(p>=a[x<<].r-a[x<<].rm+)
return query(x<<,p)+query(x<<|,mid+);
else
return query(x<<,p);
}
else
{
if(p<=a[x<<|].l+a[x<<|].lm-)
return query(x<<|,p)+query(x<<,mid);
else
return query(x<<|,p);
}
} int main()
{
int n,m,x,y;
n=read(),m=read();
build(,n,);
for(int i=;i<m;i++)
{
x=read(),y=read();
if(x==)
updata(,y,);
if(x==)
updata(,y,);
if(x==)
printf("%d\n",query(,y));
}
}
2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询的更多相关文章
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
- 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈
秋实大哥搞算数 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1074 Des ...
- 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和
B - 秋实大哥与花 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
秋实大哥打游戏 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
秋实大哥去打工 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交
E - 秋实大哥与家 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...
- 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树
C - 秋实大哥与快餐店 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 ...
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
秋实大哥与小朋友 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...
- 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp
秋实大哥の恋爱物语 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...
随机推荐
- imperva agent 的重新注册
情况是这样 公司搭了一个环境有mysql的数据库并且安装了agent,imperva管理平台上也可以看到agent的注册信息,但是没想到的是有人把我的虚机给还原快照了,而且还没保存..... 这次写个 ...
- MGR Switch single-Primary to Muti_primary
MGR single_primary 切换 Muti-Primary 模式 root@localhost [(none)]>select * from performance_schema.re ...
- photoshop 安装问题
问题:“安装程序检测到计算机重新启动操作可能处于挂起状态.建议您退出安装程序,重新启动并重试.” 解决: 1.运行 regedit 打开注册表编辑器. 2.依次展开HKEY_LOCAL_MACHINE ...
- Machine Learning系列--深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
在求取有约束条件的优化问题时,拉格朗日乘子法(Lagrange Multiplier) 和KKT条件是非常重要的两个求取方法,对于等式约束的优化问题,可以应用拉格朗日乘子法去求取最优值:如果含有不等式 ...
- PSQueue队列操作
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈(FILO,First In Last Out,先进后出)属于线性表一样,队 ...
- centos6.5 安装、启动vnc
一.安装vnc 1.确保当前账号是root2.查看本机是否已经安装vncserver rpm -qa|grep tigervnc 3.安装vncserver yum -y install tigerv ...
- ireport报表制作, 当一个字段显示的数据太多时(数据过长),则需要自动换行
1.当一个字段显示的数据太长,一个表格放不下,则需要自动换行,选中要更改的表格(要显示动态内容的字段),设置属性Stretch with overflow 为钩选状态. 未勾选之前: 勾选之后: 2. ...
- NTP详解-转
网管实战:Linux时间服务器配置 [IT168 专稿]目前计算机网络中各主机和服务器等网络设备的时间基本处于无序的状态.随着计算机网络应用的不断涌现,计算机的时间同步问题成为愈来愈重要的事情.以Un ...
- leetcode 之Longest Consecutive Sequence(六)
这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张. 另外这个思路可以进行聚类,把连续的标记为一类. int longestConsecutive(const vecto ...
- css如何将div画成三角形
首先了解一下盒模型: 盒模型 先看一段代码: #div1{ height: 100px; border-style: solid; border-width: 100px 100px 100px 10 ...