QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#591735#6837. AC Automatoncyj888TL 6993ms91160kbC++116.2kb2024-09-26 17:28:562024-09-26 17:28:57

Judging History

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

  • [2024-09-26 17:28:57]
  • 评测
  • 测评结果:TL
  • 用时:6993ms
  • 内存:91160kb
  • [2024-09-26 17:28:56]
  • 提交

answer

#include <bits/stdc++.h>
#define ott(i,l,r) for (register int i = l; i <= r; ++ i)
#define read(x) {x = 0; char c; while ((c = getchar ()) < 48); do x = x * 10 + (c ^ 48); while ((c = getchar ()) > 47);}
#define print() {long long x = res; Z = 0; if (!x) putchar (48); while (x) pc[++ Z] = x % 10, x /= 10; while (Z) putchar (pc[Z --] + 48); putchar ('\n');}
#define cdn1(ttu) {int tu, u = ttu; while (u) {if (u <= LIM) {tu = val[u]; if (!(ch[tu] ^ '?')) res -= Max (dn[tu] - up[tu]); ++ dn[tu]; if (ch[tu] == 'A') ++ res; if (!(ch[tu] ^ '?')) res += Max (dn[tu] - up[tu]);} else b[u - LIM].add2 (), res += b[u - LIM].ac; u = FA[u];}}
#define cdn2(ttu) {int tu, u = ttu; while (u) {if (u <= LIM) {tu = val[u]; if (!(ch[tu] ^ '?')) res -= Max (dn[tu] - up[tu]); -- dn[tu]; if (ch[tu] == 'A') -- res; if (!(ch[tu] ^ '?')) res += Max (dn[tu] - up[tu]);} else b[u - LIM].add1 (), res -= b[u - LIM].ac; u = FA[u];}}
using namespace std;
const int N = 3e5 + 110, B = 820, inf = 0x3f3f3f3f;
int n, m, q, idx, Z, OTH, LIM;
long long res; char pc[65], ch[N];
vector <int> e[N], E[B << 2];
int up[N], dn[N], dfn[N], dep[N], fa[N], lg[N << 1], f[N << 1][20];
void dfs0 (int u) {
	f[dfn[u] = ++ idx][0] = u;
	for (int v : e[u]) {
		dfs0 (v);
		f[++ idx][0] = u;
	}
	return ;
}
inline int lca (int x, int y) {
	x = dfn[x], y = dfn[y]; if (x > y) swap (x, y);
	Z = lg[y - x + 1], x = f[x][Z], y = f[y - (1 << Z) + 1][Z];
	return dep[x] < dep[y] ? x : y;
}
struct OPT {
	int x; char c;
} a[B + 1];
struct BLOCK {
	int ac, now, val, sum;//ac = 'A'count
	vector <int> s; vector <pair <int, int> > t;
	inline void upd (int i) {
		if (!(ch[i] ^ 'A')) ++ ac;
		if (!(ch[i] ^ '?')) {
			i = dn[i] - up[i];
			if (i >= 0) ++ sum;
			if (-m <= i && i <= m) s.emplace_back (i);
		}
		return ;
	}
	inline void init () {
		t.emplace_back (-inf, 0);
		if (s.size ()) {
			sort (s.begin (), s.end ());
			ott (i, 0, s.size () - 1) {
	        	int cnt = 1; while (i + 1 < s.size () && !(s[i] ^ s[i + 1])) ++ i, ++ cnt;
	        	t.emplace_back (s[i], cnt);
			}
		}
		t.emplace_back (inf, 0);
		int l = 0, r = t.size () - 1, mid;
		while (l <= r) {
			mid = l + r >> 1;
			if (t[mid].first >= 0) now = mid, r = mid - 1;
			else l = mid + 1;
		}
		return ;
	}
	inline void add1 () {
		if (!(t[now].first ^ val ++)) sum -= t[now ++].second; res -= sum;
		return ;
	}
	inline void add2 () {
		res += sum; if (!(t[now - 1].first ^ -- val)) sum += t[-- now].second;
		return ;
	}
	inline void clear () {
		ac = now = val = sum = 0, vector <int> ().swap (s), vector <pair <int, int> > ().swap (t);
		return ;
	}
} b[B << 1 | 1];
int id[N], val[B << 2], FA[B << 2];
inline const int Max (const int &x) {
	return x > 0 ? x : 0;
}
inline const bool cmp (const int &x, const int &y) {
	return dfn[x] < dfn[y];
}
inline void push (int u) {
	b[OTH].upd (u); for (int v : e[u]) push (v); 
	return ;
}
inline void find (int x) {
	if (id[fa[x]]) {
		val[Z = id[x] = ++ idx] = x, Z -= LIM;
		return ;
	}
	find (fa[x]), b[Z].upd (fa[x]);
	for (int v : e[fa[x]]) {
		if (!(v ^ x)) continue;
		if (!OTH) val[OTH = id[v] = ++ idx] = v, OTH -= LIM;
		push (v);
	}
	return ;
} 
inline void red (int u) {
	dn[u] = 0;
	for (int v : e[u]) {
		up[v] = up[u]; if (ch[u] ^ 'C') ++ up[v];
		red (v);
		dn[u] += dn[v]; if (ch[v] ^ 'A') ++ dn[u];
	}
	if (!(ch[u] ^ 'A')) res += dn[u];
	if (!(ch[u] ^ '?')) res += Max (dn[u] - up[u]);
	return ;
}
int t[N], l, r;
inline void cup1 (int u) {
	t[l = r = 1] = u; int tv; while (l <= r) {
		for (int v : E[t[l]]) {
			if (v <= LIM) {
				tv = val[v];
				if (!(ch[tv] ^ '?')) res -= Max (dn[tv] - up[tv]);
				++ up[tv];
				if (!(ch[tv] ^ '?')) res += Max (dn[tv] - up[tv]);
			}
			else b[v - LIM].add1 ();
			t[++ r] = v;
		}
		++ l;
	}
	return ;
}
inline void cup2 (int u) {
	t[l = r = 1] = u; int tv; while (l <= r) {
		for (int v : E[t[l]]) {
			if (v <= LIM) {
				tv = val[v];
				if (!(ch[tv] ^ '?')) res -= Max (dn[tv] - up[tv]);
				-- up[tv];
				if (!(ch[tv] ^ '?')) res += Max (dn[tv] - up[tv]);
			}
			else b[v - LIM].add2 ();
			t[++ r] = v;
		}
		++ l;
	}
	return ;
}
inline void sol () {
    ott (i, 1, m) val[i] = a[i].x;
	idx = m, sort (val + 1, val + 1 + idx), idx = unique (val + 1, val + 1 + idx) - val - 1, sort (val + 1, val + 1 + idx, cmp);
    ott (i, 2, idx) val[idx + i - 1] = lca (val[i - 1], val[i]); 
	val[idx += idx] = 1, sort (val + 1, val + 1 + idx), idx = unique (val + 1, val + 1 + idx) - val - 1;
    ott (i, 1, idx) id[val[i]] = i; LIM = idx;
    ott (i, 2, LIM) {
    	if (!id[fa[val[i]]]) {
    		OTH = 0, find (val[i]), Z += LIM, E[FA[Z] = id[fa[val[Z]]]].emplace_back (Z), E[FA[i] = Z].emplace_back (i);
    		if (OTH) OTH += LIM, E[FA[OTH] = id[fa[val[Z]]]].emplace_back (OTH);
    		
		}
		else E[FA[i] = id[fa[val[i]]]].emplace_back (i);
	}
	ott (i, 1, LIM) {
		OTH = 0; for (int v : e[val[i]]) {
			if (id[v]) continue;
			if (!OTH) val[OTH = id[v] = ++ idx] = v, E[FA[OTH] = i].emplace_back (OTH), OTH -= LIM;
			push (v);
		}
	}
	Z = idx - LIM; ott (i, 1, Z) b[i].init ();
	ott (i, 1, m) {
		int x = a[i].x; char c = a[i].c;
		if (ch[x] ^ 'C') cup2 (id[x]); if (ch[x] ^ 'A') cdn2 (FA[id[x]]);
		if (!(ch[x] ^ 'A')) res -= dn[x];
		if (!(ch[x] ^ '?')) res -= Max (dn[x] - up[x]);
		ch[x] = c;
		if (!(ch[x] ^ 'A')) res += dn[x];
		if (!(ch[x] ^ '?')) res += Max (dn[x] - up[x]);
		if (ch[x] ^ 'C') cup1 (id[x]); if (ch[x] ^ 'A') cdn1 (FA[id[x]]);
		print ();
	}
	ott (i, 1, idx) E[i].clear (), FA[i] = 0, id[val[i]] = 0, val[i] = 0;
	Z = idx - LIM, m = idx = 0, res = 0, red (1); ott (i, 1, Z) b[i].clear ();
	return ;
}
int main () {
	//freopen ("data.in", "r", stdin); freopen ("res.out", "w", stdout);
	read (n); read (q); ott (i, 1, n) ch[i] = getchar (); ott (i, 2, n) {
		read (fa[i]); e[fa[i]].emplace_back (i), dep[i] = dep[fa[i]] + 1;
	}
	dfs0 (1), red (1);
	ott (j, 1, 19) 
		ott (i, 1, idx - (1 << j) + 1) 
			OTH = f[i][j - 1], LIM = f[i + (1 << j - 1)][j - 1], f[i][j] = dep[OTH] < dep[LIM] ? OTH : LIM;
	lg[0] = -1;	ott (i, 1, idx) lg[i] = lg[i >> 1] + 1; idx = 0;
	int x; char c; ott (i, 1, q) {
		read (x); c = getchar ();
		if (!(i % B) || !(i ^ q)) a[m = i % B ? i % B : B] = {x, c}, sol ();
		else a[i % B] = {x, c};
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 20132kb

input:

5 3
AC??C
1 2 2 1
1 ?
4 A
2 ?

output:

4
3
3

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 20216kb

input:

1000 1000
AC?ACCCCC?CCA??CCCCC?A?AC?C??C?ACCCACC?C?CCC?CACACA???????C?????CC?C?AAAAACCA??AAAACACACA???AACAC?CCC?CAC?AAAACCAC???ACAA??A??A?CA?A?ACACACCAA?ACACA?AC??AC??ACAAACCC??CAA?A???CC?AA??AC???A?CCCC??CACCAACC?AA?C?AAACCCA?AAA??C?CC??CACCACAACCA?AACCC?A?CCC?CC???AA?CACCCAAC??CAA??C?AA??CA???CAAA...

output:

2344
2345
2342
2342
2768
2768
2772
2772
2772
2772
2772
2772
2772
2767
2767
2767
2767
2764
2766
2766
2769
2765
2761
2764
2767
2772
2772
2772
2772
2772
2772
2777
2777
2777
2777
2774
2771
2774
2782
2778
2778
2772
2768
2772
2772
2772
2772
2772
2774
2774
2778
2781
2781
2779
2782
2784
2787
2782
2786
2788
...

result:

ok 1000 lines

Test #3:

score: 0
Accepted
time: 6936ms
memory: 91160kb

input:

300000 300000
AAA?CA?AA?AC?A?CCA?AACCAAA???CA?ACCAACCCCAACAAA?CCAAAC?A?C??CC?C?C?CCCA?CAA?ACA??C?C?AC??CA??ACA?AA???CACAAA?CACCCCCCC?A?AAAAAC?AACCA????CCC?C?AAACCCAA?C???CCCC?AAACAAA???A?CAAC??A??A??CCCC??AA?C??ACA?AACAAA????CAA???AAAAACC?C?CCA?CCAA?AAC?CC?CA?A??CC??CCAC??C??????AAC?AA?AA?AAC?C??AAC...

output:

14995917235
14995917235
14996064601
14996083631
14995980103
14995925797
14995925797
14995925797
14995967213
14995967213
14995967213
14995876211
14995774037
14995774037
14995774037
14995876791
14995866113
14995756158
14995647554
14995647554
14995560537
14995560537
14995583619
14995583619
14995583619
...

result:

ok 300000 lines

Test #4:

score: 0
Accepted
time: 2265ms
memory: 69008kb

input:

300000 300000
?ACA???CCCA?C???AA??CAAAAACCC??A?CAC??C???????CAA?C?C?A?C???A?CC?CCAC?C?ACC??C?CAACA??CA?CA?CAACA??AACCC?CCCACACC?AAC?CA??C?C?CCCA?ACAA??AA?CCAACACCA?AC?C?CCCCCCAAA?CC??A?CCC???A?CA?ACAC???C??CCA??CCAA?AAC???CCCC??AA?C?C?C?CACAC?C?CA??AACC?A????C??CACAAAAA?C?CAACACA?ACCAC?A?CCCACACA??A...

output:

200180
200181
200182
200182
200182
200182
200182
200182
200183
200183
200183
200182
200183
200183
200183
200183
200183
200182
200183
200183
200183
200183
200183
200183
200183
200183
200184
200183
200182
200181
200180
200180
200180
200181
200181
200182
200181
200180
200180
200181
200182
200181
200182...

result:

ok 300000 lines

Test #5:

score: 0
Accepted
time: 6993ms
memory: 91048kb

input:

300000 300000
A??CCAAACAC?A?CCACA?CA??ACC?CCA?CCAACACAC?A?CCC??ACC?ACC?CA?CA?C??A?CACCCC?C?AC?AAC??A???CA?C???AC?A?A?ACCCAACC?AA?CCACCCAAAA????C?ACC?????ACACA?C?A?CCC?A?AC????AC?C?A???ACA??CAACACC????CAA???ACCAC???CCCA?A?CAA?C??CCCCA?ACCA?A?CCC?ACA?C??AA?C??ACA?AAACC?CCAACCCAC?CAAA??ACC?ACCAA??????A...

output:

15015050020
15015050020
15015135045
15015202340
15015303448
15015303448
15015282042
15015282042
15015310379
15015329461
15015377957
15015514924
15015514924
15015613521
15015724614
15015640441
15015598420
15015635210
15015635210
15015544590
15015671373
15015653717
15015706346
15015805421
15015835446
...

result:

ok 300000 lines

Test #6:

score: 0
Accepted
time: 1462ms
memory: 80028kb

input:

300000 300000
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
10018652160
...

result:

ok 300000 lines

Test #7:

score: -100
Time Limit Exceeded

input:

300000 300000
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891340
2619172
2891...

result: