数据结构(线段树):HDU 5649 DZY Loves Sorting

时间:2023-03-09 00:16:51
数据结构(线段树):HDU 5649 DZY Loves Sorting

DZY Loves Sorting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 294    Accepted Submission(s): 77

Problem Description
  DZY has a sequence a[1..n]. It is a permutation of integers 1∼n.
  Now he wants to perform two types of operations:
    0 l r: Sort a[l..r] in increasing order.
    1 l r: Sort a[l..r] in decreasing order.
  After doing all the operations, he will tell you a position k, and ask you the value of a[k].
Input
  First line contains t, denoting the number of testcases.
  t testcases follow. For each testcase:
  First line contains n,m. m is the number of operations.
  Second line contains n space-separated integers a[1],a[2],⋯,a[n], the initial sequence. We ensure that it is a permutation of 1∼n.
  Then m lines follow. In each line there are three integers opt,l,r to indicate an operation.
  Last line contains k.
  (1≤t≤50,1≤n,m≤100000,1≤k≤n,1≤l≤r≤n,opt∈{0,1}. Sum of n in all testcases does not exceed 150000. Sum of m in all testcases does not exceed 150000)
Output
  For each testcase, output one line - the value of a[k] after performing all m operations.
Sample Input
  1
  6 3
  1 6 2 5 3 4
  0 1 4
  1 3 6
  0 2 4
  3
Sample Output
  5
Hint

1 6 2 5 3 4 -> [1 2 5 6] 3 4 -> 1 2 [6 5 4 3] -> 1 [2 5 6] 4 3. At last a[3]=5.

  好题,考虑二分的话,维护的只是相对大小,题目就好做了。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int n,m,k;
int tr[maxn<<],mark[maxn<<];
int L[maxn],R[maxn],X[maxn],a[maxn]; void Make_same(int x,int l,int r,int d){
tr[x]=(r-l+)*d;
mark[x]=d;
} void Push_down(int x,int l,int r){
if(mark[x]!=-){
int mid=(l+r)>>;
Make_same(x<<,l,mid,mark[x]);
Make_same(x<<|,mid+,r,mark[x]);
mark[x]=-;
}
} void Build(int x,int l,int r,int g){
mark[x]=-;
if(l==r){
tr[x]=a[l]<=g?:;
return;
}
int mid=(l+r)>>;
Build(x<<,l,mid,g);
Build(x<<|,mid+,r,g);
tr[x]=tr[x<<]+tr[x<<|];
} int Query(int x,int l,int r,int a,int b){
Push_down(x,l,r);
if(l>=a&&r<=b)return tr[x];
int mid=(l+r)>>,ret=;
if(mid>=a)ret=Query(x<<,l,mid,a,b);
if(mid<b)ret+=Query(x<<|,mid+,r,a,b);
return ret;
} void Mark(int x,int l,int r,int a,int b,int d){
if(a>b)return;
if(l>=a&&r<=b){
Make_same(x,l,r,d);
return;
}
Push_down(x,l,r);
int mid=(l+r)>>;
if(mid>=a)Mark(x<<,l,mid,a,b,d);
if(mid<b)Mark(x<<|,mid+,r,a,b,d);
tr[x]=tr[x<<]+tr[x<<|];
} bool Check(){
for(int i=;i<=m;i++){
int sum=Query(,,n,L[i],R[i]);
if(X[i]){
Mark(,,n,L[i],L[i]+sum-,);
Mark(,,n,L[i]+sum,R[i],);
}
else{
sum=R[i]-L[i]+-sum;
Mark(,,n,L[i],L[i]+sum-,);
Mark(,,n,L[i]+sum,R[i],);
}
}
return Query(,,n,k,k);
} void Solve(){
int lo=,hi=n;
while(lo<=hi){
int mid=(lo+hi)>>;
Build(,,n,mid);
if(Check())lo=mid+;
else hi=mid-;
}
printf("%d\n",lo);
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=m;i++)scanf("%d%d%d",&X[i],&L[i],&R[i]);
scanf("%d",&k);
Solve();
}
return ;
}