답안 #199649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
199649 2020-02-02T12:34:06 Z dennisstar 가로등 (APIO19_street_lamps) C++17
40 / 100
2301 ms 291284 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ryan bear
#define all(V) ((V).begin()), ((V).end())
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef vector<int> vim;
typedef vector<ll> vlm;
 
int R, C;
int dty_cnt=1; 
struct dty {
	int l, r, dt, Key;
}st2[(1<<23)];
int new_dty(int k) { st2[dty_cnt].Key=k; return dty_cnt++; }
inline void upd2(int now, int y, int val, int ys, int ye) {
	int md=(ys+ye)/2;
	if (st2[now].Key) {
		if (st2[now].Key==y) { st2[now].dt+=val; return ;}
		if (st2[now].Key<=md) { st2[now].l=new_dty(st2[now].Key); st2[st2[now].l].dt=st2[now].dt; }
		else { st2[now].r=new_dty(st2[now].Key); st2[st2[now].r].dt=st2[now].dt; }
		st2[now].Key=0;
	}
	if (y<=md) {
		if (!st2[now].l) st2[now].l=new_dty(y);
		upd2(st2[now].l, y, val, ys, md);
	}
	else {
		if (!st2[now].r) st2[now].r=new_dty(y);
		upd2(st2[now].r, y, val, md+1, ye);
	}
	int L, R;
	L=(st2[now].l?st2[st2[now].l].dt:0);
	R=(st2[now].r?st2[st2[now].r].dt:0);
	st2[now].dt=L+R;
}
inline int get2(int now, int y1, int y2, int ys, int ye) {
	int md=(ys+ye)/2;
	if (st2[now].Key) return (y1<=st2[now].Key&&st2[now].Key<=y2)?st2[now].dt:0;
	if (y1<=ys&&ye<=y2) return st2[now].dt;
	int L, R; L=R=0;
	if (!(md<y1)) L=(st2[now].l?(get2(st2[now].l, y1, y2, ys, md)):0);
	if (md+1<=y2&&md+1<=ye) R=(st2[now].r?get2(st2[now].r, y1, y2, md+1, ye):0);
	return L+R;
}

struct dtx {
	int seg, l, r;
}st1[(1<<19)];
int dtx_cnt=1; int new_dtx() { return dtx_cnt++; }

inline void upd1(int now, int x, int y, int val, int xs, int xe) {
	if (!st1[now].seg) st1[now].seg=new_dty(y);
	if (xs==xe) { upd2(st1[now].seg, y, val, 1, C); return ; }
	int md=(xs+xe)/2;
	if (x<=md) {
		if (!st1[now].l) st1[now].l=new_dtx();
		upd1(st1[now].l, x, y, val, xs, md);
	}
	else {
		if (!st1[now].r) st1[now].r=new_dtx();
		upd1(st1[now].r, x, y, val, md+1, xe);
	}
	upd2(st1[now].seg, y, val, 1, C);
}
inline int get1(int now, int x1, int y1, int x2, int y2, int xs, int xe) {
	if (!st1[now].seg) return 0;
	if (x1<=xs&&xe<=x2) return get2(st1[now].seg, y1, y2, 1, C);
	int md=(xs+xe)/2, L, R; L=R=0;
	if (x1<=md) L=(st1[now].l?get1(st1[now].l, x1, y1, x2, y2, xs, md):0);
	if (md+1<=x2&&md+1<=xe) R=(st1[now].r?get1(st1[now].r, x1, y1, x2, y2, md+1, xe):0);
	return L+R;
}

int root;
int n, q;
char s[10]; int I, a, b;
int ar[300010];
set<pair<pii, int> > S;

