QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#75762#5310. PaintingA_zjzjAC ✓1027ms49012kbC++142.3kb2023-02-06 10:06:082023-02-06 10:06:11

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-06 10:06:11]
  • 评测
  • 测评结果:AC
  • 用时:1027ms
  • 内存:49012kb
  • [2023-02-06 10:06:08]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=3e5+10,K=__lg(N)+2;
int n,W,a[N],b[N],fa[K][N],dep[N];
struct cmp{
	bool operator () (const int &x,const int &y)const{
		return a[x]<a[y];
	}
};
set<int,cmp>s;
struct frac{
	ll x,y;
	frac trs()const{
		ll g=__gcd(abs(x),abs(y));
		return {x/g,y/g};
	}
	bool operator < (const frac &a)const{
		return x*a.y<y*a.x;
	}
	bool operator <= (const frac &a)const{
		return x*a.y<=y*a.x;
	}
	frac operator * (const int &a)const{
		return {x*a,y};
	}
	frac operator + (const frac &a)const{
		return {x*a.y+a.x*y,y*a.y};
	}
}r[N];
void write(frac x){
	x=x.trs(),printf("%lld/%lld",x.x,x.y);
}
frac calc(int i,int j){
	ll x=a[i]-a[j],y=b[j]-a[j]-b[i]+a[i];
	if(y<0)x=-x,y=-y;
	return {x,y};
}
int LCA(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(int i=K-1;~i;i--)if(dep[fa[i][u]]>=dep[v])u=fa[i][u];
	if(u==v)return u;
	for(int i=K-1;~i;i--)if(fa[i][u]^fa[i][v])u=fa[i][u],v=fa[i][v];
	return fa[0][u];
}
bool find(int p,int u,int t){
	for(int i=K-1;~i;i--){
		int v=fa[i][u];
		if(dep[v]<=t)continue;
		frac tmp=calc(v,p);
		if(tmp.x<0||!tmp.y||r[v]<tmp)
			u=v;
	}
	frac tmp=calc(u,p);
	if(r[u]<tmp||!tmp.y||tmp.x<0)u=fa[0][u];
	tmp=calc(u,p);
	if(r[u]<tmp||!tmp.y||tmp.x<0||dep[u]<=t)return 0;
	if(tmp.x>tmp.y)return 0;
	fa[0][p]=u;
	dep[p]=dep[u]+1;
	r[p]=tmp;
	for(int i=1;i<K;i++)fa[i][p]=fa[i-1][fa[i-1][p]];
	return 1;
}
int main(){
	scanf("%d%d",&n,&W);
	a[0]=b[0]=0,s.insert(0);
	a[n+1]=b[n+1]=1e6+1,s.insert(n+1);
	r[0]=r[n+1]={1,1};
	dep[0]=dep[n+1]=1;
	for(int i=0;i<K;i++)fa[i][0]=0,fa[i][n+1]=n+1;
	for(int i=1,op;i<=n;i++){
		scanf("%d%d%d",&a[i],&b[i],&op);
		auto it=s.lower_bound(i);
		int v=*it,u=*prev(it);
		if(fa[K-1][u]!=fa[K-1][v]){
			bool x=find(i,u,0),y=find(i,v,0);
			if(!x&&!y){
				dep[i]=1;
				for(int j=0;j<K;j++)fa[j][i]=i;
				r[i]=(frac){1,1};
			}
		}
		else{
			int t=LCA(u,v);
			bool x=find(i,u,dep[t]),y=find(i,v,dep[t]);
			if(!x&&!y){
				dep[i]=dep[t]+1;
				fa[0][i]=t;
				for(int j=1;j<K;j++)fa[j][i]=fa[j-1][fa[j-1][i]];
				r[i]=calc(i,t);
			}
		}
		if(op)s.insert(i);
		putchar('(');
		write(r[i]*W);
		putchar(',');
		write(r[i]*b[i]+(frac){r[i].y-r[i].x,r[i].y}*a[i]);
		puts(")");
	}
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 16148kb

input:

4 3
1 2 1
2 1 1
3 1 0
3 2 1

output:

(3/1,2/1)
(3/2,3/2)
(2/1,5/3)
(3/1,2/1)

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 316ms
memory: 35360kb

input:

300000 894665
1 1000000 1
2 999999 1
3 999997 1
4 999994 1
5 999990 1
6 999985 1
7 999979 1
8 999972 1
9 999964 1
10 999955 1
11 999945 1
12 999934 1
13 999922 1
14 999909 1
15 999895 1
16 999880 1
17 999864 1
18 999847 1
19 999829 1
20 999810 1
21 999790 1
22 999769 1
23 999747 1
24 999724 1
25 999...

output:

(894665/1,1000000/1)
(894665/2,1000001/2)
(894665/3,1000003/3)
(894665/4,500003/2)
(178933/1,200002/1)
(894665/6,1000015/6)
(894665/7,1000021/7)
(894665/8,250007/2)
(894665/9,1000036/9)
(178933/2,200009/2)
(894665/11,1000055/11)
(894665/12,500033/6)
(894665/13,1000078/13)
(894665/14,1000091/14)
(178...

result:

ok 300000 lines

Test #3:

score: 0
Accepted
time: 322ms
memory: 35516kb

input:

300000 748539
1 1000000 1
2 999999 1
3 999997 1
4 999994 1
5 999990 1
6 999985 1
7 999979 1
8 999972 1
9 999964 1
10 999955 1
11 999945 1
12 999934 1
13 999922 1
14 999909 1
15 999895 1
16 999880 1
17 999864 1
18 999847 1
19 999829 1
20 999810 1
21 999790 1
22 999769 1
23 999747 1
24 999724 1
25 999...

output:

(748539/1,1000000/1)
(748539/2,1000001/2)
(249513/1,1000003/3)
(748539/4,500003/2)
(748539/5,200002/1)
(249513/2,1000015/6)
(748539/7,1000021/7)
(748539/8,250007/2)
(83171/1,1000036/9)
(748539/10,200009/2)
(68049/1,1000055/11)
(249513/4,500033/6)
(748539/13,1000078/13)
(748539/14,1000091/14)
(249513...

result:

ok 300000 lines

Test #4:

score: 0
Accepted
time: 879ms
memory: 38188kb

input:

300000 329779
725400 466189 0
162004 217124 0
17706 369295 0
143330 449439 0
974743 495692 0
476940 609424 0
307270 769869 0
664031 252064 0
350818 610178 1
432310 398376 1
578066 277363 0
891345 770652 0
815291 550496 0
756083 89624 0
867560 132663 0
668047 648059 0
758279 558971 0
647877 798646 0
...

output:

(329779/1,466189/1)
(329779/1,217124/1)
(329779/1,369295/1)
(329779/1,449439/1)
(329779/1,495692/1)
(329779/1,609424/1)
(329779/1,769869/1)
(329779/1,252064/1)
(329779/1,610178/1)
(13437175134/146647,62014289806/146647)
(10705945456/80009,36488460402/80009)
(329779/1,770652/1)
(153173441467/524155,6...

result:

ok 300000 lines

Test #5:

score: 0
Accepted
time: 1006ms
memory: 42352kb

input:

300000 694159
107635 109585 0
534761 296221 0
144102 179000 0
538335 642812 1
51653 789858 1
260572 297502 0
422312 230960 1
323495 527460 0
117743 91695 1
875390 106429 0
592512 956940 1
429082 37803 1
711668 658986 1
614065 738921 1
667461 145160 1
480889 809090 0
468893 650474 1
945932 974900 0
8...

output:

(694159/1,109585/1)
(694159/1,296221/1)
(694159/1,179000/1)
(694159/1,642812/1)
(168917345219/316864,196002519097/316864)
(145023004121/701275,38089601594/140255)
(257296280781/929557,321636734816/929557)
(68594709903/395317,148038282320/395317)
(15292322770/254751,29421309553/254751)
(233969761745/...

result:

ok 300000 lines

Test #6:

score: 0
Accepted
time: 1008ms
memory: 46560kb

input:

300000 841571
864537 929867 1
827774 222788 1
594947 97405 1
136507 869841 1
767546 276887 0
214999 287658 1
193877 95397 1
880581 221638 1
435653 172067 1
708385 794804 1
112241 426357 1
269382 890618 1
639737 968391 1
984457 815612 1
178024 935648 1
786418 222454 1
698936 199097 1
604327 637464 1
...

output:

(841571/1,929867/1)
(841571/1,222788/1)
(841571/1,97405/1)
(96452452310/307719,126053207273/307719)
(5631793132/12703,6466646810/12703)
(22018863644/220225,49249204851/220225)
(24140464135/415907,77809902639/415907)
(13502165124/724273,627208961121/724273)
(125876299183/498460,88865122801/249230)
(1...

result:

ok 300000 lines

Test #7:

score: 0
Accepted
time: 965ms
memory: 48116kb

input:

300000 423599
702291 13316 0
823376 324624 1
332084 339170 1
796909 473709 0
414801 475840 1
712638 235854 1
613165 447937 1
252983 673258 1
622687 27409 1
982234 935915 1
444618 148018 0
902272 211128 1
318963 363730 1
447033 949507 0
219549 778365 1
903735 908804 1
830179 642747 1
383246 554384 1
...

output:

(423599/1,13316/1)
(423599/1,324624/1)
(104055399954/252919,85731000752/252919)
(11211394733/175552,2052263037/2743)
(173071961425/559791,257140876016/559791)
(11469405033/48893,21933562806/48893)
(42136663327/311556,2078565594/3709)
(33507104499/413189,137773965562/413189)
(672251613/71675,43686384...

result:

ok 300000 lines

Test #8:

score: 0
Accepted
time: 1027ms
memory: 49012kb

input:

300000 606503
969162 499887 1
982467 476155 1
813898 466871 1
919610 819701 1
267484 907609 1
824721 490861 1
387197 878325 1
809204 742752 1
35348 71222 1
75368 820418 1
733140 325762 1
121959 320121 1
104047 533979 1
845038 737152 0
827435 615384 1
982300 340253 1
50533 570452 1
734351 776533 1
92...

output:

(606503/1,499887/1)
(8069522415/37037,29651149119/37037)
(606503/1,466871/1)
(15026718328/184683,55786996082/61561)
(165700865121/493576,306910313659/493576)
(606503/1,490861/1)
(258795436603/838155,534095910263/838155)
(2846925082/280575,226730486612/280575)
(606503/1,71222/1)
(447920660590/1092077...

result:

ok 300000 lines

Test #9:

score: 0
Accepted
time: 634ms
memory: 36972kb

input:

300000 879149
116071 862 0
158243 245 0
148791 414 0
696577 955 0
382289 841 1
753537 277 0
691381 102 0
902273 638 0
636078 121 0
846069 338 0
209415 476 0
938281 286 0
567567 812 0
395269 317 0
638750 19 0
822890 57 0
529243 137 1
784931 595 0
904266 457 0
281103 606 0
207471 884 0
953117 757 0
87...

output:

(879149/1,862/1)
(879149/1,245/1)
(879149/1,414/1)
(879149/1,955/1)
(879149/1,841/1)
(81595576988/92953,131957641/92953)
(271737922708/309831,542457943/309831)
(457143413616/520187,514911211/520187)
(223118345561/254509,488684629/254509)
(407731723220/464283,582330347/464283)
(879149/1,476/1)
(48879...

result:

ok 300000 lines

Test #10:

score: 0
Accepted
time: 750ms
memory: 38192kb

input:

300000 310835
319788 772 0
497504 964 0
894936 618 0
294654 90 0
881733 706 0
245858 983 0
559020 541 0
736248 666 0
453767 954 0
158758 958 0
763887 432 0
536922 327 0
882208 941 0
674991 503 0
92620 448 0
168993 777 1
645752 987 0
354095 426 0
356444 434 0
250356 643 1
279019 297 0
64002 163 0
804...

output:

(310835/1,772/1)
(310835/1,964/1)
(310835/1,618/1)
(310835/1,90/1)
(310835/1,706/1)
(310835/1,983/1)
(310835/1,541/1)
(310835/1,666/1)
(310835/1,954/1)
(310835/1,958/1)
(310835/1,432/1)
(310835/1,327/1)
(310835/1,941/1)
(310835/1,503/1)
(310835/1,448/1)
(310835/1,777/1)
(310835/1,987/1)
(57536180170...

result:

ok 300000 lines

Test #11:

score: 0
Accepted
time: 815ms
memory: 40984kb

input:

300000 735537
701978 710 1
317350 506 1
994500 510 0
29112 354 1
595720 267 0
187266 549 0
778998 808 1
611272 914 0
99687 461 0
99147 712 0
156253 168 0
700158 863 0
39453 626 0
855867 553 0
841635 917 0
644146 114 1
519578 971 1
753580 742 1
219545 199 0
564270 186 0
26278 263 1
128304 400 0
34213...

output:

(735537/1,710/1)
(735537/1,506/1)
(35860125719/48787,58014370/48787)
(735537/1,354/1)
(204751434690/278609,216701870/278609)
(95681595108/130127,79468554/130127)
(735537/1,808/1)
(33358809561/45455,103802386/45455)
(735537/1,461/1)
(53498793337/72803,175784818/218409)
(93516909717/127327,50422746/12...

result:

ok 300000 lines

Test #12:

score: 0
Accepted
time: 955ms
memory: 46480kb

input:

300000 737138
938874 996 0
236261 342 1
294733 837 1
495487 53 1
767065 449 1
527431 967 1
769556 678 1
190570 261 0
937523 748 1
132549 535 1
666839 915 1
587237 49 1
621620 405 1
349581 510 1
234810 702 1
324423 189 1
201382 183 1
928491 1 0
616164 513 1
197342 29 1
49493 565 1
764718 8 1
657160 5...

output:

(737138/1,996/1)
(737138/1,342/1)
(737138/1,837/1)
(73991701026/100769,199550885/100769)
(43521733227/59090,31856143/29545)
(44160831873/60038,63116917/30019)
(175005038287/237491,222144699/237491)
(737138/1,261/1)
(473824935020/642879,564246467/642879)
(76450056256/103905,81067877/103905)
(18470098...

result:

ok 300000 lines