QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#380484#6671. Zadatakltunjic#0 190ms271076kbC++144.0kb2024-04-07 04:43:402024-07-04 03:33:36

Judging History

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

  • [2024-07-04 03:33:36]
  • 评测
  • 测评结果:0
  • 用时:190ms
  • 内存:271076kb
  • [2024-04-07 04:43:40]
  • 提交

answer

#include <cstdio> 
#include <algorithm> 
#include <vector> 
#include <cstring> 
#include <set> 
#include <cassert>

#define X first
#define Y second
#define PB push_back

using namespace std; 

typedef long long ll;
typedef pair<int, int> pii;

const int LOG = 17;
const int N = 1 << LOG; 
const int PERZ = LOG * LOG * N;
const ll OO = 2e18; 
const bool DEB = 0;

struct node {
	ll sum, in;
	bool prop; 
	node *l, *r;
	node() {}
	node(ll sum, ll in, bool prop, node *l = NULL, node *r = NULL) : sum(sum), in(in), prop(prop), l(l), r(r) {}
};

int NEW;
node NEWN[PERZ];

node* get() { 
	assert(NEW < PERZ); 
	NEWN[NEW].l = NEWN[NEW].r = NULL;
	NEWN[NEW].sum = NEWN[NEW].in = NEWN[NEW].prop = 0;
	return &NEWN[NEW++]; 
}

void merge(node *u, node *v, node *w) {
	w->in = u->in + v->in;
	w->sum = u->sum + v->sum;
	w->prop = 0;
}

void child(node *u) {
	if(u->l == NULL) u->l = get(); 
	if(u->r == NULL) u->r = get();
}

node* _update(node *u) {
	node* v = get();
	v->l = u->l;
	v->r = u->r;
	v->in = u->sum - u->in;
	v->sum = u->sum;
	v->prop = u->prop ^ 1;
	return v;
}

void propagate(node *u) {
	if(!u->prop) return;
	assert(u->l != NULL && u->r != NULL);
	u->l = _update(u->l);
	u->r = _update(u->r);
	u->prop = 0;
}

node* update(int l, int r, node *u, int lo = 0, int hi = N) { assert(u != NULL);
	if(r <= lo || hi <= l) return u;
	if(l <= lo && hi <= r) return _update(u);
	propagate(u);
	int mi = (lo + hi) / 2;
	node *v = get();
	v->l = update(l, r, u->l, lo, mi);
	v->r = update(l, r, u->r, mi, hi);
	merge(v->l, v->r, v);
	return v;
}

ll query(int l, int r, node *u, int lo = 0, int hi = N) { assert(u != NULL);
	if(r <= lo || hi <= l) return 0;
	if(l <= lo && hi <= r) return u->in;
	propagate(u);
	int mi = (lo + hi) / 2;
	return query(l, r, u->l, lo, mi) + query(l, r, u->r, mi, hi);
}

vector<int> C; // DODAJ 0

node* build(int lo = 0, int hi = N) {
	node* u = get();
	u->prop = 0;
	if(lo + 1 == hi) {
		u->sum = lo < (int) C.size() - 1 ? (ll) C[lo + 1] * C[lo + 1] - C[lo] * C[lo] : 0;
		u->in = 0;
		u->l = u->r = NULL;
		if(DEB && lo < (int) C.size() - 1) printf("%lld ", u->sum);
		return u;
	}
	int mi = (lo + hi) / 2;
	u->l = build(lo, mi); 
	u->r = build(mi, hi);
	merge(u->l, u->r, u);
	return u;
}

int n, A[N];

int TO[N]; 
node* RT[N];
set<int> S[N]; 

void debug(node *u, bool f = 0, int lo = 0, int hi = N) {
	f ^= u != NULL ? u->prop : 0;
	if(lo + 1 == hi) {
		printf("%d ", (int) f);
		return;
	}
	int mi = (lo + hi) / 2;
	debug(u != NULL ? u->l : NULL, f, lo, mi);
	debug(u != NULL ? u->r : NULL, f, mi, hi);
}

