C. Not Equal on a Segment(思维+区间找数字不同的下标)

https://codeforces.com/problemset/problem/622/C


思路:

开始想着离线莫队干过去。

思维点的找特殊的最大值和最小值,如果两个一样说明数只有一个,必不可能,不然取其中一个就好。然后就上线段树

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
typedef pair<LL,LL>P;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct Tree{
   LL l,r,maxval,maxpos,minval,minpos;
}tree[maxn*4];
LL a[maxn];
void push_up(LL p){
    if(tree[p].maxval<tree[p*2].maxval) tree[p].maxval=tree[p*2].maxval,tree[p].maxpos=tree[p*2].maxpos;
    if(tree[p].maxval<tree[p*2+1].maxval) tree[p].maxval=tree[p*2+1].maxval,tree[p].maxpos=tree[p*2+1].maxpos;
    if(tree[p].minval>tree[p*2].minval) tree[p].minval=tree[p*2].minval,tree[p].minpos=tree[p*2].minpos;
    if(tree[p].minval>tree[p*2+1].minval) tree[p].minval=tree[p*2+1].minval,tree[p].minpos=tree[p*2+1].minpos;
}
void build(LL p,LL l,LL r){
    tree[p].l=l;tree[p].r=r;tree[p].maxpos=-1;tree[p].maxval=-1;tree[p].maxval=-1;tree[p].minval=0x3f3f3f3f;
    if(l==r){tree[p].maxval=tree[p].minval=a[l];tree[p].minpos=tree[p].maxpos=l;return;}
    LL mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    push_up(p);
}
P mquery(LL p,LL l,LL r){
  if(l<=tree[p].l&&r>=tree[p].r){
     P A={tree[p].maxval,tree[p].maxpos};
     return A;
  }
  LL mid=(tree[p].l+tree[p].r)>>1;
  P ans={0,0};
  if(l<=mid){
    P temp=mquery(p*2,l,r);
    if(ans.first<temp.first) ans=temp;
  }
  if(r>mid){
    P temp=mquery(p*2+1,l,r);
    if(ans.first<temp.first) ans=temp;
  }
  return ans;
}
P squery(LL p,LL l,LL r){
  if(l<=tree[p].l&&r>=tree[p].r){
    P A={tree[p].minval,tree[p].minpos};
    return A;
  }
  LL mid=(tree[p].l+tree[p].r)>>1;
  P ans={0x3f3f3f3f,0x3f3f3f3f};
  if(l<=mid){
    P temp=squery(p*2,l,r);
    if(temp.first<ans.first) ans=temp;
  }
  if(r>mid){
    P temp=squery(p*2+1,l,r);
    if(temp.first<ans.first) ans=temp;
  }
  return ans;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,m;cin>>n>>m;
  for(LL i=1;i<=n;i++) cin>>a[i];
  build(1,1,n);
  while(m--){
    LL l,r,x;cin>>l>>r>>x;
    P ans1=mquery(1,l,r);
    if(ans1.first!=x){
        cout<<ans1.second<<"\n";
        continue;
    }
    P ans2=squery(1,l,r);
    if(ans2.first!=x){
        cout<<ans2.second<<"\n";
        continue;
    }
    cout<<"-1"<<"\n";
  }
return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值