[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 ...
随机推荐
- docker 错误failed to open stream: Permission denied 解决方法
在服务器上面.运行docker时,php目录会发生权限问题解决方法如下: 1:进入php目录下面 docker exec -ti php56 /bin/bash #进入php容器 chown -R w ...
- jdbc_mysql----interset
- ionic js ion-tabs选项卡栏操作
ionic 选项卡栏操作 ion-tabs ion-tabs 是有一组页面选项卡组成的选项卡栏.可以通过点击选项来切换页面. 对于 iOS,它会出现在屏幕的底部,Android会出现在屏幕的顶部(导航 ...
- SVN Cannot merge into a working copy that has local modifications
我尝试了 主支,分支都提交,但是依然无法合并. 最终,我在服务器上将分支删除,然后主支在拷贝过去. 一,打开服务器资源 二,删除分支 三,拷贝主支到分支 四,刷新分支,就能看到了. 然后在分支项目中, ...
- redis 体系结构
程序strings key-value 类型 ,value不仅是String,也可以是数字.使用strings 类型可以完全实现目前 Memcache 的功能,并且效率更高,还可以享受redis的 ...
- 学习 Apache FileMatchs 规则
# 凡是匹配到 zip,gz,rar,box,log结尾的文件,进行下面的规则进行匹配 <filesmatch ".(zip|gz|rar|box|log)"> Ord ...
- Linux 指令查询帮助
man +指令名 例子: man rename
- leetcode-分治
题目169: 分治:O(nlgn) class Solution: def majorityElement(self, nums: List[int]) -> int: def majorE(l ...
- leetcood学习笔记-82-删除排序链表中的重复元素二
题目描述: 方法一: class Solution(object): def deleteDuplicates(self, head): """ :type head: ...
- day30 python类的继承,抽象类等
Python之路,Day17 = Python基础17-面向对象入门 继承 class Student(People): pass print(Student.__bases__) # 查看 Stud ...