QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#547850 | #8536. Lazy Cow | -Ofast# | 100 ✓ | 358ms | 32576kb | C++17 | 4.4kb | 2024-09-05 11:23:41 | 2024-09-05 11:23:41 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define fir first
#define sec second
#define int long long
using namespace std;
const int N=2e5+10,mod=1e9+7,maxv=1e6;
int D;
int qpow(int a,int b){
b--;if(b==-1)return 0;
int res=1;a%=mod;
while(b){
if(b&1)res=res*a%mod;
a=a*a%mod;b>>=1;
}return res;
}
pair <int,int> q[N];
namespace seg{
int tree[(maxv+10)<<4],tag[(maxv+10)<<4];
void pushdown(int rt,int left,int right){
int mid=(left+right)>>1;
if(tag[rt]){
tag[rt<<1]+=tag[rt];
tag[rt<<1|1]+=tag[rt];
tree[rt<<1]+=(tag[rt]*(mid-left+1));
tree[rt<<1|1]+=(tag[rt]*(right-mid));
tag[rt]=0;
}
}
void pushup(int rt){tree[rt]=tree[rt<<1]+tree[rt<<1|1];}
void update(int rt,int left,int right,int L,int R,int x){
if(left<right)pushdown(rt,left,right);
if(left>=L&&right<=R){
tag[rt]+=x;
tree[rt]+=x*(right-left+1);
return;
}int mid=(left+right)>>1;
if(L<=mid)update(rt<<1,left,mid,L,R,x);
if(mid<R)update(rt<<1|1,mid+1,right,L,R,x);
pushup(rt);
}
int query(int rt,int left,int right,int L,int R){
if(left<right)pushdown(rt,left,right);
if(left>=L&&right<=R)return tree[rt];
int mid=(left+right)>>1,res=0;
if(L<=mid)res+=query(rt<<1,left,mid,L,R);
if(mid<R)res+=query(rt<<1|1,mid+1,right,L,R);
return res;
}
}
struct node{
int l,r,s;
bool operator <(const node b)const{
if(r!=b.r)return r<b.r;
return l<b.l;
}
};
int ans=0;
set <node> S;
void modify(int x,int w){
w=w-seg::query(1,1,maxv,1,x);
//cout<<w<<endl;
if(w<=0)return;
//cout<<"YES!"<<endl;
auto it=S.lower_bound((node){0,x,0});auto tmp=it;
if(x!=tmp->r){
S.insert((node){tmp->l,x,tmp->s});
S.insert((node){x+1,tmp->r,tmp->s});
S.erase(tmp);
}
it=S.lower_bound((node){0,x,0});
auto pre=it;pre--;
//cout<<"FUCK!"<<endl;
//for(auto it:S){
// cout<<it.l<<" "<<it.r<<" "<<it.s<<endl;
//}
int Tmp=w;
while(w){
int Top=pre->s,sz=(it->r)-(it->l)+1,p=it->s;
if(w>=(Top-p)*sz){
w-=(Top-p)*sz;
ans-=qpow(3,p)*sz%mod;
ans+=qpow(3,Top)*sz%mod;
seg::update(1,1,maxv,it->l,it->r,Top-p);
ans%=mod;ans+=mod;ans%=mod;
int L=pre->l,R=it->r;
S.erase(pre);
it=S.lower_bound((node){0,x,0});
S.erase(it);
it=S.insert((node){L,R,Top}).fir;
pre=it;pre--;
}else{
int to=w/sz,m=w%sz;w=0;
seg::update(1,1,maxv,it->l,it->r,to);
if(m){
ans-=qpow(3,p)*sz%mod;
p+=to;sz-=m;
ans+=qpow(3,p)*sz%mod;
ans+=qpow(3,p+1)*m%mod;
ans%=mod;ans+=mod;ans%=mod;
int L=it->l,R=it->r;
S.erase(it);
seg::update(1,1,maxv,L,L+m-1,1);
S.insert((node){L+m,R,p});
it=(S.insert((node){L,L+m-1,p+1})).fir;
}else{
ans-=qpow(3,p)*sz%mod;
p+=to;
int L=it->l,R=it->r;
S.erase(it);
it=(S.insert((node){L,R,p})).fir;
ans+=qpow(3,p)*sz%mod;
ans%=mod;ans+=mod;ans%=mod;
}
}
}
//cout<<"S: "<<endl;
//for(auto it:S){
// cout<<it.l<<" "<<it.r<<" "<<it.s<<endl;
//}
if(x==1e6)return;
x++;w=min(Tmp,seg::query(1,1,maxv,x,maxv));
it=S.lower_bound((node){0,x,0});
pre=it;pre++;
while(w){
int Top=pre->s,sz=(it->r)-(it->l)+1,p=it->s;
//cout<<"lr: "<<(it->l)<<" "<<(it->r)<<" "<<w<<" "<<p<<" "<<Top<<endl;
if(w>=(p-Top)*sz){
w-=(p-Top)*sz;
ans-=qpow(3,p)*sz%mod;
ans+=qpow(3,Top)*sz%mod;
seg::update(1,1,maxv,it->l,it->r,Top-p);
ans%=mod;ans+=mod;ans%=mod;
int L=it->l,R=pre->r;
S.erase(pre);
it=S.lower_bound((node){0,x,0});
S.erase(it);
it=S.insert((node){L,R,Top}).fir;
pre=it;pre++;
}else{
int to=w/sz,m=w%sz;w=0;
seg::update(1,1,maxv,it->l,it->r,-to);
if(m){
ans-=qpow(3,p)*sz%mod;
p-=to;sz-=m;
ans+=qpow(3,p)*sz%mod;
ans+=qpow(3,p-1)*m%mod;
ans%=mod;ans+=mod;ans%=mod;
int L=it->l,R=it->r;
S.erase(it);
seg::update(1,1,maxv,R-m+1,R,-1);
S.insert((node){L,R-m,p});
it=(S.insert((node){R-m+1,R,p-1})).fir;
}else{
ans-=qpow(3,p)*sz%mod;
p-=to;
int L=it->l,R=it->r;
S.erase(it);
it=(S.insert((node){L,R,p})).fir;
ans+=qpow(3,p)*sz%mod;
ans%=mod;ans+=mod;ans%=mod;
}
}
}
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
S.insert((node){0,0,(int)1e12});
S.insert((node){1,(int)1e6,0});
S.insert((node){(int)1e6+1,(int)1e6+1,0});
cin>>D;
for(int i=1;i<=D;i++){
cin>>q[i].fir>>q[i].sec;
modify(q[i].fir,q[i].sec);
cout<<ans<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 2ms
memory: 11896kb
input:
4 5 11 6 10 10 15 10 30
output:
21 21 25 90
result:
ok 4 lines
Test #2:
score: 5
Accepted
time: 1ms
memory: 7856kb
input:
2 100 5 100 1000000000000
output:
5 627323485
result:
ok 2 lines
Test #3:
score: 5
Accepted
time: 0ms
memory: 30584kb
input:
20 303590 482848034083 180190 112716918480 312298 258438719980 671877 605558355401 662137 440411075067 257593 261569032231 766172 268433874550 8114 905639446594 209577 11155741818 227183 874665904430 896141 55422874585 728247 456681845046 193800 632739601224 443005 623200306681 330325 955479269245 3...
output:
108753959 108753959 108753959 148189797 148189797 148189797 148189797 32884410 32884410 32884410 32884410 32884410 32884410 32884410 3883759 3883759 3883759 3883759 3883759 3883759
result:
ok 20 lines
Test #4:
score: 5
Accepted
time: 0ms
memory: 9816kb
input:
100 11 875489524494 27 655346733112 55 844149295513 82 97463415326 56 635654736546 14 429390146337 1 829970485967 82 144361536794 3 617979252184 51 143104721653 72 995577174525 93 452582513314 87 815496282953 60 536615607040 19 579372414713 12 435950766398 100 934343203410 72 845673696897 81 5529893...
output:
978822212 978822212 978822212 978822212 978822212 978822212 142296260 142296260 142296260 142296260 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 810410907 ...
result:
ok 100 lines
Test #5:
score: 5
Accepted
time: 0ms
memory: 13912kb
input:
100 43 43 15 15 27 27 30 27 80 52 14 14 41 41 71 88 9 91 33 33 13 13 25 25 34 76 84 5 4 4 93 92 20 20 48 48 15 8 33 29 96 18 29 29 38 38 46 46 47 63 18 79 1 1 50 50 24 24 49 49 10 91 12 61 88 62 45 45 44 61 3 3 11 11 47 11 6 6 63 5 47 14 2 2 35 35 28 28 36 52 30 30 10 10 39 39 57 25 7 7 62 28 34 34 ...
output:
43 43 43 43 52 52 52 105 216513 216513 216513 216513 216513 216513 216513 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 216514 21...
result:
ok 100 lines
Test #6:
score: 5
Accepted
time: 0ms
memory: 32576kb
input:
3000 389827 110992615705 457563 444372360369 256352 898797907949 50369 470114259594 536961 780871950938 474229 385066758858 336395 861058138100 703904 669605078267 929489 709932162971 60442 900651181969 711481 373701639231 724319 89189248157 747906 595362712512 476308 887763836755 896247 47995270950...
output:
838997771 701151577 842263243 321767212 321767212 321767212 321767212 321767212 321767212 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 569570113 ...
result:
ok 3000 lines
Test #7:
score: 5
Accepted
time: 0ms
memory: 28496kb
input:
3000 725588 488105926604 241997 623229247938 29245 854297554966 931536 440338608870 139184 160710171248 389995 95507999334 120857 774311729791 969885 249313688193 536982 820282535262 689287 803968971955 553855 810385575329 998109 752663281850 455152 339496697317 874383 633789126717 137619 8034383435...
output:
350043039 479497933 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 34708674 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118603 676118...
result:
ok 3000 lines
Test #8:
score: 5
Accepted
time: 0ms
memory: 29020kb
input:
3000 610610 503759232178 136114 862705461223 976840 7217801833 174872 342906979378 234812 381204217439 376593 476693687266 989047 535271248331 42316 470330041687 816430 541843519120 717453 936296681083 368387 693641260211 102397 12657891727 474507 120467507603 159588 764510985184 547389 61152744413 ...
output:
543644051 501550349 501550349 501550349 501550349 501550349 501550349 383088549 383088549 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 738388190 ...
result:
ok 3000 lines
Test #9:
score: 5
Accepted
time: 343ms
memory: 22644kb
input:
199950 65 57 96 54 43 20 45 85 73 85 12 65 72 8 97 96 89 72 66 9 7 20 48 65 34 62 59 74 2 52 58 57 14 95 73 14 11 23 62 40 60 79 5 19 42 64 93 43 45 38 50 32 98 31 33 54 74 41 96 60 82 92 1 27 24 78 73 20 46 59 11 96 41 5 21 68 59 75 70 37 87 46 6 8 3 24 85 32 67 7 43 99 76 98 80 87 56 77 13 80 83 3...
output:
57 57 57 125 125 1802 1802 1813 1813 1813 1813 1813 1813 1813 577207075 577207075 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 577207263 295345277 295345277 295345277 295345277 295345717 295345717 29534571...
result:
ok 199950 lines
Test #10:
score: 5
Accepted
time: 344ms
memory: 27960kb
input:
199950 79 21 75 89 23 81 97 62 68 58 94 9 53 85 63 15 17 51 1 86 66 32 85 69 71 88 66 79 50 14 54 61 77 51 32 12 88 3 32 52 96 99 44 8 98 30 43 58 59 89 44 5 89 77 20 34 13 33 17 35 96 98 17 42 57 48 29 55 61 32 89 49 30 14 75 12 28 55 45 15 19 88 41 12 76 88 15 69 22 31 69 94 15 47 94 54 97 58 4 88...
output:
21 103 431 431 431 431 431 431 431 473062302 473062302 473062302 473062302 473062302 473062302 473062302 473062302 473062302 473062302 473062302 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 473062312 47306...
result:
ok 199950 lines
Test #11:
score: 5
Accepted
time: 334ms
memory: 21168kb
input:
199950 85 63 12 89 89 34 16 37 60 18 81 46 61 69 54 2 41 14 89 98 85 13 51 93 63 91 18 60 35 94 93 29 40 3 62 2 47 15 50 24 47 91 92 64 38 68 37 89 88 5 83 64 24 72 48 78 98 98 97 60 17 25 84 2 10 3 22 52 37 8 40 12 83 75 14 36 91 43 20 48 26 10 69 80 57 70 37 56 25 32 59 4 22 44 95 100 42 45 82 38 ...
output:
63 16038 16038 16038 16038 16038 16038 16038 16038 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16047 16049 16049 16049 160...
result:
ok 199950 lines
Test #12:
score: 5
Accepted
time: 319ms
memory: 27612kb
input:
199950 12 48 61 59 57 41 8 8 66 6 64 69 6 2 54 31 8 66 33 80 87 70 95 52 23 31 30 99 24 13 3 41 25 56 21 78 77 16 65 45 61 68 40 39 49 39 33 39 59 46 83 11 2 14 35 64 75 21 81 51 20 61 2 35 30 33 78 100 60 34 30 70 47 1 91 15 72 11 21 82 82 52 68 27 97 57 18 65 3 50 51 86 12 30 84 41 36 15 46 12 70 ...
output:
324 335 335 335 335 345 345 345 26247 26258 26258 26258 26258 26288 26288 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 3720536 172187576 172187576 172187577 172187577 172187577 172187577 172187577 172187577 172187577 17218757...
result:
ok 199950 lines
Test #13:
score: 5
Accepted
time: 321ms
memory: 24124kb
input:
199950 87 41 85 75 58 83 2 8 53 47 22 18 16 48 38 24 69 68 37 16 21 82 91 34 33 99 82 10 2 32 80 20 17 25 70 38 81 55 56 6 6 87 14 4 18 22 76 95 81 54 20 72 72 31 4 81 90 92 75 41 8 99 21 50 71 20 76 38 21 69 89 73 85 48 19 42 32 80 51 45 82 21 45 98 48 61 46 2 78 54 36 73 22 36 60 88 86 13 1 94 83 ...
output:
41 75 108 148 148 148 203 203 203 203 532 532 553 553 28697965 28697965 28697965 28697965 28697965 28697965 34012236 34012236 34012236 34012236 34012236 34012236 34012236 973568790 973568790 973568790 973568976 973568976 973568976 973568976 973568976 973568976 973568976 973568976 973568976 973568976...
result:
ok 199950 lines
Test #14:
score: 5
Accepted
time: 346ms
memory: 25656kb
input:
199950 50 7 19 46 86 61 88 53 54 52 47 8 63 17 10 89 85 20 19 92 59 74 12 35 44 43 38 21 46 27 38 87 66 37 54 64 67 6 52 61 73 17 82 81 71 25 50 46 54 85 86 48 90 36 9 72 13 83 39 5 50 72 54 70 49 11 8 23 3 50 10 4 94 39 41 28 62 67 6 25 2 95 23 97 29 69 36 26 41 36 4 66 17 70 89 80 87 66 6 68 58 61...
output:
7 105 120 120 120 120 120 61236 61236 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 61239 100443567 100443567 100443567 100443567 100443567 100443567 738770587 738770589 738770589 738770589 738770589 73...
result:
ok 199950 lines
Test #15:
score: 5
Accepted
time: 339ms
memory: 25948kb
input:
199950 5 39 20 51 55 30 47 20 61 1 70 86 13 93 61 17 100 66 27 66 93 23 8 75 36 49 12 39 51 98 5 48 45 22 62 51 75 12 26 57 5 62 48 83 87 19 5 41 82 57 53 42 83 28 38 42 41 29 21 87 15 26 100 3 32 9 68 58 4 80 30 20 53 28 46 41 34 36 64 45 21 92 81 13 32 53 77 3 14 58 71 91 51 72 98 82 94 38 99 47 7...
output:
9477 9489 9489 9489 9489 9524 14337 14337 14337 14337 14337 91953 91953 91953 91958 91958 91958 91958 91958 91958 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 1594562 649045862 649045862 649045862 649045862 649045862 649045862 649045862 6490...
result:
ok 199950 lines
Test #16:
score: 5
Accepted
time: 358ms
memory: 23988kb
input:
199950 78 37 90 87 58 1 73 68 38 15 95 59 87 48 60 69 84 23 26 10 83 95 30 49 90 39 63 71 32 98 16 43 89 88 54 85 78 34 52 70 78 14 34 52 97 87 91 10 67 13 79 97 59 20 24 51 40 18 75 89 20 25 99 47 12 35 75 41 76 42 83 93 99 24 95 68 34 16 81 75 40 18 71 2 28 43 8 31 9 18 4 67 5 68 23 17 8 31 51 3 3...
output:
37 87 87 87 87 87 87 96 96 96 107 114 114 114 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 324 384 384 143489104 143489104 143489104 143489104 143489104 143489104 753769016 753769016 753769016 753769016 753769016 753769016 753769016 ...
result:
ok 199950 lines
Test #17:
score: 5
Accepted
time: 344ms
memory: 26148kb
input:
199950 68 27 57 74 18 85 83 44 86 16 81 47 24 54 84 15 79 56 90 31 61 10 8 13 49 48 24 12 6 57 91 3 41 38 35 79 55 62 58 60 55 12 44 85 73 19 46 14 93 45 24 87 34 80 29 15 65 10 12 50 97 42 99 17 44 53 74 57 2 65 88 46 34 18 79 28 41 93 8 31 27 28 97 75 1 51 20 45 81 5 27 17 11 71 20 85 96 64 24 28 ...
output:
27 91 1188 1188 1188 1188 1188 1188 1188 1188 1188 1188 1188 1188 78792 78792 78792 78792 78792 78792 78792 78792 78792 78792 78792 78794 78794 78794 78794 78794 78794 78794 78794 78794 567840963 567840963 567840963 567840963 567840969 567840969 567840969 567840969 711698642 711698642 711698642 7116...
result:
ok 199950 lines
Test #18:
score: 5
Accepted
time: 333ms
memory: 24224kb
input:
199950 70 9 82 77 97 97 27 78 33 26 84 86 14 66 9 86 8 89 32 85 26 47 59 95 60 52 76 89 22 97 17 34 36 11 25 30 2 11 34 64 74 51 24 3 11 52 99 58 4 57 40 19 92 34 79 48 98 89 90 53 26 66 83 77 44 86 36 89 64 99 62 98 71 39 94 88 43 63 33 15 45 54 27 6 10 80 66 15 28 36 89 78 26 77 1 86 57 41 32 12 9...
output:
9 77 97 244 244 244 949 124670 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 590498 9574694 9574694 9574694 9574694 9574694 9574694 9574694 9574694 9574694 9574694 9574696 9574696 9574696 9574696 9574696 9574696 9574696 9574696 9574696 95746...
result:
ok 199950 lines
Test #19:
score: 5
Accepted
time: 348ms
memory: 28212kb
input:
199950 50 14 34 75 78 81 23 14 60 66 58 23 52 64 27 49 74 38 8 1 65 76 18 72 76 87 70 57 53 73 54 4 38 98 33 69 26 67 61 59 26 70 7 70 27 60 36 34 96 84 65 53 47 59 81 24 58 69 51 77 70 57 44 14 28 65 26 75 67 95 62 39 70 23 45 89 58 47 39 30 58 98 40 20 64 53 82 62 32 82 2 46 97 51 19 94 50 40 14 2...
output:
14 144 150 150 150 150 150 150 150 150 150 495 501 501 501 501 518 518 518 518 518 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 137809 762119163 762119163 762119175 762119175 762119175...
result:
ok 199950 lines
Test #20:
score: 5
Accepted
time: 349ms
memory: 23276kb
input:
199950 7 89 11 26 61 52 72 96 97 95 9 12 38 14 74 43 82 1 45 47 32 86 71 80 77 51 100 36 26 47 4 91 27 1 13 8 8 86 13 60 26 96 64 37 55 65 78 73 40 18 29 80 100 77 35 95 57 85 88 62 82 22 48 31 97 74 92 37 16 86 90 2 95 88 80 45 68 69 87 6 26 6 42 56 5 74 9 26 85 11 33 100 21 55 88 44 10 4 8 69 49 6...
output:
3011499 3011499 3011499 3011506 3011506 3011506 3011506 3011506 3011506 3011506 3011506 3011506 3011506 3011506 3011506 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 603531307 ...
result:
ok 199950 lines