题目链接:

传送门

题目分析:

线段树妙题,感觉思路奇奇怪怪的,虽然对我来说不是“线段树菜题”(\(ldx\)神仙\(blog\)原话)\(QAQ\)

考虑怎么样维护可合并的信息解决这道题

首先有一个很明显的贪心,一张卡片正反面肯定是能小就小,不带修的话直接就过了

带修的话怎么处理呢,考虑在线段树上维护一个\(sum[0/1]\),表示这个节点\(l\)位置上卡片选正/反面的时候\(r\)位置上卡片的最小取值,不合法赋\(INF\)

重点在\(pushup\)里面,每次\(pushup\)的时候把左子区间维护的两个\(sum\)分别和右子区间左端点的两个值比较一下看合不合法,不合法赋\(INF\)(感觉说的不是很清楚,自己脑补一下\(or\)看代码可能会好懂一点

每次交换可以看做单点修改

代码:

#include<bits/stdc++.h>
#define N (300000 + 10)
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + c - '0'; c = getchar();}
return cnt * f;
}
const int INF = 1000000000 + 7;
int n, m;
int x, y;
int a[N], b[N];
struct node {
int l, r, sum[2];
#define l(p) tree[p].l
#define r(p) tree[p].r
#define sum0(p) tree[p].sum[0]
#define sum1(p) tree[p].sum[1]
}tree[N << 2];
void pushup(int p) {
sum0(p) = sum1(p) = INF;
if (sum0(p << 1) <= a[l(p << 1 | 1)]) sum0(p) = sum0(p << 1 | 1);
if (sum0(p << 1) <= b[l(p << 1 | 1)]) sum0(p) = min(sum0(p), sum1(p << 1 | 1));
if (sum1(p << 1) <= a[l(p << 1 | 1)]) sum1(p) = sum0(p << 1 | 1);
if (sum1(p << 1) <= b[l(p << 1 | 1)]) sum1(p) = min(sum1(p), sum1(p << 1 | 1));
}
void build(int p, int l, int r) {
l(p) = l, r(p) = r;
if (l == r) {sum0(p) = a[l], sum1(p) = b[l]; return;}
int mid = (l + r) >> 1;
build (p << 1, l, mid);
build (p << 1 | 1, mid + 1, r);
pushup(p);
}
void modify(int p, int x, int u, int v) {
if (u > v) swap(u, v);
if (l(p) == r(p)) {sum0(p) = a[l(p)] = u, sum1(p) = b[l(p)] = v; return;}
int mid = (l(p) + r(p)) >> 1;
if (x <= mid) modify(p << 1, x, u, v);
else modify(p << 1 | 1, x, u, v);
pushup(p);
}
int cura, curb;
int main() {
n = read();
for (register int i = 1; i <= n; i++) {
a[i] = read(), b[i] = read();
if (a[i] > b[i]) swap(a[i], b[i]);
}
build(1, 1, n);
m = read();
for (register int i = 1; i <= m; i++) {
x = read(), y = read();
cura = a[x], curb = b[x];
modify(1, x, a[y], b[y]), modify(1, y, cura, curb);
if (sum0(1) == INF && sum1(1) == INF) printf("NIE\n");
else printf("TAK\n");
}
return 0;
}

[POI2014]KAR-Cards的更多相关文章

  1. [POI2014]Cards

    题目大意: 有$n(n\le2\times10^5)$张卡片排成一排,每张卡片正反面有两个数$a_i$和$b_i$.$m(m\le10^6)$次操作,每次交换第$c_i$和第$d_i$张卡片,问若可以 ...

  2. BZOJ 1004 【HNOI2008】 Cards

    题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...

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

  4. BZOJ 3524: [Poi2014]Couriers [主席树]

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1892  Solved: 683[Submit][St ...

  5. bzoj 1004 Cards

    1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...

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

  7. BZOJ 3524: [Poi2014]Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][St ...

  8. CF 204B Little Elephant and Cards

    题目链接: 传送门 Little Elephant and Cards time limit per test:2 second     memory limit per test:256 megab ...

  9. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

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

随机推荐

  1. 20130317 如何批量把文件名称写入txt文档

    1.如何批量把文件名称写入txt文档 COMMAND 窗口例:存放图片的文件夹是 D:\123\就用下面一名命令就OKdir d:\123\*.jpg /b > A.TXT 那么你所以JPG格式 ...

  2. Hbase启动的时候出现:[RpcServer.handler=28,port=60000] ipc.RpcServer: RpcServer.handler=28,port=60000: exiting,master.HMasterCommandLine: Master exiting

    hadoop 版本:CDH5.02 Hbase 版本:hbase-0.96.1.1-cdh5.0.2 配置文件:hbase-site.xml <configuration> <pro ...

  3. 字符编码到python编辑器流程

    字符(存储了信息的东西)编码(): 键盘发送的是电流-->主机(内存)接受到电流(当作010100110101)-->显示屏 接受电流(当作010100110101------->键 ...

  4. Docker学习のC/S模式

    我们操作docker是通过命令行客户端,然后和守护进程通信 以前的是通过命令行 我们还可以通过RemoApI的形式,通过自己的程序访问docker 和守护进程链接方式

  5. 蒙特卡罗定位(Particle Filter Localization)

    1. 蒙特卡罗定位 定位:机器人知道地图信息的情况下如何利用传感器信息确定自己的位置(Localization). 有人会说,定位是不需要地图信息的.机器人知道初始位置,知道左右轮的速度,就可以算出在 ...

  6. 8种形式的Android Dialog使用举例

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  7. Vue 中 computed ,watch,methods 的异同

    methods,watch和computed都是以函数为基础的. computed 和 watch 都可以观察页面的相应式数据的变化.当处理页面的数据变化时,我们有时候很容易滥用watch, 而通常更 ...

  8. JS之缓冲动画

    原素材 main.html <!DOCTYPE html> <html lang="en"> <head> <link href=&quo ...

  9. java8 新特性学习笔记

    Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...

  10. 通过key_len分析联合索引的使用

    The key_len column indicates the length of the key that MySQL decided to use. The length is NULL if ...