QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#67709#2831. Game TheoryYaoBIGAC ✓191ms17204kbC++175.5kb2022-12-11 07:58:192022-12-11 07:58:21

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-11 07:58:21]
  • 评测
  • 测评结果:AC
  • 用时:191ms
  • 内存:17204kb
  • [2022-12-11 07:58:19]
  • 提交

answer

#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); ++i)
#define revrep(i, a, n) for (auto i = n; i >= (a); --i)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
using namespace std;

template<class A, class B> string to_string(const pair<A, B> &p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string) s); }
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(bool x) { return x ? "true" : "false"; }
template<class A> string to_string(const A &v) {
	bool first = 1;
	string res = "{";
	for (const auto &x: v) {
		if (!first) res += ", ";
		first = 0;
		res += to_string(x);
	}
	res += "}";
	return res;
}
template<class A, class B> string to_string(const pair<A, B> &p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(const H& h, const T&... t) {
	cerr << " " << to_string(h);
	debug_out(t...);
}
#ifndef ONLINE_JUDGE
	#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
	#define debug(...) if (0) puts("No effect.")
#endif

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

using Info = pair<ll, ll>;
Info operator +(Info a, Info b) { return {a.first + b.first, a.second + b.second}; }

void InfoApply(Info &a, int l, int r, int x) {
	assert(x == 1);
	a.first = 1ll * (l + r) * (r - l + 1) / 2 - a.first;
	a.second = r - l + 1 - a.second;
}
void TagApply(int &a, int l, int r, int b) {
	assert(b == 1);
	a ^= 1;
}

/**
 * Author: Yuhao Yao
 * Date: 22-11-23
 * Description: Segment tree with lazy propogation.
 * Usage: Always define functions \textbf{InfoApply} and \textbf{TagApply} to tell segment tree how you apply modification.
 *  Combine is set as plus so if you just let T be numerical type then you have range sum in the info and as range query result. To have something different, say rangeMin, define a struct with constructer and + operation.
 * Time: O(\log N) per operation.
 * Status: tested on https://codeforces.com/gym/103371/problem/M.
 */
template<class Info, class Tag> class LazySegTree {
	#define ls i * 2
	#define rs i * 2 + 1

	int n;
	vector<Info> info;
	vector<Tag> tag;
public:
	LazySegTree(const vector<Info> &init): n(sz(init)) {
		assert(n > 0);
		info.resize(4 << __lg(n));
		tag.resize(4 << __lg(n));
		auto build = [&](auto &dfs, int i, int l, int r) {
			if (l == r) {
				info[i] = init[l];
				return;
			}
			int mid = (l + r) >> 1;
			dfs(dfs, ls, l, mid);
			dfs(dfs, rs, mid + 1, r);
			pull(i);
		};
		build(build, 1, 0, n - 1);
	}
private:
	void pull(int i) { info[i] = info[ls] + info[rs]; }

	template<class... T>
	void apply(int i, int l, int r, const T&... val) {
		InfoApply(info[i], l, r, val...);
		TagApply(tag[i], l, r, val...);
	}

	void push(int i, int l, int r) {
		if (tag[i] == Tag{}) return;
		int mid = (l + r) >> 1;
		apply(ls, l, mid, tag[i]);
		apply(rs, mid + 1, r, tag[i]);
		tag[i] = {};
	}
public:
	template<class... T>
	void rangeApply(int ql, int qr, const T&... val) {
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql) return;
			if (ql <= l && r <= qr) {
				apply(i, l, r, val...);
				return;
			}
			push(i, l, r);
			int mid = (l + r) >> 1;
			dfs(dfs, ls, l, mid);
			dfs(dfs, rs, mid + 1, r);
			pull(i);
		};
		dfs(dfs, 1, 0, n - 1);
	}

	Info rangeAsk(int ql, int qr) {
		Info res{};
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql) return;
			if (ql <= l && r <= qr) {
				res = res + info[i];
				return;
			}
			push(i, l, r);
			int mid = (l + r) >> 1;
			dfs(dfs, ls, l, mid);
			dfs(dfs, rs, mid + 1, r);
		};
		dfs(dfs, 1, 0, n - 1);
		return res;
	}

	int findFirst(int ql, int qr, function<bool(Info, int, int)> func) {
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql || (ql <= l && r <= qr && func(info[i], l, r) == 0)) return -1;
			if (l == r) return l;

			push(i, l, r);
			int mid = (l + r) >> 1;
			int res = dfs(dfs, ls, l, mid);
			if (res != -1) return res;
			else return dfs(dfs, rs, mid + 1, r);
		};
		return dfs(dfs, 1, 0, n - 1);
	};

	int findLast(int ql, int qr, function<bool(Info, int, int)> func) {
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql || (ql <= l && r <= qr && func(info[i], l, r) == 0)) return -1;
			if (l == r) return l;

			push(i, l, r);
			int mid = (l + r) >> 1;
			int res = dfs(dfs, rs, mid + 1, r);
			if (res != -1) return res;
			else return dfs(dfs, ls, l, mid);
		};
		return dfs(dfs, 1, 0, n - 1);
	};

	#undef ls
	#undef rs
};

int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int n, q; 
	while (cin >> n >> q) {
		string s; cin >> s;
		using Info = pair<ll, ll>;
		using Tag = int;
		vector<Info> init(n);
		rep(i, 0, n - 1) if (s[i] == '1') {
			init[i] = {i, 1};
		}
		LazySegTree<Info, Tag> seg(init);
		while (q--) {
			int l, r; cin >> l >> r;
			l--, r--;
			seg.rangeApply(l, r, 1);
			auto [sum, tot] = seg.rangeAsk(0, n - 1);
			ll ans = sum * 2 - 1ll * (tot - 1) * tot + tot;
			printf("%lld\n", ans % 998244353);
		}
	}
	return 0;
}

詳細信息

Test #1:

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

input:

3 2
010
1 2
2 3
5 1
00000
1 5

output:

1
3
5

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 3484kb

input:

1 1
0
1 1

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 60ms
memory: 3516kb

input:

2 2
01
2 2
2 2
2 2
01
1 2
1 2
1 2
1
1 1
1 1
1 2
1
1 1
1 1
2 2
00
1 2
1 2
2 2
11
1 2
1 2
2 2
01
2 2
1 1
2 2
10
2 2
1 2
2 2
01
1 2
1 2
1 2
0
1 1
1 1
2 2
01
1 2
2 2
1 2
0
1 1
1 1
1 2
1
1 1
1 1
2 2
10
1 2
1 1
1 2
0
1 1
1 1
2 2
01
1 2
1 2
2 2
10
1 2
1 1
1 2
0
1 1
1 1
1 2
0
1 1
1 1
1 2
1
1 1
1 1
2 2
10
1 ...

output:

0
3
1
3
0
1
0
1
2
0
0
2
0
1
2
0
1
3
1
0
1
2
1
0
0
1
3
2
1
0
1
3
3
2
1
0
1
0
0
1
0
1
0
2
2
1
0
1
2
1
1
0
2
0
2
3
1
0
0
1
2
0
0
1
0
1
0
1
1
0
1
2
2
0
0
2
0
1
0
1
1
0
1
0
1
0
0
1
0
1
0
1
2
0
3
0
1
0
0
1
1
0
1
0
1
2
0
2
1
0
0
3
1
2
0
1
3
2
1
0
0
1
2
0
2
0
0
1
0
1
1
0
1
0
1
0
1
0
1
2
1
0
3
1
0
3
0
1
0
1
...

result:

ok 200000 lines

Test #4:

score: 0
Accepted
time: 50ms
memory: 3508kb

input:

11 20
00010111000
1 6
1 11
5 6
8 11
1 4
3 8
4 11
1 7
5 9
1 4
6 10
5 6
1 7
5 10
1 10
9 11
6 8
1 4
8 11
1 4
10 20
0101000010
3 4
2 2
4 8
4 6
6 7
3 7
3 3
1 5
1 5
6 8
2 2
2 4
2 6
5 7
1 3
2 5
6 8
7 9
5 8
3 10
4 20
1011
2 4
1 4
1 3
2 3
1 1
2 2
1 2
2 4
1 2
3 4
3 4
3 4
1 4
2 2
1 4
1 3
1 1
1 3
1 3
2 2
4 20
1...

output:

16
55
53
25
13
15
43
52
41
33
34
40
43
45
35
28
25
33
45
37
19
20
35
38
36
41
36
29
36
29
22
31
20
23
28
20
21
10
14
30
2
10
5
7
10
9
7
2
0
10
0
10
2
1
9
6
7
4
7
8
4
9
1
6
8
3
5
7
3
7
6
8
6
9
6
7
2
0
5
3
66
105
83
68
92
137
92
76
85
92
127
120
119
104
124
65
70
88
61
53
40
43
25
21
32
59
67
32
29
50...

result:

ok 200000 lines

Test #5:

score: 0
Accepted
time: 63ms
memory: 3668kb

input:

11 200
11100010010
2 3
3 7
1 7
3 10
1 3
7 11
2 8
4 8
9 10
9 11
2 7
1 4
9 11
6 7
4 4
8 10
2 6
2 3
1 2
5 7
2 7
1 3
3 4
2 10
9 10
6 11
3 11
3 9
9 10
2 6
5 10
1 8
4 9
6 7
8 11
3 9
7 10
1 9
4 5
5 8
5 7
2 7
6 8
10 10
5 10
7 11
1 11
6 10
2 11
1 4
4 8
6 11
7 10
8 10
4 5
7 10
7 8
4 4
1 6
7 11
3 5
4 9
3 9
8 1...

