[POI2014]Cards
题目大意:
有$n(n\le2\times10^5)$张卡片排成一排,每张卡片正反面有两个数$a_i$和$b_i$。$m(m\le10^6)$次操作,每次交换第$c_i$和第$d_i$张卡片,问若可以任意翻转卡片,是否存在一种方案使得卡片上的数字构成一个不下降序列。
思路:
用线段树维护区间,左端点取最大/小值时,右端点取最大值还是最小值。
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
int a[N][];
class SegmentTree {
#define _left <<1
#define _right <<1|1
private:
int val[N<<][];
void push_up(const int &p,const int &b,const int &e) {
const int mid=(b+e)>>;
val[p][]=val[p][]=-;
if(val[p _left][]==-) return;
if(a[mid+][]>=a[mid][val[p _left][]]) val[p][]=val[p _right][];
if(a[mid+][]>=a[mid][val[p _left][]]) val[p][]=val[p _right][];
if(val[p _left][]==-) return;
if(a[mid+][]>=a[mid][val[p _left][]]) val[p][]=val[p _right][];
if(a[mid+][]>=a[mid][val[p _left][]]) val[p][]=val[p _right][];
}
public:
void build(const int &p,const int &b,const int &e) {
if(b==e) {
val[p][]=;
val[p][]=;
return;
}
const int mid=(b+e)>>;
build(p _left,b,mid);
build(p _right,mid+,e);
push_up(p,b,e);
}
void modify(const int &p,const int &b,const int &e,const int &x) {
if(b==e) return;
const int mid=(b+e)>>;
if(x<=mid) modify(p _left,b,mid,x);
if(x>mid) modify(p _right,mid+,e,x);
push_up(p,b,e);
}
bool query() const {
return val[][]!=-;
}
#undef _left
#undef _right
};
SegmentTree t;
int main() {
const int n=getint();
for(register int i=;i<=n;i++) {
a[i][]=getint(),a[i][]=getint();
if(a[i][]>a[i][]) std::swap(a[i][],a[i][]);
}
t.build(,,n);
for(register int m=getint();m;m--) {
const int x=getint(),y=getint();
std::swap(a[x][],a[y][]);
std::swap(a[x][],a[y][]);
t.modify(,,n,x);
t.modify(,,n,y);
puts(t.query()?"TAK":"NIE");
}
return ;
}
[POI2014]Cards的更多相关文章
- BZOJ 1004 【HNOI2008】 Cards
题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...
- Codeforces Round #384 (Div. 2) 734E Vladik and cards
E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- bzoj 1004 Cards
1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...
- codeforces 744C Hongcow Buys a Deck of Cards
C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- CF 204B Little Elephant and Cards
题目链接: 传送门 Little Elephant and Cards time limit per test:2 second memory limit per test:256 megab ...
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
随机推荐
- Mysql忘记root密码怎么办?(已解决)
为了写这篇文档,假装一下忘记密码!!!! 首先我数据库是正常的,可以使用. root@localhost:~# mysql -uroot -p mysql> mysql> show dat ...
- python 学习分享-socket编程
socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思. 通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟 ...
- NIO--1
1.为什么不直接用jdk NIO(1) API繁杂(2) 原始NIO可靠性不是很高.可靠性包括:断开重连,网络闪断,半包读写,失败缓存(3) NIO 的epoll BUG会导致多路复用器Selecto ...
- 【bzoj4419】[Shoi2013]发微博 STL-set
题目描述 刚开通的SH微博共有n个用户(1..n标号),在短短一个月的时间内,用户们活动频繁,共有m条按时间顺序的记录: ! x 表示用户x发了一条微博: + x y 表示用户x和用户y成为了好友 ...
- 【bzoj1044】[HAOI2008]木棍分割 二分+dp
题目描述 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, 砍完后n根木棍被分成了很多段,要求满足总长度最大的一段长度最小, 并且 ...
- h5 Visibility API总结
最近活动中的小游戏,有涉及页面隐藏或app后台运行时候,暂停游戏的功能,使用了h5的Visibility API,在此总结如下: 两个属性 document.hidden (Read only) 如果 ...
- 校内训练0602 习题exercise
[题目大意] f(i)=((Af(i-1)+B)/(Cf(i-1)+D)) mod P. 给出f(0), A, B, C, D, P, n,求f(n). 多组数据T<=1e4 n<=1e1 ...
- 无法安装MVC3,一直卡在vs10-kb2483190
原文发布时间为:2011-05-15 -- 来源于本人的百度文章 [由搬家工具导入] 无法安装MVC3,一直卡在vs10-kb2483190 解决方案: 1、用winrar 解压 MVC3安装文件 2 ...
- 组合数问题(NOIP2016)
原题传送门 这题啊. 裸的杨辉三角. 预处理杨辉三角和答案即可 下面贴代码 #include<iostream> #include<cstdio> #include<al ...
- 静态链接和动态链接库混用导致的链接错误LINK2005
对于一个静态链接库L.lib,它的使用者app.exe会静态链接L.lib,意思是app.exe会将L.lib中的代码(app需要的部分,例如函数定义,类的定义等等)链接到app.exe中. 而对 ...