Submission #50901

# Submission time Handle Problem Language Result Execution time Memory
50901 2018-06-14T15:42:24 Z Nicksechko Segments (IZhO18_segments) C++14
75 / 100
5000 ms 172032 KB
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>
 
#define mp make_pair
#define pb push_back
 
 
typedef long long ll;
typedef long double ld;

#define f first
#define s second
 
using namespace std;

const int N = 2e5 + 123;

int q, n, id, l[N], r[N];
bool used[N];

vector < pair < int, pair < int, int > > > lazy; 

vector < int > L[4 * N], R[4 * N];

vector < pair < int, int > > segments;

void build(int v = 1, int tl = 0, int tr = n - 1) {
	if (tl > tr) return;
  if (tl == tr) {
  	L[v].clear();
  	R[v].clear();
  	if (tl < segments.size()) {
	    L[v].push_back(segments[tl].first);
  	  R[v].push_back(segments[tl].second);
  	}
    return;
  }
  int tm = (tl + tr) / 2;
  build(v * 2, tl, tm);
  build(v * 2 + 1, tm + 1, tr);
  L[v].resize(L[v * 2].size() + L[v * 2 + 1].size());
  R[v].resize(R[v * 2].size() + R[v * 2 + 1].size());
  merge(L[v * 2].begin(), L[v * 2].end(), L[v * 2 + 1].begin(), L[v * 2 + 1].end(), L[v].begin());
  merge(R[v * 2].begin(), R[v * 2].end(), R[v * 2 + 1].begin(), R[v * 2 + 1].end(), R[v].begin());  
}

