QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#39034#1265. Total EclipseJamiiiiiiAC ✓273ms4952kbC++112.0kb2022-07-08 11:48:072022-07-08 11:48:10

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-08 11:48:10]
  • Judged
  • Verdict: AC
  • Time: 273ms
  • Memory: 4952kb
  • [2022-07-08 11:48:07]
  • Submitted

answer

#include <cstdio>
#include <algorithm>
#pragma GCC optimize(2)
using namespace std ;
int n , m ;
struct node
{
	int bh , br ;
} a[100010] ;
int bb[100010] ;
int fir[100010] , nxt[400010] , zx[400010] , bs ;
int fa[100010] ;
int lt = 0 ;
inline bool pd( node a , node b )
{
	return a.br > b.br ;
}
int cx( int a )
{
	if( fa[a] != a ) fa[a] = cx( fa[a] );
	return fa[a];
}
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while( ch < '0' || ch > '9' ){if(ch == '-') f = -1; ch = getchar(); }
	while( ch >= '0' && ch <= '9'){x = (x<<3) + (x<<1) + ch-'0'; ch = getchar(); }
	return x*f;
}
int main ()
{
	int t ;
	int t1 , t2 ;
	t =read();
	while( t -- )
	{
		bs = 0 ; lt = 0 ;
		n = read(); m = read();
		for( int i = 0 ; i < n ; i ++ )
		{
			a[i].br = read();
			bb[i] = a[i].br ;
			a[i].bh = i ;
			fa[i] = i;
			fir[i] = -1;
		}
		for( int i = 0 ; i < m ; i ++ )
		{
			t1 = read(); t2 = read();
			t1 -- ;
			t2 -- ;
			if( a[t1].br <= a[t2].br )
			{
				nxt[bs] = fir[t1] ;
				fir[t1] = bs ;
				zx[bs] = t2 ;
				bs ++ ;
			}
			if( a[t2].br <= a[t1].br )
			{
				nxt[bs] = fir[t2] ;
				fir[t2] = bs ;
				zx[bs] = t1 ;
				bs ++ ;
			}
		}
		sort( a , a + n , pd ) ;
		long long ans = 0 ;
		for( int i = 0 ; i < n ; i ++ )
		{
			lt ++ ;
			for( int j = fir[a[i].bh] ; j != -1 ; j = nxt[j] )
			{
				if( bb[zx[j]] < a[i].br ) continue ;
				int f1 = cx(a[i].bh), f2 = cx(zx[j]);
				if( f1 != f2 )
				{
					fa[f1] = f2 ;
					lt -- ;
				}
			}
			while( i < n - 1 && a[i + 1].br == a[i].br )
			{
				i ++ ;
				lt ++ ;
				for( int j = fir[a[i].bh] ; j != -1 ; j = nxt[j] )
				{
					if( bb[zx[j]] < a[i].br ) continue ;
					int f1 = cx(a[i].bh) , f2 = cx(zx[j]);
					if( f1 != f2 )
					{
						fa[f1] = f2 ;
						lt -- ;
					}
				}
			}
			if( i != n - 1 )
			{
				ans += (long long)lt * ( a[i].br - a[i + 1].br ) ;
			}
			else
			{
				ans += (long long)lt * a[i].br ;
			}
		}
		printf( "%lld\n" , ans ) ;
	}
	return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
3 2
3 2 3
1 2
2 3

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 273ms
memory: 4952kb

input:

10
90000 89999
29695126 280198164 879119046 430007344 949559367 73085127 889966040 364820814 284458985 931692411 799753826 296370022 333668874 420472873 151595337 343496667 39363958 831424981 863897406 650573429 217091529 533448388 789707546 749864138 599409565 909987504 320920370 476338054 11898738...

output:

15024049952546
16659615874110
400000005
400000003
5601933778666
6160080698685
5988219861596
6982527535131
6237837736440
6956893227831

result:

ok 10 lines