【BZOJ 1018】 [SHOI2008]堵塞的交通traffic
【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1018
【题意】
【题解】
按照这里的题解写的http://blog.csdn.net/popoqqq/article/details/44116729
主要思路就是根据最小单元的合并方式推出整个线段的合并.
其中要往左走和往右走的过程,可以一开始让那个点往左移动,看看最远能到哪里,右边的点也一样,一直向右移动看看最远能到哪;
然后在最左和最右之间求联通性;
a[x][y]表示这个区间的左端点的上下方和右端点的上下方的连通性;
可以根据这个合并区间;
线段树操作特别是往最左找的过程不好写;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
const int M = 1e5 + 100;
int n;
bool a[M][2];
struct abcd
{
bool a[2][2];
abcd(bool _ = false)
{
a[0][0] = a[1][1] = true;
a[0][1] = a[1][0] = _;
}
bool* operator [] (int x)
{
return a[x];
}
friend abcd merge(bool sta[2], abcd x, abcd y)
{
abcd re;
re[0][0] = (x[0][0] & sta[0] & y[0][0]) | (x[0][1] & sta[1] & y[1][0]);
re[1][1] = (x[1][1] & sta[1] & y[1][1]) | (x[1][0] & sta[0] & y[0][1]);
re[0][1] = (x[0][0] & sta[0] & y[0][1]) | (x[0][1] & sta[1] & y[1][1]);
re[1][0] = (x[1][0] & sta[0] & y[0][0]) | (x[1][1] & sta[1] & y[1][0]);
return re;
}
};
struct segtree
{
segtree *ls, *rs;
abcd status;
segtree() :ls(0x0), rs(0x0) {}
#define push_up(); status = merge(a[mid],ls->status,rs->status);
void Build_Tree(int x, int y)
{
if (x == y) return;
int mid = (x + y) >> 1;
ls = new segtree, rs = new segtree;
ls->Build_Tree(x, mid);
rs->Build_Tree(mid + 1, y);
push_up();
}
void refresh(int x, int y, int pos)
{
int mid = (x + y) >> 1;
if (pos == mid)
{
push_up();
return;
}
if (pos < mid)
ls->refresh(x, mid, pos);
else
rs->refresh(mid + 1, y, pos);
push_up();
}
void modify(int x, int y, int pos, int flag)
{
int mid = (x + y) >> 1;
if (x == y)
{
new(&status) abcd(flag);
return;
}
if (pos <= mid)
ls->modify(x, mid, pos, flag);
else
rs->modify(mid + 1, y, pos, flag);
push_up();
}
void _get_left(int x, int y, int &pos, abcd &sta, int flag)
{
int mid = (x + y) >> 1;
if (x == y) return;
abcd temp = merge(a[y], rs->status, sta);
if (temp[0][flag] || temp[1][flag])
pos = mid + 1, sta = temp, ls->_get_left(x, mid, pos, sta, flag);
else
rs->_get_left(mid + 1, y, pos, sta, flag);
}
void get_left(int x, int y, int &pos, abcd &sta, int flag)
{
if (x == y) return;
int mid = (x + y) >> 1;
if (pos <= mid)
ls->get_left(x, mid, pos, sta, flag);
else
{
//pos > mid
rs->get_left(mid + 1, y, pos, sta, flag);
if (pos != mid + 1) return;
//pos==mid+1;
abcd temp = merge(a[mid], ls->status, sta);
if (temp[0][flag] || temp[1][flag])
pos = x, sta = temp;
else
ls->_get_left(x, mid, pos, sta, flag);
}
}
void _get_right(int x, int y, int &pos, abcd &sta, int flag)
{
int mid = (x + y) >> 1;
if (x == y) return;
abcd temp = merge(a[x - 1], sta, ls->status);
if (temp[flag][0] || temp[flag][1])
pos = mid, sta = temp, rs->_get_right(mid + 1, y, pos, sta, flag);
else
ls->_get_right(x, mid, pos, sta, flag);
}
void get_right(int x, int y, int &pos, abcd &sta, int flag)
{
if (x == y) return;
int mid = (x + y) >> 1;
if (mid < pos)
rs->get_right(mid + 1, y, pos, sta, flag);
else
{
//pos <= mid
ls->get_right(x, mid, pos, sta, flag);
if (pos != mid) return;
//pos==mid
abcd temp = merge(a[mid], sta, rs->status);
if (temp[flag][0] || temp[flag][1])
pos = y, sta = temp;
else
rs->_get_right(mid + 1, y, pos, sta, flag);
}
}
abcd get_ans(int x, int y, int l, int r)
{
if (x == l && y == r)
return status;
int mid = (x + y) >> 1;
if (mid < l)
return rs->get_ans(mid + 1, y, l, r);
if (r <= mid)
return ls->get_ans(x, mid, l, r);
return merge(a[mid], ls->get_ans(x, mid, l, mid), rs->get_ans(mid + 1, y, mid + 1, r));
}
} *tree = new segtree;
void modify(int x1, int y1, int x2, int y2, bool flag)
{
if (x1 == x2)
{
if (y1 > y2) swap(y1, y2);
a[y1][x1 - 1] = flag;
tree->refresh(1, n, y1);
return;
}
//y1 == y2
tree->modify(1, n, y1, flag);
}
void query(int x1, int y1, int x2, int y2)
{
if (y1 > y2)
{
swap(x1, x2);
swap(y1, y2);
}
abcd temp(false);
tree->get_left(1, n, y1, temp, x1 - 1);
x1 = temp[0][x1 - 1] ? 1 : 2;
abcd temp1(false);
tree->get_right(1, n, y2, temp1, x2 - 1);
x2 = temp1[x2 - 1][0] ? 1 : 2;
abcd temp2 = tree->get_ans(1, n, y1, y2);
puts(temp2[x1 - 1][x2 - 1] ? "Y" : "N");
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n);
tree->Build_Tree(1, n);
int x1, y1, x2, y2;
char p[10];
while (1)
{
scanf("%s", p);
rei(x1), rei(y1), rei(x2), rei(y2);
if (p[0] == 'C')
modify(x1, y1, x2, y2, false);
if (p[0] == 'O')
modify(x1, y1, x2, y2, true);
if (p[0] == 'A')
query(x1, y1, x2, y2);
if (p[0] == 'E')
break;
}
return 0;
}
【BZOJ 1018】 [SHOI2008]堵塞的交通traffic的更多相关文章
- 数据结构(线段树):BZOJ 1018: [SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2638 Solved: 864 Descri ...
- BZOJ 1018 [SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2247 Solved: 706[Submit ...
- BZOJ 1018: [SHOI2008]堵塞的交通traffic [线段树 区间信息]
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 3064 Solved: 1027[Submi ...
- [BZOJ 1018] [SHOI2008] 堵塞的交通traffic 【线段树维护联通性】
题目链接:BZOJ - 1018 题目分析 这道题就说明了刷题少,比赛就容易跪..SDOI Round1 Day2 T3 就是与这道题类似的..然而我并没有做过这道题.. 这道题是线段树维护联通性的经 ...
- BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 用线段树维护区间连通性,对于每一个区间记录6个域表示(左上,左下)(左上,右上)(右上, ...
- BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树分治+并查集)
传送门 解题思路 可以离线,然后确定每个边的出现时间,算这个排序即可.然后就可以线段树分治了,连通性用并查集维护,因为要撤销,所以要按秩合并,时间复杂度\(O(nlog^2 n)\) 代码 #incl ...
- 1018: [SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic 链接 分析: 用线段树维护区间的四个端点的联通情况,然后查询的时候,把所有覆盖到的区间合并起来即可. 六种情况左上到右上(左边到右边的情况)… ...
- 1018: [SHOI2008]堵塞的交通traffic - BZOJ
Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一 ...
- 【BZOJ】1018: [SHOI2008]堵塞的交通traffic
http://www.lydsy.com/JudgeOnline/problem.php?id=1018 题意:有2行,每行有c(c<=100000)个城市,则一共有c-1个格子,现在有q(q& ...
- BZOJ.1018.[SHOI2008]堵塞的交通(线段树维护连通性)
题目链接 只有两行,可能的路径数不多,考虑用线段树维护各种路径的连通性. 每个节点记录luru(left_up->right_up),lurd,ldru,ldrd,luld,rurd,表示这个区 ...
随机推荐
- sql server备份与还原 sql语句
USE master DECLARE tb CURSOR LOCAL FOR SELECT 'Kill '+ CAST(Spid AS VARCHAR) FROM master.dbo.sysproc ...
- 00089_字节输出流OutputStream
1.字节输出流OutputStream (1)OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法: (2)输出流中定义都是写wri ...
- 芯片AMS1117 电源芯片
- <九度 OJ>题目1012:畅通project
题目描写叙述: 某省调查城镇交通状况,得到现有城镇道路统计表.表中列出了每条道路直接连通的城镇.省政府"畅通project"的目标是使全省不论什么两个城镇间都能够实现交通(但不一定 ...
- C++ Tricks(一)—— 判断字符串 string 对象的所有字符都相等
S == string(S.size(), S[0]);
- P2P平台的"我要借款"功能,是否需要上传借款人的相关资料
P2P平台的前端系统,一般都会有"我要借款"这个功能.有的平台,非常重视这个功能, 把它作为主要菜单的其中一项.有的把它看得相对次要,放在顶部Top栏中. 毕竟P2P平台,其实主 ...
- UIBarButtonItem使用
1 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBar ...
- go 保留小数若干位数
感谢 https://blog.csdn.net/sjy8207380/article/details/79013827 解决的方法 · 利用取近似值的方法解决这个问题. (1)利用fmt.Sprin ...
- vue-cli3使用vue-svg-loader加载svg
vue-svg-loader Documentation - FAQ webpack loader that lets you use SVG files as Vue components Micr ...
- UILabel调整字间距
1.引入 在文件导入 #import <CoreText/CoreText.h> 2.程序 NSMutableAttributedString *attributedString =[[N ...