QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376279#6409. Classical Data Structure ProblemLainWA 590ms100816kbC++233.5kb2024-04-04 01:18:342024-04-04 01:18:35

Judging History

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

  • [2024-04-04 01:18:35]
  • 评测
  • 测评结果:WA
  • 用时:590ms
  • 内存:100816kb
  • [2024-04-04 01:18:34]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

struct LazySegTree {
	int n;
	vector<uint32_t> t;
	vector<uint32_t> lz;

	uint32_t comb(uint32_t a, uint32_t b) {
		return a + b;
	}
	void push(int node, int l, int r) {
		t[node] += uint32_t(1ULL*(r-l+1)*lz[node]);
		if (l != r) {
			rep(it, 0, 2) {
				lz[2*node + it] += lz[node];
			}
		}
		lz[node] = 0;
	}
	void pull(int node) {
		t[node] = comb(t[2*node], t[2*node+1]);
	}
	LazySegTree(int _n): n(_n) {
		t.resize(2*n, 0);
		lz.resize(2*n, 0);
	}

	void apply(int l, int r, uint32_t val, int node, int tl, int tr) {
		push(node, tl, tr);
		if (r < tl || tr < l) return;
		if (l <= tl && tr <= r) {
			lz[node] = val;
			push(node, tl, tr);
			return;
		}
		int tm = (tl + tr)/2;
		apply(l, r, val, 2*node, tl, tm);
		apply(l, r, val, 2*node+1, tm+1, tr);
		pull(node);
	}
	
	uint32_t query(int l, int r, int node, int tl, int tr) {
		push(node, tl, tr);
		if (r < tl || tr < l) return 0;
		if (l <= tl && tr <= r) return t[node];
		int tm = (tl + tr)/2;
		return query(l, r, 2*node, tl, tm) + query(l, r, 2*node+1, tm+1, tr);
	}

	void apply(int l, int r, int val) {
		assert(l <= r);
		apply(l, r, val, 1, 0, n-1);
	}
	uint32_t query(int l, int r) {
		assert(l <= r);
		return query(l, r, 1, 0, n-1);
	}
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  const int MASK = (1<<30) - 1;
  const int ST = 1<<22;
  int n, m;
  cin >> n >> m;
  int mod_mask = (1<<m)-1;
  map<int, map<int, int>> blocks;
  int block_size = max((1<<m)/ST, 1);
  LazySegTree T(ST);
  uint32_t accum = 0;

  auto add_range = [&](int block, int l, int r, uint32_t val) {
	  uint32_t add = 1ULL*val*(r-l+1);
	  T.apply(block, block, add);

	  auto it = blocks.try_emplace(block).first;
	  auto& mp = it->second;
	  mp[l] += val;
	  if (r+1 < block_size)
		  mp[r+1] -= val;
  };

  auto process_block = [&](int block, int l, int r) {
	  accum += T.query(block, block);

	  auto it = blocks.find(block);
	  if (it == blocks.end()) return;
	  auto& mp = it->second;
	  uint32_t p = 0;
	  rep(i, 0, block_size) {
		  auto it = mp.find(i);
		  if (it != mp.end()) {
			  p += it->second;
		  }
		  if (i >= l && i <= r) {
			  accum += p;
		  }
		  accum -= p;
	  }
  };