int getId(int k) {
  int ans = -1, l = 0, r = segments.size() - 1;
  while(l <= r) {
    int mid = (l + r) / 2;
    int len = segments[mid].second - segments[mid].first + 1;
    if (len >= k) {
      ans = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return ans;
}

int get(int l, int r, int A, int B, int k, int v = 1, int tl = 0, int tr = n - 1) {
  if (tl > r || tr < l || l > r || tl > tr) return 0;
//  cout << "l = " << l << " r = " << r << " tl = " << tl << " tr = " << tr << endl;
  if (l <= tl && tr <= r) {
    int res = (lower_bound(R[v].begin(), R[v].end(), A + k - 1) - R[v].begin())
            + (L[v].end() - upper_bound(L[v].begin(), L[v].end(), B - k + 1));
    return res;
  }
  int tm = (tl + tr) / 2;
 // cout << "tm = " << tm << endl;
  int ans = get(l, r, A, B, k, v * 2, tl, tm) + get(l, r, A, B, k, v * 2 + 1, tm + 1, tr);
  return ans;
}

int inter(int l1, int r1, int l2, int r2) {
  return max(0, min(r1, r2) - max(l1, l2) + 1);
}

int query(int A, int B, int k) {
  if (B - A + 1 < k) return 0;
  int id = getId(k);
 // cout << "A = " << A << " " << B << " k = " << k << " id = " << id << endl;
 // for (auto i : segments) cout << i.second - i.first + 1 << " ";
 //	cout << endl;
 //	cout << "start new query\n";
  int ans = id + 1 - get(0, id, A, B, k);
  for (auto i : lazy) {
		if (inter(i.second.first, i.second.second, A, B) >= k) {
			if (i.first == 1) ans++;
			else ans--;
		}  
  }
  return ans;
}

bool cmp(pair < int, int > a, pair < int, int > b) {
  return a.second - a.first + 1 > b.second - b.first + 1;
}

void rebuild() {
	segments.clear();
	lazy.clear();
	for (int i = 1;i <= id;i++) {
		if (!used[i]) {
			segments.pb({l[i], r[i]});
		}
	}
	sort(segments.begin(), segments.end(), &cmp);
	n = segments.size();
	build();
}

int main() {
//	freopen("input", "r", stdin);
//	freopen("output", "w", stdout);
  int G;
  scanf("%d%d", &q, &G);
  int K = sqrt(q * 19);
  int lst_ans = 0;
  //cout << q << " " << K << endl;
  for (int i = 0, t, x, was = 0;i < q;i++) {
  	if (i % K == 0) {
//  		cout << "starting\n";
  		rebuild();
  //		cout << "Succesfully i = " << i / K << endl;
  	}
    scanf("%d", &t);
    if (t == 1) {
      id++;
      scanf("%d%d", &l[id], &r[id]);
      l[id] = l[id] ^ (G * lst_ans);
      r[id] = r[id] ^ (G * lst_ans);
      if (l[id] > r[id]) swap(l[id], r[id]);
      lazy.push_back({1, {l[id], r[id]}});
    }
    if (t == 2) {
    	scanf("%d", &x);
    	used[x] = 1;
    	lazy.push_back({2, {l[x], r[x]}});
    }
    if (t == 3) {
      int a, b, k;
      scanf("%d%d%d", &a, &b, &k);
      a = a ^ (G * lst_ans);
      b = b ^ (G * lst_ans);
      if (a > b) swap(a, b);
      printf("%d\n", lst_ans = query(a, b, k));
    }
  }
  return 0;
}
 

Compilation message

segments.cpp: In function 'void build(int, int, int)':
segments.cpp:43:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (tl < segments.size()) {
        ~~~^~~~~~~~~~~~~~~~~
segments.cpp: In function 'int main()':
segments.cpp:133:25: warning: unused variable 'was' [-Wunused-variable]
   for (int i = 0, t, x, was = 0;i < q;i++) {
                         ^~~
segments.cpp:129:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &q, &G);
   ~~~~~^~~~~~~~~~~~~~~~
segments.cpp:139:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &t);
     ~~~~~^~~~~~~~~~
segments.cpp:142:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d%d", &l[id], &r[id]);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:149:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d", &x);
      ~~~~~^~~~~~~~~~
segments.cpp:155:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d%d%d", &a, &b, &k);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 36 ms 38104 KB Output is correct
2 Correct 35 ms 38256 KB Output is correct
3 Correct 42 ms 38312 KB Output is correct
4 Correct 38 ms 38480 KB Output is correct
5 Correct 57 ms 39584 KB Output is correct
6 Correct 69 ms 39584 KB Output is correct
7 Correct 46 ms 39584 KB Output is correct
8 Correct 53 ms 39740 KB Output is correct
9 Correct 53 ms 39788 KB Output is correct
10 Correct 61 ms 40356 KB Output is correct
11 Correct 57 ms 40356 KB Output is correct
12 Correct 53 ms 40356 KB Output is correct
13 Correct 61 ms 40744 KB Output is correct
14 Correct 49 ms 40744 KB Output is correct
15 Correct 42 ms 40744 KB Output is correct
16 Correct 41 ms 40744 KB Output is correct
17 Correct 46 ms 40744 KB Output is correct
18 Correct 55 ms 41208 KB Output is correct
19 Correct 50 ms 41208 KB Output is correct
20 Correct 50 ms 41208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1791 ms 57404 KB Output is correct
2 Correct 1780 ms 60204 KB Output is correct
3 Correct 1757 ms 62980 KB Output is correct
4 Correct 1715 ms 66416 KB Output is correct
5 Correct 2078 ms 81424 KB Output is correct
6 Correct 2045 ms 84248 KB Output is correct
7 Correct 1685 ms 84248 KB Output is correct
8 Correct 1714 ms 84248 KB Output is correct
9 Correct 1751 ms 84248 KB Output is correct
10 Correct 788 ms 84248 KB Output is correct
11 Correct 1103 ms 84248 KB Output is correct
12 Correct 1937 ms 92552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 92552 KB Output is correct
2 Correct 193 ms 92552 KB Output is correct
3 Correct 186 ms 92552 KB Output is correct
4 Correct 186 ms 92552 KB Output is correct
5 Correct 1911 ms 102732 KB Output is correct
6 Correct 1647 ms 102732 KB Output is correct
7 Correct 1795 ms 104048 KB Output is correct
8 Correct 2121 ms 116640 KB Output is correct
9 Correct 1986 ms 118504 KB Output is correct
10 Correct 1887 ms 118504 KB Output is correct
11 Correct 287 ms 118504 KB Output is correct
12 Correct 1883 ms 118504 KB Output is correct
13 Correct 1763 ms 118504 KB Output is correct
14 Correct 1014 ms 118504 KB Output is correct
15 Correct 857 ms 118504 KB Output is correct
16 Correct 622 ms 118504 KB Output is correct
17 Correct 1538 ms 121688 KB Output is correct
18 Correct 1502 ms 124100 KB Output is correct
19 Correct 1502 ms 126248 KB Output is correct
20 Correct 1577 ms 128772 KB Output is correct
21 Correct 305 ms 128772 KB Output is correct
22 Correct 1180 ms 128772 KB Output is correct
23 Correct 1466 ms 134380 KB Output is correct
24 Correct 1250 ms 134380 KB Output is correct
25 Correct 208 ms 134380 KB Output is correct
26 Correct 185 ms 134380 KB Output is correct
27 Correct 183 ms 134380 KB Output is correct
28 Correct 221 ms 134380 KB Output is correct
29 Correct 1541 ms 146752 KB Output is correct
30 Correct 1637 ms 148724 KB Output is correct
31 Correct 2052 ms 163372 KB Output is correct
32 Correct 1790 ms 163372 KB Output is correct
33 Correct 1637 ms 163372 KB Output is correct
34 Correct 912 ms 163372 KB Output is correct
35 Correct 1497 ms 163372 KB Output is correct
36 Correct 1831 ms 163372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 163372 KB Output is correct
2 Correct 179 ms 163372 KB Output is correct
3 Correct 198 ms 163372 KB Output is correct
4 Correct 190 ms 163372 KB Output is correct
5 Correct 1977 ms 172032 KB Output is correct
6 Correct 637 ms 172032 KB Output is correct
7 Correct 2071 ms 172032 KB Output is correct
8 Correct 896 ms 172032 KB Output is correct
9 Correct 1234 ms 172032 KB Output is correct
10 Correct 1932 ms 172032 KB Output is correct
11 Correct 602 ms 172032 KB Output is correct
12 Correct 2202 ms 172032 KB Output is correct
13 Correct 1716 ms 172032 KB Output is correct
14 Correct 1077 ms 172032 KB Output is correct
15 Correct 1967 ms 172032 KB Output is correct
16 Correct 1805 ms 172032 KB Output is correct
17 Correct 1752 ms 172032 KB Output is correct
18 Correct 1610 ms 172032 KB Output is correct
19 Correct 1992 ms 172032 KB Output is correct
20 Correct 2048 ms 172032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 38104 KB Output is correct
2 Correct 35 ms 38256 KB Output is correct
3 Correct 42 ms 38312 KB Output is correct
4 Correct 38 ms 38480 KB Output is correct
5 Correct 57 ms 39584 KB Output is correct
6 Correct 69 ms 39584 KB Output is correct
7 Correct 46 ms 39584 KB Output is correct
8 Correct 53 ms 39740 KB Output is correct
9 Correct 53 ms 39788 KB Output is correct
10 Correct 61 ms 40356 KB Output is correct
11 Correct 57 ms 40356 KB Output is correct
12 Correct 53 ms 40356 KB Output is correct
13 Correct 61 ms 40744 KB Output is correct
14 Correct 49 ms 40744 KB Output is correct
15 Correct 42 ms 40744 KB Output is correct
16 Correct 41 ms 40744 KB Output is correct
17 Correct 46 ms 40744 KB Output is correct
18 Correct 55 ms 41208 KB Output is correct
19 Correct 50 ms 41208 KB Output is correct
20 Correct 50 ms 41208 KB Output is correct
21 Correct 1791 ms 57404 KB Output is correct
22 Correct 1780 ms 60204 KB Output is correct
23 Correct 1757 ms 62980 KB Output is correct
24 Correct 1715 ms 66416 KB Output is correct
25 Correct 2078 ms 81424 KB Output is correct
26 Correct 2045 ms 84248 KB Output is correct
27 Correct 1685 ms 84248 KB Output is correct
28 Correct 1714 ms 84248 KB Output is correct
29 Correct 1751 ms 84248 KB Output is correct
30 Correct 788 ms 84248 KB Output is correct
31 Correct 1103 ms 84248 KB Output is correct
32 Correct 1937 ms 92552 KB Output is correct
33 Correct 172 ms 163372 KB Output is correct
34 Correct 179 ms 163372 KB Output is correct
35 Correct 198 ms 163372 KB Output is correct
36 Correct 190 ms 163372 KB Output is correct
37 Correct 1977 ms 172032 KB Output is correct
38 Correct 637 ms 172032 KB Output is correct
39 Correct 2071 ms 172032 KB Output is correct
40 Correct 896 ms 172032 KB Output is correct
41 Correct 1234 ms 172032 KB Output is correct
42 Correct 1932 ms 172032 KB Output is correct
43 Correct 602 ms 172032 KB Output is correct
44 Correct 2202 ms 172032 KB Output is correct
45 Correct 1716 ms 172032 KB Output is correct
46 Correct 1077 ms 172032 KB Output is correct
47 Correct 1967 ms 172032 KB Output is correct
48 Correct 1805 ms 172032 KB Output is correct
49 Correct 1752 ms 172032 KB Output is correct
50 Correct 1610 ms 172032 KB Output is correct
51 Correct 1992 ms 172032 KB Output is correct
52 Correct 2048 ms 172032 KB Output is correct
53 Correct 220 ms 172032 KB Output is correct
54 Correct 202 ms 172032 KB Output is correct
55 Correct 229 ms 172032 KB Output is correct
56 Correct 224 ms 172032 KB Output is correct
57 Correct 1432 ms 172032 KB Output is correct
58 Correct 604 ms 172032 KB Output is correct
59 Correct 2192 ms 172032 KB Output is correct
60 Correct 549 ms 172032 KB Output is correct
61 Correct 1913 ms 172032 KB Output is correct
62 Correct 2354 ms 172032 KB Output is correct
63 Correct 2343 ms 172032 KB Output is correct
64 Correct 2385 ms 172032 KB Output is correct
65 Correct 818 ms 172032 KB Output is correct
66 Correct 662 ms 172032 KB Output is correct
67 Correct 1935 ms 172032 KB Output is correct
68 Correct 1749 ms 172032 KB Output is correct
69 Correct 1826 ms 172032 KB Output is correct
70 Correct 1778 ms 172032 KB Output is correct
71 Correct 1689 ms 172032 KB Output is correct
72 Correct 1693 ms 172032 KB Output is correct
73 Correct 931 ms 172032 KB Output is correct
74 Correct 1535 ms 172032 KB Output is correct
75 Correct 2101 ms 172032 KB Output is correct
76 Correct 2123 ms 172032 KB Output is correct
77 Correct 203 ms 172032 KB Output is correct
78 Correct 194 ms 172032 KB Output is correct
79 Correct 227 ms 172032 KB Output is correct
80 Correct 206 ms 172032 KB Output is correct
81 Correct 1557 ms 172032 KB Output is correct
82 Correct 1010 ms 172032 KB Output is correct
83 Correct 555 ms 172032 KB Output is correct
84 Correct 1517 ms 172032 KB Output is correct
85 Correct 1749 ms 172032 KB Output is correct
86 Correct 1759 ms 172032 KB Output is correct
87 Correct 1269 ms 172032 KB Output is correct
88 Correct 559 ms 172032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 38104 KB Output is correct
2 Correct 35 ms 38256 KB Output is correct
3 Correct 42 ms 38312 KB Output is correct
4 Correct 38 ms 38480 KB Output is correct
5 Correct 57 ms 39584 KB Output is correct
6 Correct 69 ms 39584 KB Output is correct
7 Correct 46 ms 39584 KB Output is correct
8 Correct 53 ms 39740 KB Output is correct
9 Correct 53 ms 39788 KB Output is correct
10 Correct 61 ms 40356 KB Output is correct
11 Correct 57 ms 40356 KB Output is correct
12 Correct 53 ms 40356 KB Output is correct
13 Correct 61 ms 40744 KB Output is correct
14 Correct 49 ms 40744 KB Output is correct
15 Correct 42 ms 40744 KB Output is correct
16 Correct 41 ms 40744 KB Output is correct
17 Correct 46 ms 40744 KB Output is correct
18 Correct 55 ms 41208 KB Output is correct
19 Correct 50 ms 41208 KB Output is correct
20 Correct 50 ms 41208 KB Output is correct
21 Correct 1791 ms 57404 KB Output is correct
22 Correct 1780 ms 60204 KB Output is correct
23 Correct 1757 ms 62980 KB Output is correct
24 Correct 1715 ms 66416 KB Output is correct
25 Correct 2078 ms 81424 KB Output is correct
26 Correct 2045 ms 84248 KB Output is correct
27 Correct 1685 ms 84248 KB Output is correct
28 Correct 1714 ms 84248 KB Output is correct
29 Correct 1751 ms 84248 KB Output is correct
30 Correct 788 ms 84248 KB Output is correct
31 Correct 1103 ms 84248 KB Output is correct
32 Correct 1937 ms 92552 KB Output is correct
33 Correct 209 ms 92552 KB Output is correct
34 Correct 193 ms 92552 KB Output is correct
35 Correct 186 ms 92552 KB Output is correct
36 Correct 186 ms 92552 KB Output is correct
37 Correct 1911 ms 102732 KB Output is correct
38 Correct 1647 ms 102732 KB Output is correct
39 Correct 1795 ms 104048 KB Output is correct
40 Correct 2121 ms 116640 KB Output is correct
41 Correct 1986 ms 118504 KB Output is correct
42 Correct 1887 ms 118504 KB Output is correct
43 Correct 287 ms 118504 KB Output is correct
44 Correct 1883 ms 118504 KB Output is correct
45 Correct 1763 ms 118504 KB Output is correct
46 Correct 1014 ms 118504 KB Output is correct
47 Correct 857 ms 118504 KB Output is correct
48 Correct 622 ms 118504 KB Output is correct
49 Correct 1538 ms 121688 KB Output is correct
50 Correct 1502 ms 124100 KB Output is correct
51 Correct 1502 ms 126248 KB Output is correct
52 Correct 1577 ms 128772 KB Output is correct
53 Correct 305 ms 128772 KB Output is correct
54 Correct 1180 ms 128772 KB Output is correct
55 Correct 1466 ms 134380 KB Output is correct
56 Correct 1250 ms 134380 KB Output is correct
57 Correct 208 ms 134380 KB Output is correct
58 Correct 185 ms 134380 KB Output is correct
59 Correct 183 ms 134380 KB Output is correct
60 Correct 221 ms 134380 KB Output is correct
61 Correct 1541 ms 146752 KB Output is correct
62 Correct 1637 ms 148724 KB Output is correct
63 Correct 2052 ms 163372 KB Output is correct
64 Correct 1790 ms 163372 KB Output is correct
65 Correct 1637 ms 163372 KB Output is correct
66 Correct 912 ms 163372 KB Output is correct
67 Correct 1497 ms 163372 KB Output is correct
68 Correct 1831 ms 163372 KB Output is correct
69 Correct 172 ms 163372 KB Output is correct
70 Correct 179 ms 163372 KB Output is correct
71 Correct 198 ms 163372 KB Output is correct
72 Correct 190 ms 163372 KB Output is correct
73 Correct 1977 ms 172032 KB Output is correct
74 Correct 637 ms 172032 KB Output is correct
75 Correct 2071 ms 172032 KB Output is correct
76 Correct 896 ms 172032 KB Output is correct
77 Correct 1234 ms 172032 KB Output is correct
78 Correct 1932 ms 172032 KB Output is correct
79 Correct 602 ms 172032 KB Output is correct
80 Correct 2202 ms 172032 KB Output is correct
81 Correct 1716 ms 172032 KB Output is correct
82 Correct 1077 ms 172032 KB Output is correct
83 Correct 1967 ms 172032 KB Output is correct
84 Correct 1805 ms 172032 KB Output is correct
85 Correct 1752 ms 172032 KB Output is correct
86 Correct 1610 ms 172032 KB Output is correct
87 Correct 1992 ms 172032 KB Output is correct
88 Correct 2048 ms 172032 KB Output is correct
89 Correct 220 ms 172032 KB Output is correct
90 Correct 202 ms 172032 KB Output is correct
91 Correct 229 ms 172032 KB Output is correct
92 Correct 224 ms 172032 KB Output is correct
93 Correct 1432 ms 172032 KB Output is correct
94 Correct 604 ms 172032 KB Output is correct
95 Correct 2192 ms 172032 KB Output is correct
96 Correct 549 ms 172032 KB Output is correct
97 Correct 1913 ms 172032 KB Output is correct
98 Correct 2354 ms 172032 KB Output is correct
99 Correct 2343 ms 172032 KB Output is correct
100 Correct 2385 ms 172032 KB Output is correct
101 Correct 818 ms 172032 KB Output is correct
102 Correct 662 ms 172032 KB Output is correct
103 Correct 1935 ms 172032 KB Output is correct
104 Correct 1749 ms 172032 KB Output is correct
105 Correct 1826 ms 172032 KB Output is correct
106 Correct 1778 ms 172032 KB Output is correct
107 Correct 1689 ms 172032 KB Output is correct
108 Correct 1693 ms 172032 KB Output is correct
109 Correct 931 ms 172032 KB Output is correct
110 Correct 1535 ms 172032 KB Output is correct
111 Correct 2101 ms 172032 KB Output is correct
112 Correct 2123 ms 172032 KB Output is correct
113 Correct 203 ms 172032 KB Output is correct
114 Correct 194 ms 172032 KB Output is correct
115 Correct 227 ms 172032 KB Output is correct
116 Correct 206 ms 172032 KB Output is correct
117 Correct 1557 ms 172032 KB Output is correct
118 Correct 1010 ms 172032 KB Output is correct
119 Correct 555 ms 172032 KB Output is correct
120 Correct 1517 ms 172032 KB Output is correct
121 Correct 1749 ms 172032 KB Output is correct
122 Correct 1759 ms 172032 KB Output is correct
123 Correct 1269 ms 172032 KB Output is correct
124 Correct 559 ms 172032 KB Output is correct
125 Correct 401 ms 172032 KB Output is correct
126 Correct 380 ms 172032 KB Output is correct
127 Correct 402 ms 172032 KB Output is correct
128 Correct 433 ms 172032 KB Output is correct
129 Correct 406 ms 172032 KB Output is correct
130 Correct 472 ms 172032 KB Output is correct
131 Correct 1291 ms 172032 KB Output is correct
132 Correct 4241 ms 172032 KB Output is correct
133 Execution timed out 5045 ms 172032 KB Time limit exceeded
134 Halted 0 ms 0 KB -