ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#199726 | #484. interval | UperFicial | 100 | 1086ms | 14664kb | C++11 | 2.4kb | 2023-12-19 10:24:34 | 2023-12-19 12:50:25 |
answer
#include <bits/stdc++.h>
#define Pair pair<int,int>
#define mp make_pair
#define ls (p<<1)
#define rs (p<<1|1)
#define mid ((l+r)>>1)
using namespace std;
template <typename T>
inline T read(){
T x=0;char ch=getchar();bool fl=false;
while(!isdigit(ch)){if(ch=='-')fl=true;ch=getchar();}
while(isdigit(ch)){
x=(x<<3)+(x<<1)+(ch^48);ch=getchar();
}
return fl?-x:x;
}
#define read() read<int>()
const int maxn = 1e5 + 10;
namespace Tree{
struct tree{
int l,r;
int mn,tag;
}t[maxn<<2];
inline void pushup(int p){
t[p].mn=min(t[ls].mn,t[rs].mn);
}
inline void pushdown(int p){
if(!t[p].tag)return ;
t[ls].mn+=t[p].tag;t[rs].mn+=t[p].tag;
t[ls].tag+=t[p].tag;t[rs].tag+=t[p].tag;
t[p].tag=0;
}
void build(int p,int l,int r){
t[p].l=l;t[p].r=r;
if(l==r)return t[p].mn=l,void();
build(ls,l,mid);build(rs,mid+1,r);
pushup(p);
}
void update(int p,int x,int y,int val){
int l=t[p].l,r=t[p].r;
if(x>y)return ;
if(x<=l && r<=y){
t[p].mn+=val;t[p].tag+=val;return ;
}
pushdown(p);
if(x<=mid)update(ls,x,y,val);
if(y>mid)update(rs,x,y,val);
pushup(p);
}
int query(int p,int x,int y){
int l=t[p].l,r=t[p].r;
if(t[p].mn>0)return -1;
if(l==r)return l;
pushdown(p);
if(y>mid && !t[rs].mn){
int res=query(rs,x,y);
if(res!=-1)return res;
}
if(x<=mid && !t[ls].mn)return query(ls,x,y);
return -1;
}
}using namespace Tree;
int n,m,a[maxn];
int stk1[maxn],stk2[maxn],top1,top2;
vector<Pair> pos[maxn];
multiset<Pair> s;
Pair ans[maxn];
int main(){
n=read();
for(int i=1;i<=n;i++)a[i]=read();
m=read();
for(int i=1;i<=m;i++){
int l=read(),r=read();
pos[r].emplace_back(l,i);
}
build(1,1,n);
for(int i=1;i<=n;i++){
int x,y;
while(top1 && a[stk1[top1]]<a[i]){
y=stk1[top1];top1--;
x=stk1[top1]+1;
update(1,x,y,-a[y]);
}
x=stk1[top1]+1;y=i;
update(1,x,y,a[y]);
stk1[++top1]=i;
while(top2 && a[stk2[top2]]>a[i]){
y=stk2[top2];top2--;
x=stk2[top2]+1;
update(1,x,y,a[y]);
}
x=stk2[top2]+1;y=i;
update(1,x,y,-a[y]);
stk2[++top2]=i;
update(1,1,n,-1);
for(auto x:pos[i])s.insert(x);
while(s.size()){
auto it=s.end();it--;
int l=it->first,res=query(1,1,l);
if(res==-1)break;
s.erase(it);
ans[it->second].first=res;
ans[it->second].second=i;
}
}
for(int i=1;i<=m;i++)printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 3ms
memory: 4416kb
input:
1000 897 899 896 900 898 879 877 878 880 876 887 889 886 890 888 885 884 883 882 881 892 894 891 893...
output:
1 1000 126 1000 126 1000 126 1000 1 1000 126 1000 126 1000 1 1000 401 1000 126 1000 126 1000 126 100...
result:
ok 1000 lines
Test #2:
score: 10
Accepted
time: 0ms
memory: 4400kb
input:
1000 985 986 987 984 982 983 989 990 988 996 994 995 991 993 992 999 998 997 974 975 973 978 976 977...
output:
353 757 1 757 272 1000 29 757 353 757 353 757 29 1000 272 1000 272 757 29 757 272 1000 29 757 272 10...
result:
ok 1000 lines
Test #3:
score: 10
Accepted
time: 3ms
memory: 4396kb
input:
1000 196 197 199 200 198 178 179 177 176 180 181 185 184 183 182 188 186 189 187 190 193 195 194 191...
output:
1 875 1 625 1 875 1 875 251 875 1 625 1 625 376 600 376 875 251 875 251 875 1 625 376 875 1 625 251 ...
result:
ok 1000 lines
Test #4:
score: 10
Accepted
time: 136ms
memory: 13372kb
input:
100000 1 3 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 23 20 25 22 27 24 29 26 31 28 33 30 35 32 ...
output:
1 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2...
result:
ok 99999 lines
Test #5:
score: 10
Accepted
time: 150ms
memory: 12176kb
input:
100000 1 3 5 2 7 4 9 6 11 8 13 10 15 12 442 444 443 446 445 447 439 441 440 422 421 423 429 427 428 ...
output:
1 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2...
result:
ok 99999 lines
Test #6:
score: 10
Accepted
time: 132ms
memory: 12024kb
input:
100000 1 3 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 155 156 154 151 153 152 149 148 150 135 13...
output:
1 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2...
result:
ok 99999 lines
Test #7:
score: 10
Accepted
time: 152ms
memory: 11944kb
input:
100000 98727 98726 98728 98729 98730 98749 98747 98746 98748 98750 98736 98737 98739 98738 98740 987...
output:
1 2 1 3 3 4 4 5 5 25 6 9 7 8 7 9 6 10 6 20 11 12 12 14 13 14 13 15 15 16 16 17 17 18 18 19 19 20 11 ...
result:
ok 99999 lines
Test #8:
score: 10
Accepted
time: 165ms
memory: 12336kb
input:
100000 44888 44889 44887 44891 44890 44892 44885 44884 44886 44899 44900 44901 44895 44893 44894 448...
output:
1 100000 1 100000 1 100000 1 100000 1 100000 32806 59049 1 100000 1 100000 1 100000 1 100000 1 10000...
result:
ok 100000 lines
Test #9:
score: 10
Accepted
time: 169ms
memory: 14352kb
input:
100000 1 3 5 2 7 4 9 6 11 8 12 14 16 13 18 15 20 17 22 19 24 21 26 23 28 25 30 27 32 29 34 31 33 36 ...
output:
2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2...
result:
ok 100000 lines
Test #10:
score: 10
Accepted
time: 176ms
memory: 14664kb
input:
100000 1 3 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 23 20 25 22 27 24 29 26 31 28 33 30 35 32 ...
output:
2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2 100000 2...
result:
ok 100000 lines