hrbeu基础题解(高级数据结构之树状数组)

题目地址:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1001&cid=51

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define INF 0xfffffff
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
#define N 100001
int c[N],ma[N];
int lowbit(int t)
{
        return t&(-t);
} 
void insert(int k,int d,int max)
{
        while(k<=max)
        {
                c[k]+=d;
                k+=lowbit(k);
        }
}
int getsum(int k)
{
        int t=0;
        while(k>0)     //必须k>0,不能k为真。 
        {
                t+=c[k];
                k-=lowbit(k);
        }
        return t;
}
void swap(int &a,int &b)
{
        int t=a;
        a=b;
        b=t;
}
int main()
{
        int i,j,k,t;
        int m,n;
        while(cin>>n>>k)
         {
                memset(c,0,sizeof(c));
                for(i=1;i<=n;i++)
                {
                 insert(i,1,n);
                 ma[i]=1;       
                }
                while(k--)
                 {
                  int a,b,f;
                   cin>>f>>a>>b;
                   if(f==0)
                   {
                        if(a>b) swap(a,b);
                        if(b==n&&a==1) 
                        {
                                if(ma[n]==1)
                                {
                                        c[n]--;
                                        ma[n]=0;
                                }
                                else 
                                {
                                        c[n]++;
                                        ma[n]=1;
                                }
                        }
                        else 
                        {
                            if(ma[a]==1)
                            {
                                insert(a,-1,n);
                                ma[a]=0;
                            }
                            else 
                            {
                                insert(a,1,n);
                                ma[a]=1;                                
                            }
                        }
                   }
                  else 
                  {
                    if(a>b) swap(a,b);
                    int t1,t2,t3,t4;
                    t1=getsum(a-1);
                    t2=getsum(b-1);
                    int flag=0;
                    if(t2-t1==b-a)
                     {
                     flag=1;
                     cout<<"YES"<<endl;
                     }
                     if(!flag)
                     {
                        t3=getsum(n)-t2;
                        if(t3+t1==n-b+a)
                        {
                         flag=1;
                         cout<<"YES"<<endl;
                         }
                         else cout<<"NO"<<endl;
  					 }
        			}       
                 }//while
        }//while       
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值