QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#307730 | #8016. 不休陀螺 | Jryno1 | 0 | 338ms | 89076kb | C++14 | 3.3kb | 2024-01-19 07:55:29 | 2024-01-19 07:55:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define Root T.rt,-inf,inf
const int maxn=5e6+10,inf=1e16;
int n,E;
struct node{
int a,b,op;
}nd[maxn];
void sub1(){
int ans=0;
for(int l=1;l<=n;l++){
int Emin=0,maxsub=0,presub=0,sigget=0,siglost=0;
for(int i=l;i<=n;i++){
int nowsub=presub,nowmin=presub+nd[i].a;
if(!nd[i].op)Emin+=nd[i].a-nd[i].b,maxsub+=nd[i].a-nd[i].b,presub+=nd[i].a-nd[i].b;
if(nowmin>Emin)Emin=nowmin,maxsub=nowsub;
if(Emin>E)break;
sigget+=nd[i].b,siglost+=nd[i].a;
if(sigget>=siglost)ans++;
}
}
cout<<ans<<"\n";
}
struct SGT{
struct trnode{
int ls,rs,val;
}tr[maxn<<2];
int tnod,rt;
int newnod(){
tnod++;
tr[tnod].ls=0,tr[tnod].rs=0,tr[tnod].val=0;
return tnod;
}
void PU(int x){
tr[x].val=0;
if(tr[x].ls)tr[x].val+=tr[tr[x].ls].val;
if(tr[x].rs)tr[x].val+=tr[tr[x].rs].val;
}
void ins(int &x,int l,int r,int pos,int cv){
if(!x)x=newnod();
if(l==r){
tr[x].val+=cv;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)ins(tr[x].ls,l,mid,pos,cv);
else ins(tr[x].rs,mid+1,r,pos,cv);
PU(x);
}
int QT(int x,int l,int r,int L,int R){
if(!x)return 0;
if(L<=l&&R>=r)return tr[x].val;
int mid=(l+r)>>1,res=0;
if(L<=mid)res+=QT(tr[x].ls,l,mid,L,R);
if(R>mid)res+=QT(tr[x].rs,mid+1,r,L,R);
return res;
}
}T;
int flag=0,v[maxn],pv[maxn],predelt[maxn],d[maxn];
int Q[maxn],lq=1,rq=0;
long long ans=0;
void suball(){
for(int i=1;i<=n;i++){
predelt[i]=predelt[i-1]+nd[i].b-nd[i].a;
// cout<<predelt[i]<<" ";
if(nd[i].a>nd[i].b)v[i]=nd[i].a-nd[i].b;
else v[i]=0;
d[i]=nd[i].a-v[i];
}
// cout<<endl;
for(int i=1;i<=n;i++)pv[i]=pv[i-1]+v[i];
for(int L=1,R=0;L<=n;L++){
if(nd[L].a>E){
R=L,lq=1,rq=0;
flag+=nd[L].b-nd[L].a;
continue;
}
while(R<n&&(lq>rq||pv[R+1]-pv[L-1]+max(d[R+1],d[Q[lq]])<=E)){
R++;
while(lq<=rq&&d[R]>d[Q[rq]])rq--;
Q[++rq]=R;
T.ins(Root,predelt[R],1);
}
// cout<<L<<" "<<R<<endl;
ans+=1ll*T.QT(Root,flag,inf);
// cout<<ans<<endl;
T.ins(Root,predelt[L],-1);
flag+=nd[L].b-nd[L].a;
if(Q[lq]==L)lq++;
}
cout<<ans<<endl;
}
signed main(){
// freopen("top.in","r",stdin);
// freopen("top.out","w",stdout);
cin.tie(0),cout.tie(0),ios::sync_with_stdio(false);
cin>>n>>E;
for(int i=1;i<=n;i++)cin>>nd[i].a;
for(int i=1;i<=n;i++)cin>>nd[i].b,nd[i].op=(nd[i].a<nd[i].b);
if(n<=5000)sub1();
else suball();
return 0;
}
/*
10 2
4 7 10 0 4 7 1 1 0 6
4 6 6 2 1 3 7 10 8 6
*/
/*
When can [l,r] be a possible solution?
two main factors:
1.$\sum a<=\sum b$ because if it isn't then it will keep getting smaller
2.we go through all the nodes in [l,r],see if it can "die" at this one:
worse case is that if everything before this node makes E smaller,and then it has to face this a_i
if none node can "die" then it is a possible solution
lets make v_i=(a_i-b_i)*[a_i>b_i]
then this transfer to $Emin=\max (\sum v_i)-v_i+a_i$
the interesting thing is -v_i+a_i dosn't really matter with [l,r]
and what is more interesting is when L incrases,R increase as well,and if [l,r] is bad then [l,r+x] is bad as well
this is two-pointer
Going back to the first limitation, we need to find given [L,R] how many [L,r<=R] can be used
Since L++ everytime,we can use range-based segement tree for this process by maintaining a flag and \sum a-\sum b.
O(nlogn)
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 5732kb
input:
5000 939255322 47952340 92329911 61615795 40122788 47258178 29326499 9822850 42767362 86610596 60318756 52429688 87502511 50194916 96377063 74322128 19511341 28794957 53813791 79075058 35555414 5249682 45174421 101856091 25257909 94697470 45853817 82945426 108415825 41731145 87133877 75167193 598696...
output:
1843187
result:
wrong answer 1st lines differ - expected: '1846283', found: '1843187'
Subtask #2:
score: 0
Memory Limit Exceeded
Test #5:
score: 0
Memory Limit Exceeded
input:
774484 763692678 47702350 34856775 28447988 4178162 45063720 8232662 36845607 27038945 44858289 5952529 39159657 21628528 60199611 5544054 59216841 39287087 43449994 20034684 56440004 11583811 44465341 32347476 49196492 22731571 9481143 11726859 35167370 23103544 23109378 38822668 29778048 58004104 ...
output:
result:
Subtask #3:
score: 0
Memory Limit Exceeded
Test #10:
score: 0
Memory Limit Exceeded
input:
1000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
result:
Subtask #4:
score: 0
Wrong Answer
Test #14:
score: 0
Wrong Answer
time: 66ms
memory: 19332kb
input:
174457 888 0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 1...
output:
85730493
result:
wrong answer 1st lines differ - expected: '329807918', found: '85730493'
Subtask #5:
score: 0
Memory Limit Exceeded
Test #18:
score: 0
Memory Limit Exceeded
input:
343922 773619774 0 8292680 5684115 0 0 170056 5385926 0 0 1588575 0 0 10947891 170867 35145 0 0 103085 7231562 0 0 0 0 11128944 0 4872226 0 2879880 7565181 0 8631665 0 5162564 9511835 514165 0 9628987 14357934 174784 0 12400154 0 0 8198218 0 8496060 0 0 0 0 10376826 3523227 0 14548249 0 6840016 0 0 ...
output:
result:
Subtask #6:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 338ms
memory: 89076kb
input:
468676 582048177 6889433 7293342 20676061 15545414 4911497 12352219 8921719 1705801 19695926 25259227 2645394 17518171 19753552 9449377 982708 22479531 1267985 15594372 20685422 9627290 2017543 6459134 18614020 16206301 14962487 12932255 7101003 29140540 6479702 20607124 2540287 15565156 20274141 11...
output:
74304663
result:
wrong answer 1st lines differ - expected: '353280708', found: '74304663'