hrebu OJ 数据结构1001 人工湖的公路

http://acm.hrbeu.edu.cn/index.php?act=problem&id=1001&cid=19

昨天一小盆友问我的,我看了下,觉得不怎么难,还蛮好玩的,今早给过了。

给你一条路(是个环),相邻的俩点之间的路可能断,也可以修复,问你俩点之间是否能连通。

环的处理的话比较好办,直接路段长度延长1,相当于第n---1这段路。

更新的时候注意下就好。

查询的时候,因为是个环,所以需要查两次。速度有点慢,囧。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)

using namespace std;

const int MAX = 100010;

struct Tnode{				// 一维线段树 
    int l,r;
    bool ok; 
    int len() { return r - l;}
    int mid() { return MID(l,r);}
    bool in(int ll,int rr) { return l >= ll && r <= rr; }
    void lr(int ll,int rr){ l = ll; r = rr;}
};
Tnode node[MAX<<2];
void Build(int t,int l,int r)
{
	node[t].lr(l,r);
	node[t].ok = true;
	if( node[t].len() == 1 )
		return ;
	int mid = MID(l,r);
	Build(L(t),l,mid);
	Build(R(t),mid,r);
}

void Updata(int t,int l,int r)
{
	if( node[t].in(l,r) )
	{
		node[t].ok ^= 1;
		return ;
	}
	if( node[t].len() == 1 ) return ;
	int mid = node[t].mid();
	if( l < mid ) Updata(L(t),l,r);
	if( r > mid ) Updata(R(t),l,r);
	
	node[t].ok = node[L(t)].ok & node[R(t)].ok;
}

bool Query(int t,int l,int r)
{
	if( l >= r ) return true;
	if( node[t].in(l,r) )	return node[t].ok;
	if( node[t].len() == 1 ) return 0;
	int mid = node[t].mid();
	bool ans = true;
	if( l < mid ) ans &= Query(L(t),l,r);
	if( r > mid ) ans &= Query(R(t),l,r);
	return ans;
}

int main()
{
	int n, m, x, y, op;
	
	while( ~scanf("%d%d", &n, &m) )
	{
		Build(1, 1, n+1);
		while( m-- )
		{
			scanf("%d%d%d", &op, &x, &y);
			
			if( x > y )
				swap(x, y);
			
			if( op == 0 )
			{
				if( y - x > 1 )
					Updata(1, y, y+1);
				else
					Updata(1, x, y);
			}
			else
			{
				bool a = Query(1, x, y) | (Query(1, 1, x) & Query(1, y, n+1));
				if( a )
					puts("YES");
				else
					puts("NO");
			}
		}
	}

return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值