int main() {
	root=new_dtx();
	scanf("%d %d", &n, &q);
	R=C=n;
	for (int i=1; i<=n; i++) scanf("%1d", &ar[i]);
	for (int i=1; i<=n; i++) {
		if (!ar[i]) continue;
		int j;
		for (j=i; j<=n; j++) if (!ar[j]) break;
		S.insert({make_pair(i, j-1), 0});
		i=j-1;
	}
	set<pair<pii, int> >::iterator it;
	pair<pii, int> pi, pi1, pi2;
	int ans;
	for (int i=1; i<=q; i++) {
		scanf("%s", s);
		if (s[0]=='t') {
			scanf("%d", &I);
			if (ar[I]) {
				it=S.upper_bound({make_pair(I, (1<<30)), (1<<30)});
				it--; pi=(*it); S.erase(it);
				upd1(root, pi.fi.fi, pi.fi.se, i-pi.se, 1, R);
				if (pi.fi.fi<I) S.insert({make_pair(pi.fi.fi, I-1), i});
				if (pi.fi.se>I) S.insert({make_pair(I+1, pi.fi.se), i});
			}
			else {
				if (ar[I-1]) {
					it=S.upper_bound({make_pair(I-1, (1<<30)), (1<<30)});
					it--; pi1=(*it); S.erase(it);
					upd1(root, pi1.fi.fi, pi1.fi.se, i-pi1.se, 1, R);
				}
				else pi1={make_pair(I, I), 0};
				if (ar[I+1]) {
					it=S.upper_bound({make_pair(I+1, (1<<30)), (1<<30)});
					it--; pi2=(*it); S.erase(it);
					upd1(root, pi2.fi.fi, pi2.fi.se, i-pi2.se, 1, R);
				}
				else pi2={make_pair(I, I), 0};
				S.insert({make_pair(pi1.fi.fi, pi2.fi.se), i});
			}
			ar[I]=1-ar[I];
		}
		else {
			scanf("%d %d", &a, &b); b--;
			if (!S.size()) ans=0;
			else {
				it=S.upper_bound({make_pair(a, (1<<30)), (1<<30)});
				if (it==S.begin()) ans=0;
				else {
					it--;
					if ((*it).fi.fi<=a&&(*it).fi.se>=b) ans=i-(*it).se;
					else ans=0;
				}
			}
			ans+=get1(root, 1, b, a, C, 1, R);
			printf("%d\n", ans);
		}
	}
	return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &q);
  ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:89:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i=1; i<=n; i++) scanf("%1d", &ar[i]);
                           ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", s);
   ~~~~~^~~~~~~~~
street_lamps.cpp:103:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &I);
    ~~~~~^~~~~~~~~~
street_lamps.cpp:129:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &a, &b); b--;
    ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 7 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 288 ms 4600 KB Output is correct
2 Correct 391 ms 5180 KB Output is correct
3 Correct 803 ms 10780 KB Output is correct
4 Correct 1861 ms 117624 KB Output is correct
5 Correct 2301 ms 133700 KB Output is correct
6 Correct 2105 ms 132116 KB Output is correct
7 Correct 183 ms 8184 KB Output is correct
8 Correct 217 ms 9464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 632 KB Output is correct
2 Correct 7 ms 632 KB Output is correct
3 Correct 7 ms 632 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Runtime error 1822 ms 291284 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 504 KB Output is correct
3 Correct 7 ms 632 KB Output is correct
4 Correct 8 ms 632 KB Output is correct
5 Correct 617 ms 15736 KB Output is correct
6 Correct 1262 ms 82888 KB Output is correct
7 Correct 1884 ms 131576 KB Output is correct
8 Runtime error 1987 ms 290808 KB Execution killed with signal 11 (could be triggered by violating memory limits)
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 7 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 424 KB Output is correct
8 Correct 288 ms 4600 KB Output is correct
9 Correct 391 ms 5180 KB Output is correct
10 Correct 803 ms 10780 KB Output is correct
11 Correct 1861 ms 117624 KB Output is correct
12 Correct 2301 ms 133700 KB Output is correct
13 Correct 2105 ms 132116 KB Output is correct
14 Correct 183 ms 8184 KB Output is correct
15 Correct 217 ms 9464 KB Output is correct
16 Correct 7 ms 632 KB Output is correct
17 Correct 7 ms 632 KB Output is correct
18 Correct 7 ms 632 KB Output is correct
19 Correct 6 ms 376 KB Output is correct
20 Runtime error 1822 ms 291284 KB Execution killed with signal 11 (could be triggered by violating memory limits)
21 Halted 0 ms 0 KB -