ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#203725 | #176. fibonacci | snow_trace | 100 | 6548ms | 57064kb | C++11 | 3.1kb | 2024-03-10 10:53:30 | 2024-03-10 12:14:23 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1000000007;
int B[200005],C[200005],F[500005],F1[500005],F2[500005];
struct node{
int l,r;
int a2,b2,c2,ab,ac,bc;
int kb,kc;
void addB(int k){
a2 = (a2+k*k%mod*b2+2*k*ab)%mod;
ab = (ab+k*b2)%mod;
ac = (ac+k*bc)%mod;
kb = (kb+k)%mod;
}void addC(int k){
a2 = (a2+k*k%mod*c2+2*k*ac)%mod;
ac = (ac+k*c2)%mod;
ab = (ab+k*bc)%mod;
kc = (kc+k)%mod;
}
}tree[1000005];
void push_up(int k){
tree[k].a2 = (tree[k<<1].a2+tree[k<<1|1].a2)%mod;
tree[k].ab= (tree[k<<1].ab+tree[k<<1|1].ab)%mod;
tree[k].ac = (tree[k<<1].ac+tree[k<<1|1].ac)%mod;
}void push_down(int k){
if(tree[k].kb){
tree[k<<1].addB(tree[k].kb);
tree[k<<1|1].addB(tree[k].kb);
tree[k].kb = 0;
}if(tree[k].kc){
tree[k<<1].addC(tree[k].kc);
tree[k<<1|1].addC(tree[k].kc);
tree[k].kc = 0;
}
}void build(int l,int r,int k){
tree[k].l= l ,tree[k].r = r;
if(l+1 == r){
tree[k].b2 = B[l]*B[l]%mod;
tree[k].c2 = C[l]*C[l]%mod;
tree[k].bc = B[l]*C[l]%mod;
return;
}build(l,l+r>>1,k<<1),build(l+r>>1,r,k<<1|1);
tree[k].b2 = (tree[k<<1].b2+tree[k<<1|1].b2)%mod;
tree[k].c2 = (tree[k<<1].c2+tree[k<<1|1].c2)%mod;
tree[k].bc = (tree[k<<1].bc+tree[k<<1|1].bc)%mod;
}void update(int l,int r,int k,int kb,int kc){
int ll = tree[k].l,rr = tree[k].r;
if(ll>=r or l>=rr)return;
if(l<=ll and rr<=r){
tree[k].addB(kb);
//cout <<" " << k << " " << l << " " << r << " " << tree[k].a2 << " " << tree[k].ab << " " << tree[k].ac << endl;
tree[k].addC(kc);
//cout <<" " << k << " " << l << " " << r << " " << tree[k].a2 << " " << tree[k].ab << " " << tree[k].ac << endl;
return;
}push_down(k);
update(l,r,k<<1,kb,kc);update(l,r,k<<1|1,kb,kc);
push_up(k);
}int query(int l,int r,int k){
int ll = tree[k].l,rr = tree[k].r;
if(ll>=r or l>=rr)return 0;
if(l<=ll and rr<=r)return tree[k].a2;
push_down(k);
return (query(l,r,k<<1)+query(l,r,k<<1|1))%mod;
}int n,q;//int aa[5005];
signed main(){
srand(time(0));
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// n = 50,q = 500000;
cin>> n >> q;
F[1] = F[2] = 1;
F1[1] = 1,F2[2] = 1;
for(int i = 3;i<=500000;i++)F[i] = (F[i-1]+F[i-2])%mod;
for(int i = 3;i<=500000;i++){
F1[i] = (F1[i-2]-F1[i-1]+mod)%mod;
F2[i] = (F2[i-2]-F2[i-1]+mod)%mod;
}for(int i = 1;i<=n;i++)B[i] = F[i],C[i] = F[i-1];
build(1,n+1,1);
while(q--){
int op,l,r;
cin >> op >> l >> r;
//op = rand()%2+1,l = rand()%n+1,r = rand()%n+1;
//if(l>r)swap(l,r);
if(op == 1){
// for(int i = l;i<=r;i++)aa[i] = (aa[i]+F[i-l+1])%mod;
update(l,r+1,1,F1[l],F2[l]);
}else{
int res =query(l,r+1,1);
// int rr = 0;
// for(int i = l;i<=r;i++)rr = (rr+aa[i]*aa[i])%mod;
// assert(res == rr);
cout << res << endl;
}//for(int i = 1;i<=n;i++)cout << query(i,i+1,1) << " ";cout << endl;
}
return 0;
}/*
我们考虑直接把 fj 表示为 fi 和 fi-1 的线性组合
那么我们每个点上对应的值是 k1fi+k2fi-1,
这个东西可以直接维护
维护的东西有点太多了。
5 2
1 3 5
2 1 5
*/
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 14ms
memory: 13156kb
input:
1000 1000 1 483 980 1 868 888 1 982 995 2 132 368 2 704 992 2 358 919 2 282 468 1 59 466 1 390 872 1...
output:
0 282551976 498506390 0 402632756 279093696 920434803 320089633 788131703 551229883 79289048 7348739...
result:
ok 494 lines
Test #2:
score: 5
Accepted
time: 5ms
memory: 13160kb
input:
1000 1000 1 42 468 1 170 501 1 359 479 2 465 706 2 282 828 1 492 996 2 437 828 2 605 903 2 293 383 2...
output:
741866782 415913094 675453798 676700809 683183762 652499868 53495222 169514117 786693613 301834283 9...
result:
ok 510 lines
Test #3:
score: 5
Accepted
time: 9ms
memory: 13160kb
input:
1000 1000 1 150 755 1 30 358 2 205 652 2 219 879 1 146 819 1 667 905 1 16 156 2 274 874 1 446 988 1 ...
output:
305512036 547484640 842627175 651308368 456467140 588199944 514742453 38261868 361046811 15165025 59...
result:
ok 490 lines
Test #4:
score: 5
Accepted
time: 10ms
memory: 13160kb
input:
1000 1000 2 543 864 2 100 132 2 387 415 1 6 427 2 415 726 2 339 690 2 128 133 1 803 974 2 311 599 1 ...
output:
0 0 0 332359929 9214886 731056350 558255681 248675488 96773504 764979705 857282231 724070871 4705292...
result:
ok 513 lines
Test #5:
score: 5
Accepted
time: 11ms
memory: 13156kb
input:
1000 1000 1 87 867 1 141 916 1 251 260 1 612 776 2 80 759 2 392 963 1 168 169 1 416 873 2 176 892 1 ...
output:
193892071 472522667 86032618 874007698 482117996 881318385 57762633 999853998 428809988 681212671 75...
result:
ok 517 lines
Test #6:
score: 5
Accepted
time: 7ms
memory: 13156kb
input:
1000 1000 2 468 998 2 448 506 1 61 625 1 414 788 2 142 429 2 320 653 2 85 475 1 343 352 1 187 456 2 ...
output:
0 0 406525593 643954333 854057295 441389076 260343306 834611188 688061970 577900391 767239779 878148...
result:
ok 508 lines
Test #7:
score: 5
Accepted
time: 147ms
memory: 24000kb
input:
50000 50000 2 20971 26669 2 9303 32222 1 3465 11750 2 8048 8635 1 24984 25896 2 4028 11353 2 6964 94...
output:
0 0 296703105 467083804 119457050 26240498 0 411219337 647902041 968218341 244782853 994315711 97016...
result:
ok 24848 lines
Test #8:
score: 5
Accepted
time: 201ms
memory: 24000kb
input:
50000 50000 1 2457 29709 2 8125 14435 1 1567 25791 2 8267 21744 2 3695 19405 1 25524 28574 2 18390 2...
output:
366943780 244785486 265520828 164357822 825296106 428312725 859668465 553147533 745734409 2702426 59...
result:
ok 25187 lines
Test #9:
score: 5
Accepted
time: 215ms
memory: 24000kb
input:
50000 50000 1 4482 21592 1 25911 26580 1 5007 12624 1 12713 20150 2 5211 23058 1 21465 27732 2 22720...
output:
913001264 500405697 192096084 263428592 653716478 416580509 622278730 483343260 440795770 540800563 ...
result:
ok 25187 lines
Test #10:
score: 5
Accepted
time: 165ms
memory: 24004kb
input:
50000 50000 1 11496 27953 2 24249 30802 1 23071 25334 1 10484 24846 2 7943 9971 1 5441 15289 2 10411...
output:
952510565 0 872916256 740406894 830377070 299013298 310666643 532259154 64269623 743095976 31243471 ...
result:
ok 25104 lines
Test #11:
score: 5
Accepted
time: 149ms
memory: 24000kb
input:
50000 50000 1 5176 13783 1 15703 20395 1 3902 20006 2 19780 29146 2 2148 31454 2 1046 22702 2 23758 ...
output:
543600682 388674072 388674072 0 732170156 897461079 755389789 888390880 64585707 337067643 2133508 7...
result:
ok 25101 lines
Test #12:
score: 5
Accepted
time: 140ms
memory: 24004kb
input:
50000 50000 1 22605 24944 1 6963 19903 1 27566 28217 2 742 13189 2 5149 23534 2 14142 17074 2 3366 1...
output:
302153002 870137297 177473841 244578963 639201586 225970652 855292038 296016297 598514528 939204858 ...
result:
ok 24941 lines
Test #13:
score: 5
Accepted
time: 813ms
memory: 57064kb
input:
200000 200000 1 19362 32330 1 322 3895 1 23101 27080 1 9108 31477 2 23890 29804 2 7541 22509 1 1849 ...
output:
213280667 547440148 143470219 204855952 881108430 320435426 9430407 772733524 473321158 282334742 55...
result:
ok 100195 lines
Test #14:
score: 5
Accepted
time: 568ms
memory: 57060kb
input:
200000 200000 1 11112 27199 2 8547 22842 1 7901 32765 1 15214 20902 1 6780 23244 2 2418 4855 1 11635...
output:
103583070 0 404313020 211429996 565866569 344747044 367865951 883827665 492919898 568899976 98060658...
result:
ok 99902 lines
Test #15:
score: 5
Accepted
time: 689ms
memory: 57060kb
input:
200000 200000 1 6558 22763 1 8387 31224 1 10694 30928 2 15621 20882 2 22561 30418 1 22527 25011 1 15...
output:
561306906 20837446 749026809 233565549 0 817926422 346396821 222833968 24079570 11745367 553524004 5...
result:
ok 100204 lines
Test #16:
score: 5
Accepted
time: 519ms
memory: 57060kb
input:
200000 200000 2 18971 22288 1 8582 28719 1 4048 9474 2 13969 27797 2 4704 24502 2 17364 20131 1 2040...
output:
0 433326673 210102430 901332479 763928833 844740056 857286875 414556439 626467992 539318245 86104556...
result:
ok 100166 lines
Test #17:
score: 5
Accepted
time: 794ms
memory: 57064kb
input:
200000 200000 1 25851 26492 1 5257 16206 2 2090 8382 2 17178 30011 2 2661 9481 2 15169 30843 1 23304...
output:
581608652 494077081 475924261 390494363 376193594 185311064 0 837384107 568174979 0 422356429 373746...
result:
ok 100177 lines
Test #18:
score: 5
Accepted
time: 691ms
memory: 57064kb
input:
200000 200000 2 173 22421 2 5769 5993 2 4905 10572 1 7918 32558 1 23321 24101 2 5887 27139 1 12521 2...
output:
0 0 0 832365138 253696266 94110485 899116332 66027182 258404532 958588075 702121231 636271156 804155...
result:
ok 99809 lines
Test #19:
score: 5
Accepted
time: 691ms
memory: 57060kb
input:
200000 200000 1 19498 28884 1 20440 22452 2 8294 29619 2 10119 21307 1 8700 27176 1 5646 12407 1 191...
output:
399573454 285560165 0 260122027 264203261 388886217 220471149 515196202 180349062 918219944 70713431...
result:
ok 100132 lines
Test #20:
score: 5
Accepted
time: 710ms
memory: 57060kb
input:
200000 200000 1 6558 22763 1 8387 31224 1 10694 30928 2 15621 20882 2 22561 30418 1 22527 25011 1 15...
output:
561306906 20837446 749026809 233565549 0 817926422 346396821 222833968 24079570 11745367 553524004 5...
result:
ok 100204 lines