【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

相关文章

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

使用MapStruct实现Java对象映射的示例代码

《使用MapStruct实现Java对象映射的示例代码》本文主要介绍了使用MapStruct实现Java对象映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、什么是 MapStruct?二、实战演练:三步集成 MapStruct第一步:添加 Mave

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

MySQL之复合查询使用及说明

《MySQL之复合查询使用及说明》文章讲解了SQL复合查询中emp、dept、salgrade三张表的使用,涵盖多表连接、自连接、子查询(单行/多行/多列)及合并查询(UNION/UNIONALL)等... 目录复合查询基本查询回顾多表查询笛卡尔积自连接子查询单行子查询多行子查询多列子查询在from子句中使

Vue3 如何通过json配置生成查询表单

《Vue3如何通过json配置生成查询表单》本文给大家介绍Vue3如何通过json配置生成查询表单,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录功能实现背景项目代码案例功能实现背景通过vue3实现后台管理项目一定含有表格功能,通常离不开表单

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java实现复杂查询优化的7个技巧小结

《Java实现复杂查询优化的7个技巧小结》在Java项目中,复杂查询是开发者面临的“硬骨头”,本文将通过7个实战技巧,结合代码示例和性能对比,手把手教你如何让复杂查询变得优雅,大家可以根据需求进行选择... 目录一、复杂查询的痛点:为何你的代码“又臭又长”1.1冗余变量与中间状态1.2重复查询与性能陷阱1.

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十