Codeforces 461C. Appleman and a Sheet of Paper

每次只把短的部分往长的部分折叠,直接用树状数组爆搞就可以了。

每次长度都缩小一些暴力的复杂度不是太高,启发式暴力????

C. 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.

Sample test(s)

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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=101000;

int a[maxn],n,q;

inline int lowbit(int x)
{
	return x&(-x);
}

void add(int p,int v)
{
	for(int i=p;i<=n;i+=lowbit(i))
		a[i]+=v;
}

int sum(int p)
{
	int sum=0;
	for(int i=p;i;i-=lowbit(i))
		sum+=a[i];
	return sum;
}

int main()
{
	scanf("%d%d",&n,&q);
	int flag=0;
	for(int i=1;i<=n;i++) add(i,1);
	int beg=1,end=n;
	while(q--)
	{
		int k,p,l,r;
		scanf("%d",&k);
		if(k==1)
		{
			scanf("%d",&p);
			if(p<=(end-beg+1)/2)
			{
				if(flag==0)
				{
					for(int i=p+beg-1,j=p+beg;i>=beg;i--,j++)
					{
						add(j,sum(i)-sum(i-1));
					}
					beg=beg+p;
				}
				else
				{
					for(int i=end-p,j=end-p+1;j<=end;i--,j++)
					{
						add(i,sum(j)-sum(j-1));
					}
					end=end-p;
				}
			}
			else
			{
				if(flag==0)
				{
					for(int i=beg+p-1,j=p+beg;j<=end;j++,i--)
					{
						add(i,sum(j)-sum(j-1));
					}
					end=beg+p-1;
				}
				else
				{
					for(int i=end-p,j=end-p+1;i>=beg;i--,j++)
					{
						add(j,sum(i)-sum(i-1));
					}
					beg=end-p+1;
				}
				flag=flag^1;
			}
		}
		else if(k==2)
		{
			scanf("%d%d",&l,&r);
			l++;
			if(flag==0)
			{
				l=beg+l-1; r=beg+r-1;
				printf("%d\n",sum(r)-sum(l-1));
			}
			else
			{
				l=end-l+1; r=end-r+1;
				printf("%d\n",sum(l)-sum(r-1));
			}
		}
	}
	return 0;
}
时间: 2024-09-28 02:26:28

Codeforces 461C. Appleman and a Sheet of Paper的相关文章

Codeforces 461C Appleman and a Sheet of Paper(模拟)

题目链接:Codeforces 461C Appleman and a Sheet of Paper 题目大意:就是一个叠被子的过程,穿插着询问一段区间上被子的单位厚度. 解题思路:用前缀和数组模拟即可.因为对于折超过一半的处理为将令一半叠上来,所以需要变量记录当前被子的正反状态.处理好下标关系即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

461C. Appleman and a Sheet of Paper(树状数组)

C. Appleman and a Sheet of Paper time limit per test 2 seconds memory limit per test 256 megabytes 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 she

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

C. Appleman and a Sheet of Paper 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

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper

题目地址:http://codeforces.com/contest/461/problem/C 题目大意:见原题. 算法分析:启发式暴力,每次把短的纸带折到长的纸带中,在全局记一个rev标记.注意细节. Code: #include <cstdio> #include <algorithm> #define N 100000 using namespace std; bool rev; int n,q,beg=1,end,typ,p,l,r,bit[N+10]; inline v

Codeforces 461B Appleman and Tree(木dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量.保证每一个联通分量有且仅有1个黑色节点.问有多少种切割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include &l

Codeforces 461B Appleman and Tree(树形dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每个节点的父亲节点,以及每个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量,保证每个联通分量有且仅有1个黑色节点.问有多少种分割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的分割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include <c

Codechef A Game With a Sheet of Paper

Discription Yuuko and Nagi like to play the following game: Initially they take a checkered sheet of paper of the size of N x M cells. Then, one cell, let's call this cell (X, Y) is marked. Then, they take the moves alternately. A move consists of ma

CodeForces 462B Appleman and Card Game(贪心)

题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards

codeforces 462C Appleman and Toastman 解题报告

题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得到一个集合,他计算所有数的和,并且将它加入到score里.之后他将这个集合传给Appleman. (2)Appleman得到的集合如果只有一个数,就把它弃之,否则将这个集合分成 两个不相交且不空的集合,传回给Toastman. 这些操作不断执行直到集合个数变为0,也就是通过使集合都变成只有一个元素而