【Codeforces Round 263 (Div 2)E】【坐标映射 脑洞】Appleman and a Sheet of Paper 折纸游戏 区间查询
来源:互联网 发布:开淘宝天猫店要多少钱 编辑:程序博客网 时间:2024/05/07 23:06
#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|1typedef 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 1001 92 0 12 0 22 0 32 0 9*/
0 0
- 【Codeforces Round 263 (Div 2)E】【坐标映射 脑洞】Appleman and a Sheet of Paper 折纸游戏 区间查询
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper
- Codeforces 461C. Appleman and a Sheet of Paper
- Codeforces 461C Appleman and a Sheet of Paper(模拟)
- CodeForces 461C Appleman and a Sheet of Paper
- Codeforces Round #263 (Div. 2)A. Appleman and Easy Task
- Codeforces Round #263 (Div. 2)A. Appleman and Easy Task
- 461C - Appleman and a Sheet of Paper 模拟
- Codeforces Round #263 (Div. 1) A. Appleman and Toastman
- Codeforces Round #263 (Div. 2) A.Appleman and Easy Task(坑)
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)C. Appleman and Toastman
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2) C. Appleman and Toastman
- Codeforces Round #263 (Div. 2)-C. Appleman and Toastman
- Codeforces Round #263 (Div. 2)-B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)-C. Appleman and Toastman
- CF 461C - Appleman and a Sheet of Paper (树状数组)
- oop模式实现activeMQ传送消息(activeMQ 的API)
- 解决mAdapter.notifyDataSetChanged()无效问题
- UVA 11988——Broken Keyboard (a.k.a. Beiju Text)
- Codeforces 653C Bear and Up-Down【暴力】
- 博客开通纪念
- 【Codeforces Round 263 (Div 2)E】【坐标映射 脑洞】Appleman and a Sheet of Paper 折纸游戏 区间查询
- 【bzoj4407】【于神之怒加强版】【莫比乌斯反演】
- hdu2016校赛 C
- PAT 乙级 1016.部分A+B
- Java常用类
- 隐藏的线程死锁
- android studio 1.5安装笔记
- JAVA环境配置
- [LeetCode][数论]Roman to Integer