【Codeforces Round 263 (Div 2)E】【坐标映射 脑洞】Appleman and a Sheet of Paper 折纸游戏 区间查询

本文主要是介绍【Codeforces Round 263 (Div 2)E】【坐标映射 脑洞】Appleman and a Sheet of Paper 折纸游戏 区间查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Appleman and a Sheet of Paper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:

  1. Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi).
  2. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper.

Please look at the explanation of the first test example for better understanding of the problem.

Input

The first line contains two integers: n and q (1  ≤ n ≤ 105; 1 ≤ q ≤ 105) — the width of the paper and the number of queries.

Each of the following q lines contains one of the described queries in the following format:

  • "1 pi(1 ≤ pi < [current width of sheet]) — the first type query.
  • "2 li ri(0 ≤ li < ri ≤ [current width of sheet]) — the second type query.
Output

For each query of the second type, output the answer.

Examples
input
7 4
1 3
1 2
2 0 1
2 1 2
output
4
3
input
10 9
2 2 9
1 1
2 0 1
1 8
2 0 8
1 2
2 1 3
1 4
2 2 4
output
7
2
10
4
5
Note

The pictures below show the shapes of the paper during the queries of the first example:

After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.



#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, q;
int o, p, l, r;
int a[N];
int s[N];
int L, R; bool flip;
void init()
{L = 1; R = n;for (int i = 1; i <= n; ++i){a[i] = 1;s[i] = s[i - 1] + a[i];}flip = 0;
}
void solve()
{while (q--){scanf("%d", &o);if (o == 1){int len = R - L + 1;scanf("%d", &p);if (!flip){//翻转部分更少,我们进行翻转if (p <= len - p){int l = L + p;int r = L + p + p - 1;int U = l + l - 1;s[l - 1] = 0;for (int i = l; i <= r; ++i){a[i] += a[U - i];s[i] = s[i - 1] + a[i];}L += p;}//不翻转部分更少,我们反向操作else{p = len - p;int l = R - p - p + 1;int r = R - p;int U = r + r + 1;for (int i = l; i <= r; ++i){a[i] += a[U - i];s[i] = s[i - 1] + a[i];}R -= p;flip = 1;}}else{//翻转部分更少,我们进行翻转if (p <= len - p){int l = R - p - p + 1;int r = R - p;int U = r + r + 1;for (int i = l; i <= r; ++i){a[i] += a[U - i];s[i] = s[i - 1] + a[i];}R -= p;}//不翻转部分更少,我们反向操作else{p = len - p;int l = L + p;int r = L + p + p - 1;int U = l + l - 1;s[l - 1] = 0;for (int i = l; i <= r; ++i){a[i] += a[U - i];s[i] = s[i - 1] + a[i];}L += p;flip = 0;}}}else{scanf("%d%d", &l, &r); --r;int ans;if (!flip)ans = s[L + r] - s[L + l - 1];else ans = s[R - l] - s[R - r - 1];printf("%d\n", ans);}}
}
int main()
{while (~scanf("%d%d", &n, &q)){init();solve();}return 0;
}
/*
【trick&&吐槽】
CFdiv2E并不一定很难。毕竟诗诗都想出来怎么做了233>_<【题意】
给你一张1*n(1e5)的纸条,纸条上有0~n共计n个刻度。
我们有q(1e5)个操作。
对于每个操作,有两种类型:
(1,pi),表示我们把纸条的[0,pi]向右翻转。
(2,l,r),表示我们查询l~r区间段有多少个纸条单位【类型】
复杂度分析 坐标映射【分析】
其实,我们每次只要翻转较小的一侧就好了。
唯一需要处理的是坐标映射。
我们只要记录一个实际区间,同时记录flip表示是否翻转过。
如果未翻转过,操作从从左往右翻转,从左往右查询。
如果翻转过,操作从右向左翻转,从右向左查询【时间复杂度&&优化】
总翻转长度不会超过n,复杂度为O(n)【数据】
10 100
1 9
2 0 1
2 0 2
2 0 3
2 0 9*/


这篇关于【Codeforces Round 263 (Div 2)E】【坐标映射 脑洞】Appleman and a Sheet of Paper 折纸游戏 区间查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/744487

相关文章

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

查询SQL Server数据库服务器IP地址的多种有效方法

《查询SQLServer数据库服务器IP地址的多种有效方法》作为数据库管理员或开发人员,了解如何查询SQLServer数据库服务器的IP地址是一项重要技能,本文将介绍几种简单而有效的方法,帮助你轻松... 目录使用T-SQL查询方法1:使用系统函数方法2:使用系统视图使用SQL Server Configu

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

MYSQL关联关系查询方式

《MYSQL关联关系查询方式》文章详细介绍了MySQL中如何使用内连接和左外连接进行表的关联查询,并展示了如何选择列和使用别名,文章还提供了一些关于查询优化的建议,并鼓励读者参考和支持脚本之家... 目录mysql关联关系查询关联关系查询这个查询做了以下几件事MySQL自关联查询总结MYSQL关联关系查询

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

查询Oracle数据库表是否被锁的实现方式

《查询Oracle数据库表是否被锁的实现方式》本文介绍了查询Oracle数据库表是否被锁的方法,包括查询锁表的会话、人员信息,根据object_id查询表名,以及根据会话ID查询和停止本地进程,同时,... 目录查询oracle数据库表是否被锁1、查询锁表的会话、人员等信息2、根据 object_id查询被

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

数据库oracle用户密码过期查询及解决方案

《数据库oracle用户密码过期查询及解决方案》:本文主要介绍如何处理ORACLE数据库用户密码过期和修改密码期限的问题,包括创建用户、赋予权限、修改密码、解锁用户和设置密码期限,文中通过代码介绍... 目录前言一、创建用户、赋予权限、修改密码、解锁用户和设置期限二、查询用户密码期限和过期后的修改1.查询用

使用SQL语言查询多个Excel表格的操作方法

《使用SQL语言查询多个Excel表格的操作方法》本文介绍了如何使用SQL语言查询多个Excel表格,通过将所有Excel表格放入一个.xlsx文件中,并使用pandas和pandasql库进行读取和... 目录如何用SQL语言查询多个Excel表格如何使用sql查询excel内容1. 简介2. 实现思路3