[POI2014]KAR-Cards
题目链接:
题目分析:
线段树妙题,感觉思路奇奇怪怪的,虽然对我来说不是“线段树菜题”(\(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的更多相关文章
- [POI2014]Cards
题目大意: 有$n(n\le2\times10^5)$张卡片排成一排,每张卡片正反面有两个数$a_i$和$b_i$.$m(m\le10^6)$次操作,每次交换第$c_i$和第$d_i$张卡片,问若可以 ...
- 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 ...
随机推荐
- 20140316 window live write 插件 推荐代码高亮插件 构造函数只能用初始化成员列表方式的例子
1.window live write 插件:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html 2.推荐代码高亮插件:W ...
- 20140315 模板类pair的用法 2、visual 2010调整代码格式是ctrl+k+
1.模板类pair的用法 包含头文件#include<utility> http://blog.csdn.net/laixingjun/article/details/9005200 ...
- spring boot资源文件配置读取
一般业务配置,尽量新建自己的配置文件,来读取,而不是配置在application.properties或application-*.properties或yml/yaml配置中. applicatio ...
- C++之关键字&标识符命名规则
关键字 **作用:**关键字是C++中预先保留的单词(标识符) * **在定义变量或者常量时候,不要用关键字** C++关键字如下: 提示:在给变量或者常量起名称时候,不要用C++得关键字,否则会产生 ...
- <Django>博客项目
0.项目的通用流程 项目立项 需求分析 原型 前端 页面设计 UI及交互实现 后端 架构设计 数据库设计 代码模板实现 单元测试 网站整合 功能及集成测试 网站发布 1.BBS项目需求分析 需要哪些表 ...
- Oracle SQL外连接
SQL提供了多种类型的连接方式,它们之间的区别在于:从相互交叠的不同数据集合中选择用于连接的行时所采用的方法不同.连接类型 定义内连接 只连接匹配的行左外连接 ...
- 将sparkStreaming结果保存到Redshift数据库
1.保存到redshift数据库的代码 package test05 import org.apache.log4j.{Level, Logger}import org.apache.spark.rd ...
- 纯css制作三级菜单
<!DOCTYPE html> <html> <head> <title>三级菜单</title> <meta charset=&qu ...
- 2019-5-21-dotnet-使用-GC.GetAllocatedBytesForCurrentThread-获取当前线程分配过的内存大小...
title author date CreateTime categories dotnet 使用 GC.GetAllocatedBytesForCurrentThread 获取当前线程分配过的内存大 ...
- Android开发 AndroidStudio解决Error:moudle not specified
问题描述 在使用Android Studio 进行Builder APKs的时候,如果发现无法degub, 进行配置的时候 没有module可以进行指定 问题原因 项目未与Grade Files 文件 ...