Submission #122519

# Submission time Handle Problem Language Result Execution time Memory
122519 2019-06-28T13:51:52 Z abacaba Street Lamps (APIO19_street_lamps) C++14
100 / 100
1721 ms 200512 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 15;
int n, q, fenw[N], a[N];

struct node {
	node *l, *r;
	int val, add, pr, key;
	node() : val(0), add(0), pr(0), l(NULL), r(NULL) {}
	node(int key) : key(key), val(0), add(0), pr(0), l(NULL), r(NULL) {
		pr = (int)rand() * rand();
	}
};

struct query {
	int type, l, r;
} qs[N];

struct upd {
	int l, r, val;
	upd(int l, int r, int val) : l(l), r(r), val(val) {}
};

typedef node* pnode;

void push(pnode &t) {
	if(t) {
		if(t->l) {
			t->l->add += t->add;
			t->l->val += t->add;
		}
		if(t->r) {
			t->r->add += t->add;
			t->r->val += t->add;
		}
		t->add = 0;
	}
}

void split(pnode t, pnode &l, pnode &r, int key) {
	if(!t)
		return void(l = r = t);
	push(t);
	if(t->key <= key)
		split(t->r, t->r, r, key), l = t;
	else
		split(t->l, l, t->l, key), r = t;
}

void merge(pnode &t, pnode l, pnode r) {
	push(l);
	push(r);
	if(!l || !r)
		t = l ? l : r;
	else if(l->pr > r->pr)
		merge(l->r, l->r, r), t = l;
	else
		merge(r->l, l, r->l), t = r;
}

inline void inc(int i, int val) {
	for(; i <= n; i = (i | (i + 1)))
		fenw[i] += val;
}

inline int get(int i) {
	int res = 0;
	for(; i >= 1; i = (i & (i + 1)) - 1)
		res += fenw[i];
	return res;
}

inline int get(int l, int r) {
	if(l > r)
		return 0;
	return get(r) - get(l - 1);
}

void change(int i) {
	inc(i, -a[i]);
	a[i] ^= 1;
	inc(i, a[i]);
}

pnode t[N << 4];
vector <upd> add[N << 4];

int getleft(int j) {
	int l = 1, r = j, leftmost = j;
	while(l <= r) {
		int mid = l + r >> 1;
		if(j - mid == get(mid, j - 1)) {
			leftmost = mid;
			r = mid - 1;
		}
		else
			l = mid + 1;
	}
	return leftmost;
}

int getright(int j) {
	int l = j, r = n, rightmost = j;
	while(l <= r) {
		int mid = l + r >> 1;
		if(mid - j == get(j + 1, mid)) {
			rightmost = mid;
			l = mid + 1;
		}
		else
			r = mid - 1;
	}
	return rightmost;
}

void update(int v, int mostleft, int mostright, int val) {
	pnode t1, t23, t2, t3, cur;
	cur = t[v];
	split(cur, t1, t23, mostleft - 1);
	split(t23, t2, t3, mostright);
	if(t2) {
		t2->add += val;
		t2->val += val;
	}
	merge(t23, t2, t3);
	merge(cur, t1, t23);
}

void pusht(int v) {
	while(!add[v].empty()) {
		upd now = add[v].back();
		add[v << 1].push_back(now);
		add[v << 1 | 1].push_back(now);
		add[v].pop_back();
		update(v << 1, now.l, now.r, now.val);
		update(v << 1 | 1, now.l, now.r, now.val);
	}
}

void multiupdate(int v, int tl, int tr, int l, int r, int mostleft, int mostright, int val) {
	if(tl > r || tr < l || l > r)
		return;
	pusht(v);
	if(tl >= l && tr <= r) {
		add[v].push_back(*new upd(mostleft, mostright, val));
		update(v, mostleft, mostright, val);
		return;
	}
	int mid = tl + tr >> 1;
	multiupdate(v << 1, tl, mid, l, r, mostleft, mostright, val);
	multiupdate(v << 1 | 1, mid + 1, tr, l, r, mostleft, mostright, val);
}

void update(int v, int tl, int tr, int pos, pnode val) {
	if(tl == tr) {
		t[v] = val;
		return;
	}
	int mid = tl + tr >> 1;
	if(pos <= mid)
		update(v << 1, tl, mid, pos, val);
	else
		update(v << 1 | 1, mid + 1, tr, pos, val);
}

int get(int v, int tl, int tr, int l, int r) {
	pusht(v);
	if(tl == tr) {
		pnode t1, t23, t2, t3;
		split(t[v], t1, t23, l - 1);
		split(t23, t2, t3, l);
		int ans = 0;
		if(t2)
			ans = t2->val;
		merge(t23, t2, t3);
		merge(t[v], t1, t23);
		return ans;
	}
	int mid = tl + tr >> 1;
	if(r <= mid)
		return get(v << 1, tl, mid, l, r);
	return get(v << 1 | 1, mid + 1, tr, l, r);
}

