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;
}