ll unija(int a, int b, int ind) {
	if(S[a].size() < S[b].size()) swap(a, b);
	TO[ind] = a;
	
	if(DEB) {
		printf("{"); for(int x : S[a]) printf("%d ", x); printf("}\n");
		printf("{"); for(int x : S[b]) printf("%d ", x); printf("}\n");
		printf("  "); debug(RT[a]); 
		printf("\n^ "); debug(RT[b]);
	}

	ll ret = 0;
	vector<int> TMP(S[b].begin(), S[b].end()); 
	for(int i = (int) TMP.size() - 1; i >= 0; i -= 2) {
		int r = TMP[i], l = i != 0 ? TMP[i - 1] : 0; /// |(l) .... |(r)    |(r + 1)
		ret += query(l, r, RT[a]);
	}

	for(int x : TMP) {
		RT[a] = update(0, x, RT[a]);
		if(S[a].find(x) == S[a].end()) S[a].insert(x);
		else S[a].erase(x);
	}
	
	if(DEB) {
		printf("\n= "); debug(RT[a], 0); printf("\n");
		printf("{"); for(int x : S[a]) printf("%d ", x); printf("}\n");
	}
	return ret;
}

int main() {
	C.PB(0); 
	
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", A + i); 
		C.PB(A[i]); 
		TO[i] = i;
	}

	sort(C.begin(), C.end());
	C.erase(unique(C.begin(), C.end()), C.end());

	RT[0] = build();
	if(DEB) printf("\n");

	for(int i = 1; i <= n; ++i) {
		int ind = lower_bound(C.begin(), C.end(), A[i]) - C.begin();
		RT[i] = update(0, ind, RT[0]);
		S[i].insert(ind);
		if(DEB) { debug(RT[i]); printf("\n"); }
	}

	for(int i = n + 1; i < 2 * n; ++i) {
		int a, b; scanf("%d%d", &a, &b); 
		printf("%lld\n", unija(TO[a], TO[b], i));
		if(DEB) printf("~~~\n");
	}
	return 0;
}


詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 7ms
memory: 45964kb

input:

5000
217378 945562 533764 323494 69148 240722 205370 463122 552700 31800 616898 678076 893816 258468 34822 905360 967562 731346 340940 584418 684926 785402 107584 995542 363278 255302 196912 870994 329464 338390 154870 977540 65120 130388 350020 239660 553428 710306 385138 633274 841672 740778 17929...

output:

366054863229156
845813550079248
220608254662448
199632201322896
932765903941712
0
86235112108644
562875762772004
0
44546450873540
677210937682436
236218133186960
289397219206144
373565151827008
620816245873664
0
255882209317700
77968987115008
18771655892864
25211771691280
0
396990751293092
795175396...

result:

wrong answer 1st lines differ - expected: '359872811236', found: '366054863229156'

Subtask #2:

score: 0
Wrong Answer

Test #27:

score: 0
Wrong Answer
time: 190ms
memory: 271076kb

input:

100000
590812 862538 815196 397712 773198 172122 270600 609324 841858 4868 597128 216378 982576 385590 842010 55844 671758 885088 577804 194248 229770 859754 274744 678176 607974 791062 607192 210234 863164 619708 804538 430978 237704 10512 840374 843732 875326 255462 970338 898540 925508 661464 413...

output:

6227033730770192
10136910750661632
1892464930877696
4334568799892496
152269036756900
440114537459868
2063963068383972
13438156516238804
23697424
1654294350105476
152329642371472
9069633683462844
1279026559007924
9478855531141284
23697424
6026904961938692
7076013048321096
5231129275894068
14862810460...

result:

wrong answer 1st lines differ - expected: '349058819344', found: '6227033730770192'

Subtask #3:

score: 0
Memory Limit Exceeded

Test #40:

score: 0
Memory Limit Exceeded

input:

65536
131908 883754 813278 197778 704074 981802 297078 903698 485360 496064 726120 251990 462786 129558 704500 920556 903884 454552 949354 328526 921462 975888 780002 276668 675308 49774 83014 136308 679916 42174 151084 358830 284218 259680 65684 526980 516764 200170 265060 294150 128046 658864 2984...

output:

45737326586384
157247804072772
7168714532842852
536220432963172
2349479606026496
328044381551844
43331530455524
7182583582294160
1926976191146560
723663889820612
16054960103295588
435775207557648
835701106500
10314812834596
1778646276
68484605073296
357528971813888
5020836189584
163394854164964
3817...

result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%