  rep(i, 1, n+1) {
	  int l, r;
	  cin >> l >> r;
	  l = (l + accum)&mod_mask;
	  r = (r + accum)&mod_mask;
	  if (l > r) swap(l, r);
	  // cout << l << " " << r << " " << accum << '\n';

	  if ((1<<m) < ST) {
		  // Block size is always 1.
		  T.apply(l, r, i);
		  accum += T.query(l, r);
		  continue;
	  } 

	  int lblock = l/block_size, rblock = r/block_size;
	  int lidx = l%block_size, ridx=  r%block_size;

	  // Handle the edge case where they are in the same block.
	  if (lblock == rblock) {
		  add_range(lblock, lidx, ridx, i);
		  process_block(lblock, lidx, ridx);
		  continue;
	  }

	  // Handle edge blocks
	  add_range(lblock, lidx, block_size - 1, i);
	  add_range(rblock, 0, ridx, i);
	  process_block(lblock, lidx, block_size - 1);
	  process_block(rblock, 0, ridx);
	  lblock++;
	  rblock--;

	  // Handle mid-blocks
	  if (lblock <= rblock) {
		  uint32_t add = uint32_t(1ULL*block_size*i);
		  T.apply(lblock, rblock, add);
		  accum += T.query(lblock, rblock);
	  }
  }
  cout << (accum&MASK) << '\n';
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 68580kb

input:

5 2
2 1
1 3
3 2
1 0
0 2

output:

87

result:

ok 1 number(s): "87"

Test #2:

score: 0
Accepted
time: 4ms
memory: 68580kb

input:

1 1
1 1

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 4ms
memory: 68580kb

input:

1 2
3 1

output:

3

result:

ok 1 number(s): "3"

Test #4:

score: 0
Accepted
time: 3ms
memory: 68740kb

input:

1 5
31 15

output:

17

result:

ok 1 number(s): "17"

Test #5:

score: 0
Accepted
time: 3ms
memory: 68736kb

input:

1 20
366738 218765

output:

147974

result:

ok 1 number(s): "147974"

Test #6:

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

input:

1 30
86669484 41969116

output:

44700369

result:

ok 1 number(s): "44700369"

Test #7:

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

input:

10 5
20 31
2 2
14 18
13 25
26 4
22 9
15 9
19 16
15 27
9 20

output:

1414

result:

ok 1 number(s): "1414"

Test #8:

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

input:

100 10
245 987
817 813
743 560
548 504
116 479
223 199
775 998
126 542
791 823
96 318
69 349
0 584
245 601
119 513
93 820
115 307
838 978
249 767
433 287
240 8
22 683
169 720
395 592
903 866
919 652
510 563
858 345
938 250
550 239
981 7
784 926
212 644
272 365
490 871
470 987
571 53
65 593
515 370
1...

output:

20383734

result:

ok 1 number(s): "20383734"

Test #9:

score: 0
Accepted
time: 7ms
memory: 68560kb

input:

1000 1
0 1
1 0
1 0
1 0
1 0
0 1
0 0
0 1
0 0
0 0
1 1
0 0
1 0
0 1
0 1
1 1
0 1
0 1
0 0
1 1
1 0
0 1
1 1
0 0
0 1
0 0
1 1
1 0
1 1
1 1
0 0
1 1
1 0
0 0
0 1
1 1
0 1
0 0
1 1
1 1
0 1
0 0
0 1
0 1
1 1
1 0
1 0
1 1
1 1
1 0
1 1
1 0
1 1
1 1
1 0
0 1
0 0
0 1
0 0
0 0
1 1
0 1
1 1
0 0
0 1
0 1
1 0
0 1
0 0
0 0
0 0
1 1
1 1
1...

output:

190098735

result:

ok 1 number(s): "190098735"

Test #10:

score: 0
Accepted
time: 3ms
memory: 68560kb

input:

1000 5
8 18
31 28
19 3
15 28
5 22
19 1
26 27
17 17
5 26
6 27
10 6
5 2
3 19
6 6
28 16
17 16
0 21
7 31
13 25
13 10
28 30
0 13
21 5
2 9
25 28
4 18
31 13
1 26
30 3
5 8
19 16
22 10
15 17
3 25
6 27
18 26
27 1
26 29
18 21
14 20
5 1
26 9
7 13
19 25
15 11
24 17
12 28
24 17
4 27
20 27
31 18
25 17
1 28
5 0
16 ...

output:

794181769

result:

ok 1 number(s): "794181769"

Test #11:

score: 0
Accepted
time: 3ms
memory: 68708kb

input:

1000 10
732 399
190 578
491 867
330 55
113 400
34 734
790 927
201 156
107 359
790 398
401 523
634 505
570 305
281 513
551 35
610 33
231 330
833 756
213 444
412 315
139 165
533 21
35 977
319 884
894 72
149 667
16 538
282 860
850 550
100 99
801 138
159 34
468 852
840 853
368 994
469 906
393 298
464 89...

output:

755182648

result:

ok 1 number(s): "755182648"

Test #12:

score: 0
Accepted
time: 7ms
memory: 68632kb

input:

5000 15
7705 10737
21186 31441
10307 825
19372 1969
32358 6343
22487 31897
12802 25210
17920 4297
5726 8409
28174 12489
16532 12646
9916 14917
19592 26927
23987 9279
26951 31081
3673 10505
20727 10730
28961 26581
11005 29624
13931 32180
29764 19108
23553 28977
30178 6537
25586 3041
15333 31927
4671 ...

output:

374742544

result:

ok 1 number(s): "374742544"

Test #13:

score: 0
Accepted
time: 15ms
memory: 68564kb

input:

10000 20
914013 637387
162942 785196
55799 893293
359726 714014
109456 559963
689320 161360
164737 25370
260018 436870
801394 34900
564741 620583
1008448 956143
788249 695007
633673 1020122
431990 397822
253241 746513
322933 927863
843120 378180
343689 583409
788822 249760
839003 753443
910418 20908...

output:

719391110

result:

ok 1 number(s): "719391110"

Test #14:

score: -100
Wrong Answer
time: 590ms
memory: 100816kb

input:

100000 30
1063412225 224331901
116583527 514118426
121269548 678461017
856756753 250958443
1064104926 412721149
829078609 544244155
734000135 742933979
9127283 962205064
595091107 123655320
593251579 687018764
1052215261 661849950
297391487 243419322
105274358 526149321
1069300711 46673358
208918023...

output:

236165186

result:

wrong answer 1st numbers differ - expected: '252791218', found: '236165186'