【BZOJ3526】[Poi2014]Card 线段树

【BZOJ3526】[Poi2014]Card

Description

有n张卡片在桌上一字排开,每张卡片上有两个数,第i张卡片上,正面的数为a[i],反面的数为b[i]。现在,有m个熊孩子来破坏你的卡片了!
第i个熊孩子会交换c[i]和d[i]两个位置上的卡片。
每个熊孩子捣乱后,你都需要判断,通过任意翻转卡片(把正面变为反面或把反面变成正面,但不能改变卡片的位置),能否让卡片正面上的数从左到右单调不降。

Input

第一行一个n。
接下来n行,每行两个数a[i],b[i]。
接下来一行一个m。
接下来m行,每行两个数c[i],d[i]。

Output

m行,每行对应一个答案。如果能成功,输出TAK,否则输出NIE。

Sample Input

4
2 5
3 4
6 3
2 7
2
3 4
1 3

Sample Output

NIE
TAK

HINT

【样例解释】
交换3和4后,卡片序列为(2,5) (3,4) (2,7) (6,3),不能成功。
交换1和3后,卡片序列为(2,7) (3,4) (2,5) (6,3),翻转第3张卡片,卡片的正面为2,3,5,6,可以成功。

【数据范围】
n≤200000,m≤1000000,0≤a[i],b[i]≤10000000,1≤c[i],d[i]≤n.

题解:线段树的区间合并好题~

对于区间x,我们令s[x][0/1][0/1]表示i的左端点选择(a/b),右端点选择(a/b)能否单调不降,然后pushup一下就行了

#include <cstdio>
#include <cstring>
#include <iostream>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=200010;
int n,m;
int s[maxn<<2][2][2],v[maxn][2];
void pushup(int l,int r,int x)
{
	int mid=l+r>>1,i,j,k;
	for(i=0;i<=1;i++)	for(j=0;j<=1;j++)	for(s[x][i][j]=0,k=0;k<=1;k++)	for(l=0;l<=1;l++)
		s[x][i][j]|=s[lson][i][k]&s[rson][l][j]&(v[mid][k]<=v[mid+1][l]);
}
void build(int l,int r,int x)
{
	if(l==r)
	{
		s[x][1][1]=s[x][0][0]=1;
		return ;
	}
	int mid=l+r>>1;
	build(l,mid,lson),build(mid+1,r,rson);
	pushup(l,r,x);
}
void updata(int l,int r,int x,int a)
{
	if(l==r)
	{
		s[x][1][1]=s[x][0][0]=1;
		return ;
	}
	int mid=l+r>>1;
	if(a<=mid)	updata(l,mid,lson,a);
	else	updata(mid+1,r,rson,a);
	pushup(l,r,x);
}
int rd()
{
	int ret=0,f=1;	char gc=getchar();
	while(gc<‘0‘||gc>‘9‘)	{if(gc==‘-‘)f=-f;	gc=getchar();}
	while(gc>=‘0‘&&gc<=‘9‘)	ret=ret*10+gc-‘0‘,gc=getchar();
	return ret*f;
}
int main()
{
	n=rd();
	int i,a,b;
	for(i=1;i<=n;i++)	v[i][0]=rd(),v[i][1]=rd();
	build(1,n,1),m=rd();
	for(i=1;i<=m;i++)
	{
		a=rd(),b=rd();
		swap(v[a][0],v[b][0]),swap(v[a][1],v[b][1]);
		updata(1,n,1,a),updata(1,n,1,b);
		if(s[1][0][0]|s[1][0][1]|s[1][1][0]|s[1][1][1])	printf("TAK\n");
		else	printf("NIE\n");
	}
	return 0;
}
时间: 2024-08-01 18:56:43

【BZOJ3526】[Poi2014]Card 线段树的相关文章

bzoj 3832: [Poi2014]Rally(线段树+拓扑排序)

3832: [Poi2014]Rally Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special Judge Submit: 113  Solved: 56 [Submit][Status][Discuss] Description An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long distance cyclis

BZOJ 3524 [Poi2014]Couriers 可持久化线段树

题意:链接 方法:可持久化线段树. 解析: 可持久化数据结构好神啊,感觉都好玄妙的感觉. 首先建树的目的就是建立一棵权值树,维护的是在L,R里某些权值的数的出现个数.然后呢,对于1~n每个节点建一棵树,并且是基于前一棵树的基础上的.然后对于每一次的新值我们只需要update一次,并且连接一下原来的树? 神犇们不是说这种结构就是一堆线段树连啊连就出来了吗. 查询的时候呢?有一些小改变,据说是以二分为基础的查询. 神犇们发明这种数据结构的时候,就发现了这种数据结构里的所有线段树是可以相减的这种性质,

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise o

POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

题意  n个人顺时针围成一圈玩约瑟夫游戏  每个人手上有一个数val[i]   开始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k] > 0 时向左数val[k]个  第m出队的人可以得到m的约数个数个糖果  问得到最多糖果的人是谁 约瑟夫环问题  n比较大 直接模拟会超时   通过线段树可以让每次出队在O(logN)时间内完成  类似上一道插队的题  线段树维护对应区间还有多少个人没出队  那么当我们知道出队的人在剩余人中排第几个

POJ2886 Who Gets the Most Candies? 【线段树】+【单点更新】+【模拟】+【反素数】

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9416   Accepted: 2868 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise o

[poj 2886] Who Gets the Most Candies? 线段树

Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the oth

URAL 2014 Zhenya moves from parents 线段树

线段树,前缀和最小 Zhenya moves from parents Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Zhenya moved from his parents' home to study in other city. He didn't take any cash with him, he only took his f

(线段树,反素数)poj2886-Who Gets the Most Candies?

N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the inte

POJ 题目2513 Who Gets the Most Candies?(线段树)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 11682   Accepted: 3653 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise