首页 技术 正文
技术 2022年11月14日
0 收藏 759 点赞 4,085 浏览 2578 个字

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  3Sample 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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906