您的位置:首页 > 理论基础 > 计算机网络

http://acm.hrbeu.edu.cn/index.php?act=problem&id=1001&cid=19 人工湖的公路

2012-05-26 20:45 387 查看
#include<iostream>
#define MAX 100000
using namespace std;
long A[MAX+1];//环形公路数据
long com[MAX+1];//树状数组
long N,M;//节点数和询问次数

long lowbit(long x)
{
return x&(-x);
}

void modify(long pos,int value)
{
while(pos<=N)
{
com[pos]+=value;
pos+=lowbit(pos);
}
}

long query(long x)
{
long sum=0;
while(x>0)
{
sum+=com[x];
x-=lowbit(x);
}
return sum;
}

void init()
{
for(long i=1;i<=N;i++)
{
com[i]=0;
}
for(long i=1;i<=N;i++)
{
modify(i,1);
}
}

void solve(long x,long y)
{
long sum1,sum2;
sum1=query(x);
sum2=query(y);
if(sum2-sum1==y-x)
{
cout<<"YES"<<endl;
return;
}
long sum3;
sum1=query(x);
sum2=query(y);
sum3=query(N);
sum2=sum3-sum2;
if(sum1+sum2==x+N-y)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}

int main()
{
while(cin>>N>>M)
{
init();
for(long i=1;i<=N;i++)
{
A[i]=1;
}
int f;
long x,y;
for(long i=0;i<M;i++)
{
cin>>f>>x>>y;
if(x>y)
{
long temp;
x=temp;
x=y;
y=temp;
}
if(f==0)
{
if(x==1&&y==N)
{
if(A[y]==1)
{
modify(x,-1);
A[x]=0;
}
else
{
modify(x,1);
A[x]=1;
}
}
else
{
if(A[y]==1)
{
modify(y,-1);
A[y]=0;
}
else
{
modify(y,1);
A[y]=1;
}
}
}
else
{
solve(x,y);
}
}
}
return 0;
}


#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