set <int> rs[N], temporaryR;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> q;
	for(int i = 1; i <= n; ++i) {
		char c;
		cin >> c;
		a[i] = (c == '1');
		inc(i, a[i]);
	}
	for(int i = 1; i <= q; ++i) {
		string s;
		cin >> s;
		qs[i].type = (s[0] != 't') + 1;
		if(qs[i].type == 1) {
			int j;
			cin >> j;
			qs[i].l = j;
		}
		else {
			cin >> qs[i].l >> qs[i].r;
			--qs[i].r;
			rs[qs[i].r].insert(qs[i].l);
			temporaryR.insert(qs[i].r);
		}
	}
	for(set <int>::iterator it = temporaryR.begin(); it != temporaryR.end(); ++it) {
		int r = *it;
		pnode t = NULL;
		for(set <int>::iterator ik = rs[r].begin(); ik != rs[r].end(); ++ik)
			merge(t, t, new node(*ik));
		update(1, 1, n, r, t);
	}
	for(int i = 1; i <= q; ++i) {
		if(qs[i].type == 1) {
			int j = qs[i].l;
			change(j);
			int leftmost = getleft(j);
			int rightmost = getright(j);
			if(!a[j])
				multiupdate(1, 1, n, j, rightmost, leftmost, j, i);
			else
				multiupdate(1, 1, n, j, rightmost, leftmost, j, -i);
		}
		else {
			int val = get(qs[i].l, qs[i].r);
			int ans = get(1, 1, n, qs[i].l, qs[i].r);
			if(val == qs[i].r - qs[i].l + 1)
				ans += i;
			cout << ans << endl;
		}
	}
	return 0;
}

Compilation message

street_lamps.cpp: In constructor 'node::node()':
street_lamps.cpp:9:16: warning: 'node::pr' will be initialized after [-Wreorder]
  int val, add, pr, key;
                ^~
street_lamps.cpp:8:8: warning:   'node* node::l' [-Wreorder]
  node *l, *r;
        ^
street_lamps.cpp:10:2: warning:   when initialized here [-Wreorder]
  node() : val(0), add(0), pr(0), l(NULL), r(NULL) {}
  ^~~~
street_lamps.cpp: In constructor 'node::node(int)':
street_lamps.cpp:9:20: warning: 'node::key' will be initialized after [-Wreorder]
  int val, add, pr, key;
                    ^~~
street_lamps.cpp:9:6: warning:   'int node::val' [-Wreorder]
  int val, add, pr, key;
      ^~~
