답안 #264648

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
264648 2020-08-14T08:11:04 Z dimash241 가로등 (APIO19_street_lamps) C++17
100 / 100
3803 ms 193852 KB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("avx,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:33:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   33 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'void setOut(std::string)':
street_lamps.cpp:34:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   34 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 512 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1192 ms 1476 KB Output is correct
2 Correct 1216 ms 1720 KB Output is correct
3 Correct 1226 ms 5384 KB Output is correct
4 Correct 2165 ms 115476 KB Output is correct
5 Correct 2406 ms 126404 KB Output is correct
6 Correct 2282 ms 122556 KB Output is correct
7 Correct 525 ms 20196 KB Output is correct
8 Correct 125 ms 2788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1184 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 3803 ms 193852 KB Output is correct
6 Correct 2580 ms 165348 KB Output is correct
7 Correct 1796 ms 125900 KB Output is correct
8 Correct 126 ms 2788 KB Output is correct
9 Correct 93 ms 1272 KB Output is correct
10 Correct 104 ms 1340 KB Output is correct
11 Correct 102 ms 1520 KB Output is correct
12 Correct 486 ms 20064 KB Output is correct
13 Correct 142 ms 2788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 3 ms 768 KB Output is correct
3 Correct 6 ms 896 KB Output is correct
4 Correct 9 ms 896 KB Output is correct
5 Correct 500 ms 18508 KB Output is correct
6 Correct 1286 ms 95476 KB Output is correct
7 Correct 1863 ms 122576 KB Output is correct
8 Correct 2850 ms 140732 KB Output is correct
9 Correct 1523 ms 1144 KB Output is correct
10 Correct 2164 ms 888 KB Output is correct
11 Correct 1594 ms 1192 KB Output is correct
12 Correct 2164 ms 568 KB Output is correct
13 Correct 1569 ms 1144 KB Output is correct
14 Correct 2109 ms 684 KB Output is correct
15 Correct 517 ms 20012 KB Output is correct
16 Correct 124 ms 2788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 512 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 1192 ms 1476 KB Output is correct
9 Correct 1216 ms 1720 KB Output is correct
10 Correct 1226 ms 5384 KB Output is correct
11 Correct 2165 ms 115476 KB Output is correct
12 Correct 2406 ms 126404 KB Output is correct
13 Correct 2282 ms 122556 KB Output is correct
14 Correct 525 ms 20196 KB Output is correct
15 Correct 125 ms 2788 KB Output is correct
16 Correct 10 ms 1184 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 3803 ms 193852 KB Output is correct
21 Correct 2580 ms 165348 KB Output is correct
22 Correct 1796 ms 125900 KB Output is correct
23 Correct 126 ms 2788 KB Output is correct
24 Correct 93 ms 1272 KB Output is correct
25 Correct 104 ms 1340 KB Output is correct
26 Correct 102 ms 1520 KB Output is correct
27 Correct 486 ms 20064 KB Output is correct
28 Correct 142 ms 2788 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 3 ms 768 KB Output is correct
31 Correct 6 ms 896 KB Output is correct
32 Correct 9 ms 896 KB Output is correct
33 Correct 500 ms 18508 KB Output is correct
34 Correct 1286 ms 95476 KB Output is correct
35 Correct 1863 ms 122576 KB Output is correct
36 Correct 2850 ms 140732 KB Output is correct
37 Correct 1523 ms 1144 KB Output is correct
38 Correct 2164 ms 888 KB Output is correct
39 Correct 1594 ms 1192 KB Output is correct
40 Correct 2164 ms 568 KB Output is correct
41 Correct 1569 ms 1144 KB Output is correct
42 Correct 2109 ms 684 KB Output is correct
43 Correct 517 ms 20012 KB Output is correct
44 Correct 124 ms 2788 KB Output is correct
45 Correct 867 ms 1016 KB Output is correct
46 Correct 796 ms 888 KB Output is correct
47 Correct 992 ms 49624 KB Output is correct
48 Correct 1654 ms 115172 KB Output is correct
49 Correct 119 ms 1144 KB Output is correct
50 Correct 117 ms 1144 KB Output is correct
51 Correct 118 ms 1144 KB Output is correct
52 Correct 118 ms 1112 KB Output is correct
53 Correct 113 ms 1016 KB Output is correct
54 Correct 106 ms 1116 KB Output is correct