QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#78506 | #5278. Mex and Cards | Yanagi_Origami | TL | 42ms | 5860kb | C++14 | 2.4kb | 2023-02-19 10:50:56 | 2023-02-19 10:51:00 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cmath>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,a[N];
struct node{
int Min,l,r;
ll sum;
}seg[N<<2];
int query(int p,int l,int r,int L,int x){
if(l==r){
return seg[p].Min<=x?l:(n+1);
}
int mid=(l+r)>>1;
if(r<L) return n+1;
if(seg[p].Min>x) return n+1;
return min(query(p<<1,l,mid,L,x),query(p<<1|1,mid+1,r,L,x));
}
ll calc(int l,int r,int w){
return 1ll*(r-l+1)*w;
}
node Query(int p,int l,int r,int L,int R);
node upd(node ls,node rs){
node res; res.l=ls.l, res.r=rs.r;
//printf("## l :%d %d %d %lld\n",ls.l,ls.r,ls.Min,ls.sum);
//printf("## r :%d %d %d %lld\n",rs.l,rs.r,rs.Min,rs.sum);
res.Min=min(ls.Min,rs.Min);
if(ls.Min>=a[rs.l]){
res.sum=ls.sum+rs.sum;
return res;
}
if(ls.Min<rs.Min){
res.sum=ls.sum+calc(rs.l,rs.r,ls.Min);
return res;
}
int ps=query(1,1,n,rs.l,ls.Min);
int rsmid=rs.l+rs.r>>1;
if(ps>rsmid){
node val=Query(1,1,n,ps,rs.r);
res.sum=ls.sum+calc(rs.l,ps-1,ls.Min)+val.sum;
}else{
node val=Query(1,1,n,rs.l,ps-1);
res.sum=ls.sum+calc(rs.l,ps-1,ls.Min)-val.sum+rs.sum;
}
//printf("## s :%d %d %d %lld\n",res.l,res.r,res.Min,res.sum);
return res;
}
node Query(int p,int l,int r,int L,int R){
if(L==l&&r==R){
return seg[p];
}
int mid=(l+r)>>1;
if(R<=mid) return Query(p<<1,l,mid,L,R);
if(L>mid) return Query(p<<1|1,mid+1,r,L,R);
node res=upd(Query(p<<1,l,mid,L,mid),Query(p<<1|1,mid+1,r,mid+1,R));
return res;
}
void upd(int p){
seg[p]=upd(seg[p<<1],seg[p<<1|1]);
}
void build(int p,int l,int r){
if(l==r){
seg[p].Min=a[l];
seg[p].sum=a[l];
seg[p].l=l; seg[p].r=r;
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
upd(p);
}
void modify(int p,int l,int r,int x){
if(l==r){
seg[p].Min=a[l];
seg[p].sum=a[l];
return ;
}
int mid=(l+r)>>1;
if(x<=mid) modify(p<<1,l,mid,x);
else modify(p<<1|1,mid+1,r,x);
upd(p);
}
int main(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",a+i);
build(1,1,n);
printf("%lld\n",seg[1].sum);
int Q; scanf("%d",&Q);
while(Q--){
int opt,p;
scanf("%d%d",&opt,&p);
if(opt==1){
a[p+1]++;
modify(1,1,n,p+1);
}else{
a[p+1]--;
modify(1,1,n,p+1);
}
printf("%lld\n",seg[1].sum);
}
return 0;
}
/*
6
6 5 4 7 3 1
0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5860kb
input:
5 2 1 3 0 2 6 1 0 1 1 2 4 1 3 2 1 2 1
output:
4 5 7 7 9 7 3
result:
ok 7 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
1 0 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3812kb
input:
10 3 8 1 4 10 3 10 9 7 10 20 2 5 1 4 1 2 1 4 1 3 1 3 1 0 2 8 1 5 1 4 1 0 1 3 1 8 1 6 1 4 1 1 1 5 1 9 1 6 2 7
output:
14 14 14 22 22 22 22 24 24 24 24 26 26 26 26 26 26 26 26 26 26
result:
ok 21 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 3812kb
input:
10 9 8 7 5 5 4 3 2 1 1 20 2 4 1 8 2 6 1 2 1 2 2 5 2 2 1 0 1 6 1 6 2 9 1 2 2 7 2 8 2 3 1 9 1 7 1 4 2 6 1 7
output:
45 44 45 44 45 45 44 44 45 46 46 45 45 43 43 42 43 44 44 44 45
result:
ok 21 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 3652kb
input:
100 969 519 608 546 957 82 670 100 92 581 974 529 970 54 639 216 238 620 966 162 430 10 446 884 895 292 450 180 619 389 943 855 204 605 514 997 325 98 643 915 744 249 333 431 160 434 714 976 168 573 682 69 873 285 668 561 159 858 864 683 266 564 350 214 461 421 213 568 279 624 749 433 735 437 978 95...
output:
4923 4923 4923 4923 4923 4923 4923 4923 4923 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4927 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 4931 ...
result:
ok 1001 numbers
Test #6:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
100 994 983 981 980 962 959 959 950 933 887 877 869 841 828 809 807 803 793 789 778 773 772 768 767 765 765 759 757 749 747 742 738 730 724 681 675 656 638 627 626 615 612 593 592 545 543 540 534 531 529 525 520 518 515 512 510 500 494 472 472 432 415 414 411 399 375 350 328 315 307 292 261 258 246 ...
output:
51041 51042 51041 51042 51043 51042 51043 51042 51040 51038 51037 51038 51039 51038 51039 51040 51039 51040 51039 51038 51037 51036 51037 51038 51037 51038 51037 51036 51035 51036 51037 51035 51034 51035 51034 51035 51036 51037 51036 51037 51039 51038 51037 51036 51035 51034 51033 51032 51031 51032 ...
result:
ok 1001 numbers
Test #7:
score: 0
Accepted
time: 42ms
memory: 5736kb
input:
1000 549187 776775 748956 28001 575132 18015 112492 144497 206885 842190 403842 456113 424268 871411 648618 186832 693358 781526 443190 175126 586343 918652 923262 973941 509929 433837 392849 452585 497398 331180 118333 152788 959909 539943 747365 261855 819641 618091 801231 355664 285761 895793 398...
output:
6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317898 6317...
result:
ok 10001 numbers
Test #8:
score: 0
Accepted
time: 5ms
memory: 3636kb
input:
1000 999823 997756 997661 997567 995015 993681 993623 993621 993428 993163 990908 989370 987917 986483 984926 983477 982705 982330 982173 981725 981590 980871 980406 978064 976231 974657 969279 968447 968067 967307 966294 965934 965581 964707 964400 962842 962262 961936 961798 961105 958480 957851 9...
output:
515262961 515262960 515262959 515262958 515262959 515262960 515262961 515262960 515262961 515262962 515262963 515262964 515262963 515262964 515262963 515262964 515262965 515262964 515262965 515262966 515262965 515262966 515262967 515262966 515262967 515262968 515262969 515262968 515262967 515262966 ...
result:
ok 10001 numbers
Test #9:
score: -100
Time Limit Exceeded
input:
10000 810601 729711 139357 433916 959178 573779 86115 773421 634334 59653 613563 529054 644885 727194 44262 709529 1681 469658 158976 628846 642964 486044 969568 598670 753102 925097 495487 506784 238141 791563 750457 218880 115527 944709 664199 321209 984344 317419 568689 296228 50862 458459 766006...
output:
6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625728 6625...