street_lamps.cpp:11:2: warning:   when initialized here [-Wreorder]
  node(int key) : key(key), val(0), add(0), pr(0), l(NULL), r(NULL) {
  ^~~~
street_lamps.cpp:9:16: warning: 'node::pr' will be initialized after [-Wreorder]
  int val, add, pr, key;
                ^~
street_lamps.cpp:8:8: warning:   'node* node::l' [-Wreorder]
  node *l, *r;
        ^
street_lamps.cpp:11:2: warning:   when initialized here [-Wreorder]
  node(int key) : key(key), val(0), add(0), pr(0), l(NULL), r(NULL) {
  ^~~~
street_lamps.cpp: In function 'int getleft(int)':
street_lamps.cpp:92:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid = l + r >> 1;
             ~~^~~
street_lamps.cpp: In function 'int getright(int)':
street_lamps.cpp:106:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid = l + r >> 1;
             ~~^~~
street_lamps.cpp: In function 'void multiupdate(int, int, int, int, int, int, int, int)':
street_lamps.cpp:150:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = tl + tr >> 1;
            ~~~^~~~
street_lamps.cpp: In function 'void update(int, int, int, int, pnode)':
street_lamps.cpp:160:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = tl + tr >> 1;
            ~~~^~~~
street_lamps.cpp: In function 'int get(int, int, int, int, int)':
street_lamps.cpp:180:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid = tl + tr >> 1;
            ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 108 ms 127224 KB Output is correct
2 Correct 108 ms 127220 KB Output is correct
3 Correct 121 ms 127168 KB Output is correct
4 Correct 108 ms 127224 KB Output is correct
5 Correct 109 ms 127228 KB Output is correct
6 Correct 109 ms 127132 KB Output is correct
7 Correct 134 ms 127224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 503 ms 147164 KB Output is correct
2 Correct 592 ms 147764 KB Output is correct
3 Correct 718 ms 150236 KB Output is correct
4 Correct 1406 ms 175392 KB Output is correct
5 Correct 1506 ms 193312 KB Output is correct
6 Correct 1241 ms 177784 KB Output is correct
7 Correct 1306 ms 172504 KB Output is correct
8 Correct 1278 ms 174068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 127352 KB Output is correct
2 Correct 111 ms 127352 KB Output is correct
3 Correct 112 ms 127352 KB Output is correct
4 Correct 114 ms 127352 KB Output is correct
5 Correct 1047 ms 177040 KB Output is correct
6 Correct 1301 ms 190728 KB Output is correct
7 Correct 1581 ms 200512 KB Output is correct
8 Correct 1414 ms 183492 KB Output is correct
9 Correct 605 ms 134268 KB Output is correct
10 Correct 643 ms 134720 KB Output is correct
11 Correct 658 ms 135288 KB Output is correct
12 Correct 1502 ms 181928 KB Output is correct
13 Correct 1613 ms 183416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 127352 KB Output is correct
2 Correct 117 ms 127352 KB Output is correct
3 Correct 117 ms 127608 KB Output is correct
4 Correct 125 ms 127484 KB Output is correct
5 Correct 1721 ms 182240 KB Output is correct
6 Correct 1499 ms 184952 KB Output is correct
7 Correct 1346 ms 183804 KB Output is correct
8 Correct 1110 ms 174848 KB Output is correct
9 Correct 855 ms 169208 KB Output is correct
10 Correct 516 ms 163848 KB Output is correct
11 Correct 795 ms 169208 KB Output is correct
12 Correct 482 ms 164216 KB Output is correct
13 Correct 771 ms 168796 KB Output is correct
14 Correct 477 ms 164028 KB Output is correct
15 Correct 1519 ms 182060 KB Output is correct
16 Correct 1545 ms 183556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 127224 KB Output is correct
2 Correct 108 ms 127220 KB Output is correct
3 Correct 121 ms 127168 KB Output is correct
4 Correct 108 ms 127224 KB Output is correct
5 Correct 109 ms 127228 KB Output is correct
6 Correct 109 ms 127132 KB Output is correct
7 Correct 134 ms 127224 KB Output is correct
8 Correct 503 ms 147164 KB Output is correct
9 Correct 592 ms 147764 KB Output is correct
10 Correct 718 ms 150236 KB Output is correct
11 Correct 1406 ms 175392 KB Output is correct
12 Correct 1506 ms 193312 KB Output is correct
13 Correct 1241 ms 177784 KB Output is correct
14 Correct 1306 ms 172504 KB Output is correct
15 Correct 1278 ms 174068 KB Output is correct
16 Correct 111 ms 127352 KB Output is correct
17 Correct 111 ms 127352 KB Output is correct
18 Correct 112 ms 127352 KB Output is correct
19 Correct 114 ms 127352 KB Output is correct
20 Correct 1047 ms 177040 KB Output is correct
21 Correct 1301 ms 190728 KB Output is correct
22 Correct 1581 ms 200512 KB Output is correct
23 Correct 1414 ms 183492 KB Output is correct
24 Correct 605 ms 134268 KB Output is correct
25 Correct 643 ms 134720 KB Output is correct
26 Correct 658 ms 135288 KB Output is correct
27 Correct 1502 ms 181928 KB Output is correct
28 Correct 1613 ms 183416 KB Output is correct
29 Correct 109 ms 127352 KB Output is correct
30 Correct 117 ms 127352 KB Output is correct
31 Correct 117 ms 127608 KB Output is correct
32 Correct 125 ms 127484 KB Output is correct
33 Correct 1721 ms 182240 KB Output is correct
34 Correct 1499 ms 184952 KB Output is correct
35 Correct 1346 ms 183804 KB Output is correct
36 Correct 1110 ms 174848 KB Output is correct
37 Correct 855 ms 169208 KB Output is correct
38 Correct 516 ms 163848 KB Output is correct
39 Correct 795 ms 169208 KB Output is correct
40 Correct 482 ms 164216 KB Output is correct
41 Correct 771 ms 168796 KB Output is correct
42 Correct 477 ms 164028 KB Output is correct
43 Correct 1519 ms 182060 KB Output is correct
44 Correct 1545 ms 183556 KB Output is correct
45 Correct 374 ms 140804 KB Output is correct
46 Correct 530 ms 142548 KB Output is correct
47 Correct 897 ms 160248 KB Output is correct
48 Correct 1340 ms 182344 KB Output is correct
49 Correct 719 ms 135416 KB Output is correct
50 Correct 739 ms 135436 KB Output is correct
51 Correct 760 ms 135992 KB Output is correct
52 Correct 929 ms 147252 KB Output is correct
53 Correct 951 ms 147136 KB Output is correct
54 Correct 1004 ms 147220 KB Output is correct