output:

27
22
29
25
30
39
34
17
15
12
18
22
33
35
40
45
54
36
34
37
39
52
54
37
39
33
40
51
37
46
52
24
26
24
16
7
35
30
32
40
39
37
28
15
41
28
39
4
26
54
49
31
39
26
28
44
46
41
35
26
17
31
24
28
30
22
38
13
18
60
38
36
49
41
41
43
27
28
54
41
14
16
7
8
20
22
45
26
27
20
21
12
27
31
28
21
39
37
39
30
31
2...

result:

ok 200000 lines

Test #6:

score: 0
Accepted
time: 98ms
memory: 3720kb

input:

228 2000
111000100100000001001101001100110001011110001110001000101110101100010011011111000001011011000110111010111101011000010010000111111111100011111011011100111010011100111001010011110110011000100100110011111000110001111001010011001010
23 112
24 165
103 162
86 203
99 204
114 217
55 132
168 184
110...

output:

13974
13044
13700
13434
14000
13220
13546
13913
13184
13533
13051
13689
13533
14119
14470
13742
13980
13022
13167
13648
13592
13159
13028
13041
12964
12996
12792
12539
12039
11974
12336
12742
12841
12831
12730
12004
12065
12352
13037
11923
12332
14242
13475
13935
13121
12996
12755
13353
13720
13043
...

result:

ok 200000 lines

Test #7:

score: 0
Accepted
time: 132ms
memory: 5252kb

input:

20000 20000
110010001110111001001001011110011011011011101011010000011100100100110000011111000010010101001001110101010000011010101110001100111010000101000010110100011111001100001110011011001011010000100110010001000101111101001011100110101111110110100001100100110100000100100001011100111011000101111010...

output:

99977542
98746996
98989557
99015753
98938270
98605428
99504163
99699325
101118187
100862580
99993292
99728608
101006398
100940632
100522798
99799311
99585772
100035886
99976445
99131130
99309148
99370536
100535551
99600860
99595679
99735286
99976663
100435885
100334687
100103891
100055339
100160376
...

result:

ok 200000 lines

Test #8:

score: 0
Accepted
time: 179ms
memory: 16620kb

input:

200000 200000
1101110010111101001110011011011100110000111011010010001110010111010000111100010101100111111010010100111110001001011000010110000100111111011110101101011011011101000010000110010011001011101000000100010110001100001010110010011101010100000011101111001110001110110000101101111011111101000101...

output:

25901941
994525462
2814500
3463763
9230330
28640886
26981481
14997778
13823451
6099927
24702020
21362157
11973234
13511974
21653636
33119640
40350328
46109651
29192352
43766507
30728437
28870157
45189617
4217943
13558652
35410757
20571398
40319106
40216880
18748167
997839045
17848827
33465234
615861...

result:

ok 200000 lines

Test #9:

score: 0
Accepted
time: 170ms
memory: 16536kb

input:

200000 200000
1110111011100101000100110100111011011011111011111100110000001011010001010110001000101110101100100010100001110011111110000000100101100100011011000001110001100100011110000001111011111001101101110001100000101001000001000001101111011001101010100011100100100001000100111010110111101110000100...

output:

2996976
988387591
974626789
985502887
993615462
990275063
19203830
35696796
34289774
7267993
22056809
5091142
997828555
1539157
997787283
4900319
987896873
997340091
993638482
646650
995227298
17206077
22155672
27082357
4722230
24086385
35208979
32166288
35436320
985667659
997480097
988679642
694097...

result:

ok 200000 lines

Test #10:

score: 0
Accepted
time: 180ms
memory: 16612kb

input:

200000 200000
0110001110111111001111110001001110110111001111111100011110111100100110111100010100111010100110000100001110011111011011010111111001101110001011110010011111010000111101010110110111010110100100010011110001101101101101110000000010100010100111110000101111011000110000101000010001010110111111...

output:

988220745
991618043
995353102
43218945
966357434
971782588
948018714
980810727
27797113
53021088
23781501
39601625
967254286
975505074
976134175
73222692
979117703
64837391
50012902
73270693
19854546
7138045
60686015
71233145
20864017
62206852
50231821
50375154
47803471
28033289
29278344
35572675
14...

result:

ok 200000 lines

Test #11:

score: 0
Accepted
time: 191ms
memory: 16600kb

input:

200000 200000
1001110010001000011010101100000101100000111011101001101000000110010101101111110100011111001011011100001101001110100111010000000010001111001011011010000101010111101110111111100011010001110001001110011111011010010101101111100110000110101100100000011110110110011100111011101000010110100010...

