Submission #240273

# Submission time Handle Problem Language Result Execution time Memory
240273 2020-06-19T06:04:45 Z frodakcin Fortune Telling 2 (JOI14_fortune_telling2) C++11
100 / 100
680 ms 58904 KB
#include <cstdio>
#include <cstring>
#define NDEBUG
#include <cassert>
#include <algorithm>
#include <vector>
#include <functional>

using ll = long long;
const int MN = 2e5+10, MK = 2e5+10, MV = MN*2+MK, MX = 1.5e7;//MX = 1.5e7

int s[MX], c[MX][2], X=1, v[MV], V, a[MN], b[MN], t[MK], N, K, n[MV], r[MN];
struct evt{public:int v, id;bool operator < (evt o)const{return v>o.v;}};//reversed sort
std::vector<evt> q;
ll f;

int upd(int n, int l, int r, int q, int v)
{
	++X;
	assert(X<MX);
	if(n)
	{
		memcpy(c[X], c[n], 8);
		s[X]=s[n];
	}
	n=X;
	s[n]+=v;
	if(r-l>1)
	{
		int m=l+(r-l)/2;
		if(q<m) c[n][0]=upd(c[n][0], l, m, q, v);
		else c[n][1]=upd(c[n][1], m, r, q, v);
		assert(s[n] == s[c[n][0]] + s[c[n][1]]);
	}
	return n;
}
int diff(int n, int u, int l, int r)
{
	if(r-l>1)
	{
		int m=l+(r-l)/2;
		if(s[c[n][1]] != s[c[u][1]]) return diff(c[n][1], c[u][1], m, r);
		else return diff(c[n][0], c[u][0], l, m);
	}
	else
		return l;
}
int qry(int n, int l, int r, int ql, int qr)
{
	if(!n) return 0;
	if(ql <= l&&r <= qr) return s[n];
	else
	{
		int m=l+(r-l)/2, f=0;
		if(ql<m) f+=qry(c[n][0], l, m, ql, qr);
		if(m<qr) f+=qry(c[n][1], m, r, ql, qr);
		return f;
	}
}
int main()
{
	scanf("%d%d", &N, &K);
	for(int i=0;i<N;++i)
	{
		scanf("%d", a+i), v[V++]=a[i];
		scanf("%d", b+i), v[V++]=b[i];
	}
	for(int i=0;i<K;++i) scanf("%d", t+i), v[V++]=t[i];
	std::sort(v, v+V);
	V = std::unique(v, v+V)-v;
	//for(int i=0;i<V;++i) printf("%d%c", v[i], " \n"[i+1==V]);
	for(int i=0;i<K;++i)
		t[i]=std::lower_bound(v, v+V, t[i])-v, q.push_back({t[i], i});
	std::sort(q.begin(), q.end());
	r[V]=1;
	for(int i=V-1,j=0;i>=0;--i)
	{
		r[i]=r[i+1];
		for(;j<K&&q[j].v==i;++j)//greater or equ
			r[i] = upd(r[i], 0, K, q[j].id, 1);
		//printf("NUM: %d: %d\n", v[i], s[r[i]]);
	}
	for(int i=0;i<N;++i)
	{
		a[i]=std::lower_bound(v, v+V, a[i])-v;
		b[i]=std::lower_bound(v, v+V, b[i])-v;
		//printf("TESTALL(%d,%d): %d %d\n", v[a[i]], v[b[i]], s[r[a[i]]], s[r[b[i]]]);
		if(s[r[a[i]]] == s[r[b[i]]])
			if(s[r[a[i]]]&1)
				f+=v[b[i]];
			else
				f+=v[a[i]];
		else
		{
			assert(a[i]!=b[i]);
			int t=diff(r[a[i]], r[b[i]], 0, K), u=t+1<K?qry(r[a[i]], 0, K, t+1, K):0;//for finding u, result should be same for r[a[i]] and r[b[i]]
			//greater one is selected, then perform u queries
			//printf("%d: %d %d\n", i, t, u);
			if(b[i]>a[i])++u;
			if(u&1)
				f+=v[b[i]];
			else
				f+=v[a[i]];
		}
	}
	printf("%lld\n", f);
	return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:65:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i), v[V++]=a[i];
   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
fortune_telling2.cpp:66:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", b+i), v[V++]=b[i];
   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
fortune_telling2.cpp:68:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0;i<K;++i) scanf("%d", t+i), v[V++]=t[i];
                       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 6 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 512 KB Output is correct
8 Correct 6 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 6 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 512 KB Output is correct
8 Correct 6 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 21 ms 2560 KB Output is correct
15 Correct 41 ms 4980 KB Output is correct
16 Correct 64 ms 8180 KB Output is correct
17 Correct 86 ms 10992 KB Output is correct
18 Correct 86 ms 10992 KB Output is correct
19 Correct 84 ms 10992 KB Output is correct
20 Correct 90 ms 11120 KB Output is correct
21 Correct 78 ms 10996 KB Output is correct
22 Correct 59 ms 10352 KB Output is correct
23 Correct 59 ms 10352 KB Output is correct
24 Correct 59 ms 10352 KB Output is correct
25 Correct 59 ms 10524 KB Output is correct
26 Correct 74 ms 10608 KB Output is correct
27 Correct 93 ms 10992 KB Output is correct
28 Correct 67 ms 10864 KB Output is correct
29 Correct 81 ms 10992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 6 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 512 KB Output is correct
8 Correct 6 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 21 ms 2560 KB Output is correct
15 Correct 41 ms 4980 KB Output is correct
16 Correct 64 ms 8180 KB Output is correct
17 Correct 86 ms 10992 KB Output is correct
18 Correct 86 ms 10992 KB Output is correct
19 Correct 84 ms 10992 KB Output is correct
20 Correct 90 ms 11120 KB Output is correct
21 Correct 78 ms 10996 KB Output is correct
22 Correct 59 ms 10352 KB Output is correct
23 Correct 59 ms 10352 KB Output is correct
24 Correct 59 ms 10352 KB Output is correct
25 Correct 59 ms 10524 KB Output is correct
26 Correct 74 ms 10608 KB Output is correct
27 Correct 93 ms 10992 KB Output is correct
28 Correct 67 ms 10864 KB Output is correct
29 Correct 81 ms 10992 KB Output is correct
30 Correct 234 ms 50660 KB Output is correct
31 Correct 311 ms 52352 KB Output is correct
32 Correct 412 ms 54632 KB Output is correct
33 Correct 579 ms 58852 KB Output is correct
34 Correct 233 ms 50148 KB Output is correct
35 Correct 578 ms 58724 KB Output is correct
36 Correct 575 ms 58856 KB Output is correct
37 Correct 680 ms 58852 KB Output is correct
38 Correct 572 ms 58724 KB Output is correct
39 Correct 608 ms 58856 KB Output is correct
40 Correct 479 ms 58676 KB Output is correct
41 Correct 631 ms 58840 KB Output is correct
42 Correct 582 ms 58904 KB Output is correct
43 Correct 376 ms 58088 KB Output is correct
44 Correct 353 ms 58084 KB Output is correct
45 Correct 346 ms 58212 KB Output is correct
46 Correct 342 ms 55780 KB Output is correct
47 Correct 363 ms 55140 KB Output is correct
48 Correct 466 ms 58204 KB Output is correct
49 Correct 430 ms 58084 KB Output is correct