Submission #264640

# Submission time Handle Problem Language Result Execution time Memory
264640 2020-08-14T08:09:26 Z dimash241 Street Lamps (APIO19_street_lamps) C++17
100 / 100
3511 ms 193748 KB
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
 
//# include <x86intrin.h>
# include <bits/stdc++.h>
 
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
using namespace std;
 
template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red
 
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
	// cin.exceptions(cin.failbit); 
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
 
const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 3e5 + 3;                                          
const int M = 22;
const int pri = 997;
const int Magic = 2101;
 
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
 
int rnd (int l, int r) {
	return uniform_int_distribution<int> (l, r)(gen);
}
 
set < array<int,3> > act;
//on segments l, r, st_time
int n, m;
string s;
 
int t[N*18*18];
int L[N*18*18];
int R[N*18*18];
int root[N];
 
int ptr;
 
void updQuery (int l, int r, int x, int &v, int tl = 1, int tr = N) {
	if (tl > r || l > tr || l > r) return;
	if (!v) v = ++ptr;
	if (l <= tl && tr <= r) {
		t[v] += x;
		return;
	}
	
	int tm = (tl+tr)>>1;
	updQuery(l, r, x, L[v], tl, tm);
	updQuery(l, r, x, R[v], tm+1, tr);
}
 
int getQuery (int p, int v, int tl = 1, int tr = N) {
	if (tl == tr || !v) {
		return t[v];
	}
	
	int tm = (tl+tr)>>1;
	
	if(p <= tm) return t[v] + getQuery(p, L[v], tl, tm);
	
	return t[v] + getQuery(p, R[v], tm+1, tr);
}
 
int getCalc (int v, int r) {
	int res = 0;
	while(v) {
		res += getQuery(r, root[v]);
		v -= v&-v;
	}
	return res;
}
 
void updRec (int v, int l, int r, int x) {
	while(v < N) {
		updQuery(l, r, x, root[v]);
		v += v&-v;
	}
}
 
void Rem (array<int, 3> cur, int t) {
	if (cur[0] > cur[1]) return;
	assert(cur[0] <= cur[1]);
	updRec (cur[0], cur[0], cur[1], t-cur[2]);
	updRec (cur[1]+1, cur[0], cur[1], cur[2]-t);
}
 
void turn_off (int pos, int i) {
	auto cur = *prev(act.lb({pos+1, 0, 0}));
	Rem(cur, i);
	
	act.erase(cur);
	act.insert({cur[0], pos, i});
	act.insert({pos+1, cur[1], i});
}
 
void turn_on (int pos, int i) {
	auto curL = *prev(act.lb({pos+1, 0, 0}));
	auto curR = *prev(act.lb({pos+2, 0, 0}));
	
	Rem(curL, i);
	Rem(curR, i);
	
	act.erase(curL);
	act.erase(curR);
	act.insert({curL[0], curR[1], i});
}
 
int main () {
	SpeedForce;
	
	cin >> n >> m >> s;
	s='0'+s;
	for (auto &e : s) e -= '0';
	
	for (int i = 1, j = 1; i <= n+1; i = j + 1) {
		for (j = i; j <= n && s[j]; j++);
		act.insert({i,j,0});
	}
	
	for (int i = 1; i <= m; ++i) {
		string op;
		cin >> op;
		
		if (op[0] == 't') {
			int pos;
			cin >> pos;
			if(s[pos]) turn_off(pos, i);
			else turn_on(pos, i);
			
			
			s[pos] ^= 1;
		} else {
			int l, r;
			cin >> l >> r;
			//in rectangle x = {1, l} y = {1, r} 
			int ans = getCalc(l, r);
			auto cur = *prev(act.lb({l+1, 0, 0}));
			if (cur[1] >= r) ans += i-cur[2];
			
			cout << ans << '\n';
		}
	}
	
	
	return Accepted;
}
 
// B...a

Compilation message