output:

15407731
14631678
10186954
10345130
23847449
19860104
10808937
982977033
980356736
962694321
979467129
46038861
44085763
996388027
994522500
992200054
985014110
986398855
18482220
31196285
985715396
982645871
978310933
985212490
989962508
726523
21528316
18120979
25301777
8202316
6324376
995798193
9...

result:

ok 200000 lines

Test #12:

score: 0
Accepted
time: 179ms
memory: 16532kb

input:

200000 200000
0010011111010000110101000011110010101101110011000001000111010010100000100101101000011110010001111010000010101000000010100110011010010110011110110010101111100010001000111000001010010110111000110111000000011101101001011100011111110001100001111100010101001101101000101101101100100110010001...

output:

15506154
19348083
15746021
14628980
32334683
33654933
36224089
50873722
9847255
40651090
42120167
41528380
22636212
13786942
11412334
11873979
18281213
26693110
36507317
24846136
989656082
984635105
9059232
2580548
23212772
22950175
46305459
6758484
6053136
37780773
40099104
55511708
22877321
339317...

result:

ok 200000 lines

Test #13:

score: 0
Accepted
time: 165ms
memory: 16616kb

input:

200000 200000
1011010011001010111111001110110111000000000100100101011011100101000111010100010101000001010011111111111101000000100110110000110011110101011110101111100101000110010000001110000110111110111001000100100000111000000010101111011011111000110111111101011011110010001110110110100100010101011010...

output:

4606987
990360003
992738299
986383034
993014365
987669948
995664674
988276581
988873616
976938498
979101136
985430484
2565996
19548107
13130299
993838803
972233325
995501405
12769099
988692913
14410100
21699482
977804151
983846398
993716775
2571363
16441249
15782766
15593059
23323619
979633325
27776...

result:

ok 200000 lines

Test #14:

score: 0
Accepted
time: 176ms
memory: 17132kb

input:

200000 200000
1100101010110000111100011011010000101100000100011001101101111011100111111110001010101000110101001001100110111011100001100101110111101110010010001110001001011011011011011101101111011101110100110101110111111100101111111100110000011001111110100011110000011111111110100000000000100111101000...

output:

2393949
992730082
972611510
970251279
989437013
985877121
982257978
982959060
987600156
45641948
30059952
979943946
986888253
981389778
978967476
978126848
980481621
17817645
60426483
45758808
16835699
16586467
13373729
6741689
964511911
977717722
991981395
990826974
978075401
19704839
7193456
45368...

result:

ok 200000 lines

Test #15:

score: 0
Accepted
time: 172ms
memory: 17204kb

input:

200000 200000
0101100111101010110100110110100100001101111000110011010011111110101000010100100111110000100101101111001001010101000000110011001110110100000111100010100011101011001111001010101101100001101100110110000011111001000001001111011101000100001011000011111011101101101010110011000010111100111001...

output:

10297534
980029753
30272619
49375218
50632169
69574288
6797467
975065422
62087979
981950427
49855469
57837339
22715009
83393559
78649052
74244695
75889469
62421920
77415773
6572226
40586704
34350302
19139901
19264382
4935761
48990001
32994216
48972163
33566566
8869513
14871104
22941027
28170197
1087...

result:

ok 200000 lines

Test #16:

score: 0
Accepted
time: 171ms
memory: 16732kb

input:

200000 200000
0100011011111000010011111001000011100011101001010111011000101001001001111010111001110001101111111000110111111111101110110101000011010111000111010001101001000110000101001101000001100010101101000000010001001100001100111100111110101100000110001110011001010000001110000101001110001100000010...

output:

47026902
40495162
27149747
33363453
30911117
26108909
30730419
42927718
7603813
6364035
31042501
785203
974526721
974629452
974483169
24562058
31743526
24682968
19166722
11218277
996640683
32644571
28773258
17356726
17787261
18074809
67670633
37296794
26805347
30084028
30261128
31216228
15159508
378...

result:

ok 200000 lines

Test #17:

score: 0
Accepted
time: 191ms
memory: 16664kb

input:

200000 200000
1010010110110010011001001100110110001110001111111111100110111111101100010000000100101100111101011110111010110110111011101111001011001010010011011101000111110011001100111011001000000010100010110001100100001011110010000111100010101111011101011110010010101011111010010011111010101110010001...

output:

64855899
37247247
55246159
17086703
12325666
20301217
976171122
960745510
423388
11055803
30095418
973334427
973479528
975055740
984907581
992422626
20482449
7416938
9353933
17514662
14818478
18430532
19184520
23370654
7214842
991356723
25528201
28366255
41290790
38524173
45023405
36380190
37693504
...

result:

ok 200000 lines