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; }
相关文章推荐
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=791&pid=1001&ojid=0
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=963&pid=1019&ojid=1
- http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2500&cid=1122
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1004&ojid=1&cid=6866&hide=1&problem=Pro
- SDUT 选夫婿1 http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1569&cid=1211
- http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2504&cid=1122
- http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2502&cid=1122
- 火影忍者之~鸣人http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1310
- http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2467&cid=1131
- http://acm.hdu.edu.cn/showproblem.php?pid=3342&&拓扑入门题
- nefu 118 质数在阶乘中的幂 http://acm.nefu.edu.cn/test/problemshow.php?problem_id=118
- http://acm.hdu.edu.cn/showproblem.php?pid=2222&&AC自动机
- A C&&http://acm.hdu.edu.cn/showproblem.php?pid=1570
- http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?&&次小生成树
- Happy 2004&&http://acm.hdu.edu.cn/showproblem.php?pid=1452
- http://acm.hdu.edu.cn/showproblem.php?pid=1269&&迷宫城堡
- http://acm.hdu.edu.cn/showproblem.php?pid=2844&&背包问题
- Clairewd’s message&&http://acm.hdu.edu.cn/showproblem.php?pid=4300
- http://acm.hdu.edu.cn/showproblem.php?pid=2199&&二分查找求高次方程的根
- nefu 118 质数在阶乘中的幂 http://acm.nefu.edu.cn/test/problemshow.php?problem_id=118