street_lamps.cpp: In function 'void setIn(std::string)':
street_lamps.cpp:34:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   34 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'void setOut(std::string)':
street_lamps.cpp:35:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   35 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 544 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1394 ms 1528 KB Output is correct
2 Correct 1173 ms 5368 KB Output is correct
3 Correct 1300 ms 9516 KB Output is correct
4 Correct 2133 ms 120424 KB Output is correct
5 Correct 2043 ms 131800 KB Output is correct
6 Correct 2281 ms 127360 KB Output is correct
7 Correct 546 ms 25944 KB Output is correct
8 Correct 131 ms 8712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1152 KB Output is correct
2 Correct 8 ms 1024 KB Output is correct
3 Correct 5 ms 896 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 3511 ms 193748 KB Output is correct
6 Correct 2756 ms 165312 KB Output is correct
7 Correct 1837 ms 126052 KB Output is correct
8 Correct 123 ms 2784 KB Output is correct
9 Correct 95 ms 1276 KB Output is correct
10 Correct 107 ms 4472 KB Output is correct
11 Correct 106 ms 4784 KB Output is correct
12 Correct 518 ms 26060 KB Output is correct
13 Correct 128 ms 8804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 4 ms 800 KB Output is correct
3 Correct 5 ms 896 KB Output is correct
4 Correct 10 ms 924 KB Output is correct
5 Correct 491 ms 18532 KB Output is correct
6 Correct 1180 ms 95320 KB Output is correct
7 Correct 1834 ms 122080 KB Output is correct
8 Correct 2830 ms 140624 KB Output is correct
9 Correct 1727 ms 1188 KB Output is correct
10 Correct 2414 ms 688 KB Output is correct
11 Correct 1635 ms 4472 KB Output is correct
12 Correct 2246 ms 3500 KB Output is correct
13 Correct 1610 ms 4472 KB Output is correct
14 Correct 2335 ms 3704 KB Output is correct
15 Correct 497 ms 26056 KB Output is correct
16 Correct 123 ms 8676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 544 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1394 ms 1528 KB Output is correct
9 Correct 1173 ms 5368 KB Output is correct
10 Correct 1300 ms 9516 KB Output is correct
11 Correct 2133 ms 120424 KB Output is correct
12 Correct 2043 ms 131800 KB Output is correct
13 Correct 2281 ms 127360 KB Output is correct
14 Correct 546 ms 25944 KB Output is correct
15 Correct 131 ms 8712 KB Output is correct
16 Correct 10 ms 1152 KB Output is correct
17 Correct 8 ms 1024 KB Output is correct
18 Correct 5 ms 896 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 3511 ms 193748 KB Output is correct
21 Correct 2756 ms 165312 KB Output is correct
22 Correct 1837 ms 126052 KB Output is correct
23 Correct 123 ms 2784 KB Output is correct
24 Correct 95 ms 1276 KB Output is correct
25 Correct 107 ms 4472 KB Output is correct
26 Correct 106 ms 4784 KB Output is correct
27 Correct 518 ms 26060 KB Output is correct
28 Correct 128 ms 8804 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 4 ms 800 KB Output is correct
31 Correct 5 ms 896 KB Output is correct
32 Correct 10 ms 924 KB Output is correct
33 Correct 491 ms 18532 KB Output is correct
34 Correct 1180 ms 95320 KB Output is correct
35 Correct 1834 ms 122080 KB Output is correct
36 Correct 2830 ms 140624 KB Output is correct
37 Correct 1727 ms 1188 KB Output is correct
38 Correct 2414 ms 688 KB Output is correct
39 Correct 1635 ms 4472 KB Output is correct
40 Correct 2246 ms 3500 KB Output is correct
41 Correct 1610 ms 4472 KB Output is correct
42 Correct 2335 ms 3704 KB Output is correct
43 Correct 497 ms 26056 KB Output is correct
44 Correct 123 ms 8676 KB Output is correct
45 Correct 877 ms 3080 KB Output is correct
46 Correct 813 ms 2892 KB Output is correct
47 Correct 1096 ms 52628 KB Output is correct
48 Correct 1728 ms 120256 KB Output is correct
49 Correct 129 ms 4600 KB Output is correct
50 Correct 143 ms 4600 KB Output is correct
51 Correct 143 ms 4728 KB Output is correct
52 Correct 109 ms 4984 KB Output is correct
53 Correct 112 ms 5112 KB Output is correct
54 Correct 107 ms 4984 KB Output is correct