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 ...
随机推荐
- 使用showplan.sql分析sql Performance
在HelloDBA网站找到一个分析sql性能的工具-showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.z ...
- PHP7+Nginx的配置与安装教程详解
下面脚本之家小编把PHP7+Nginx的配置与安装教程分享给大家,供大家参考,本文写的不好还请见谅. 系统环境:centos6.5 x64 软件版本:nginx-1.10.0 php-7.0.6 安装 ...
- Postgres中tuple的组装与插入
1.相关的数据类型 我们先看相关的数据类型: HeapTupleData(src/include/access/htup.h) typedef struct HeapTupleData { uint3 ...
- Linux下如何打开img镜像文件
有些镜像文件为IMG格式,在Linux如何打开呢?例如从微软dreampark下载的Windows Server 2008 R2镜像文件,使用file命令查看: $ file chs_windows_ ...
- Java线上应用故障排查之一:高CPU占用【转】
近期java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下. 方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.htm ...
- Tomcat: Connector中HTTP与AJP差别与整合
apache tomcat 整合(ajp proxy, http proxy) 1.软件: apache: httpd-2.2.17-win32-x86-openssl-0.9.8o.msi tomc ...
- PSQueue队列操作
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈(FILO,First In Last Out,先进后出)属于线性表一样,队 ...
- java基础10 单例模式之饿汉式和懒汉式单例
前言: 软件行业中有23中设计模式 单例模式 模版模式 装饰者模式 观察者模式 工厂模式 ........... 单例模式 1. 单例模式包括 1.1 饿汉式单例 1.2 ...
- 可图性判定--Havel-Hakimi定理
两个概念 1.度序列 若把图G所有顶点的度数排成一个序列S,则称S为图G的度序列. 2.序列是可图的 一个非负整数组成的序列如果是某个无向图的度序列,则称该序列是可图的. Havel-Hakimi定理 ...
- React.js学习之理解JSX和组件
在开启JSX的学习旅程前,我们先了解一下React的基本原理.React本质上是一个"状态机",它只关心两件事:更新DOM和响应事件,React不处理Ajax.路由和数据存储,也不 ...