답안 #231151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
231151 2020-05-12T22:16:21 Z ChrisT 가로등 (APIO19_street_lamps) C++17
100 / 100
1510 ms 83640 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
using pib = pair<int,bool>;
using ll = long long;
using pll = pair<ll,ll>;
using ld = long double;
#define all(x) (x).begin(),(x).end()
#ifdef fread_unlocked
#define fread fread_unlocked
#define fwrite fwrite_unlocked
#endif
#define lc ind<<1
#define rc ind<<1|1
const int MN = 3e5+4, MOD = 1e9+7, BASE = 31;
struct Q {
	int x,y,t,full; //full is -1 if update
};
int ans[MN], bit[MN];
set<int> zeroes;
char s[10];
void update (int i, int v) {
	for (;i<MN;i+=i&-i)
		bit[i] += v;
}
int query (int i) {
	int ret = 0;
	for (;i;i^=i&-i)
		ret += bit[i];
	return ret;
}
void cdq (int l, int r, vector<Q> &queries) { //cdq over x
	if (queries.empty()) return;
	int mid = (l+r)/2;
	vector<Q> lq,rq;
	for (Q &q : queries) {
		if (q.full == -1) {
			if (q.x <= mid) update(q.y,q.t),lq.push_back(q);
			else rq.push_back(q);
		} else {
			if (q.x <= mid && l != r) lq.push_back(q);
			else ans[q.t] += query(q.y),rq.push_back(q);
		}
	}
	for (Q &q : lq) if (q.full==-1) update(q.y,-q.t);
	if (l<r) {
		cdq(l,mid,lq); cdq(mid+1,r,rq);
	}
}
int main () {
	int n,q; char c; int a,b;
	scanf ("%d %d",&n,&q);
	vector<Q> queries;
	zeroes.insert(0); zeroes.insert(n+1);
	for (int i = 1; i <= n; i++) {
		scanf (" %c",&c);
		if (c == '0') zeroes.insert(i);
	}
	for (int i = 1; i <= q; i++) {
		scanf (" %s %d",s,&a);
		if (s[0] == 'q') {
			scanf ("%d",&b); --b;
			if (*zeroes.lower_bound(a) > b) ans[i] = i;
			queries.push_back({a,b,i,0});
		} else {
			int cmpl = *prev(zeroes.lower_bound(a)) + 1;
			int cmpr = *zeroes.upper_bound(a) - 1;
			int sign = zeroes.count(a) ? -1 : 1;
			queries.push_back({cmpl,a,i*sign,-1});
			queries.push_back({cmpl,cmpr+1,i*-sign,-1});
			queries.push_back({a+1,a,i*-sign,-1});
			queries.push_back({a+1,cmpr+1,i*sign,-1});
			if (sign > 0) zeroes.insert(a);
			else zeroes.erase(a);
		}
	}
	cdq(1,n,queries);
	for (Q &qq : queries) if (!qq.full) printf ("%d\n",ans[qq.t]);
	return 0;
}
/*
turn into 1:
update cmpl <= l <= i, i <= r <= cmpr with -t
turn into 0:
update cmpl <= l <= i, i <= r <= cmpr with t
query is point query
bit of a special case if it is currently all 1s, need to add current time
transform with 2d diff array since point update range query is nicer
turn into 1:
update (cmpl,i) with -t
update (cmpl,cmpr+1) with t
update (i+1,i) with t
update(i+1,cmpr+1) with -t
turn into 0:
update (cmpl,i) with t
update (cmpl,cmpr+1) with -t
update (i+1,i) with -t
update(i+1,cmpr+1) with t
query:
find sum with l <= L, r <= R
also add t+1 if currently intact

seems like easy cdq bash
*/

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:52:8: 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:56:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf (" %c",&c);
   ~~~~~~^~~~~~~~~~
street_lamps.cpp:60:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf (" %s %d",s,&a);
   ~~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf ("%d",&b); --b;
    ~~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 415 ms 46116 KB Output is correct
2 Correct 475 ms 44860 KB Output is correct
3 Correct 588 ms 46400 KB Output is correct
4 Correct 1056 ms 55612 KB Output is correct
5 Correct 913 ms 47424 KB Output is correct
6 Correct 1091 ms 57660 KB Output is correct
7 Correct 679 ms 38652 KB Output is correct
8 Correct 408 ms 25936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 512 KB Output is correct
2 Correct 7 ms 512 KB Output is correct
3 Correct 7 ms 512 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 1510 ms 74052 KB Output is correct
6 Correct 1216 ms 53312 KB Output is correct
7 Correct 924 ms 44228 KB Output is correct
8 Correct 408 ms 27600 KB Output is correct
9 Correct 131 ms 19292 KB Output is correct
10 Correct 147 ms 22488 KB Output is correct
11 Correct 146 ms 21636 KB Output is correct
12 Correct 658 ms 41556 KB Output is correct
13 Correct 406 ms 26960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 8 ms 512 KB Output is correct
4 Correct 9 ms 640 KB Output is correct
5 Correct 568 ms 35664 KB Output is correct
6 Correct 843 ms 47832 KB Output is correct
7 Correct 1082 ms 57876 KB Output is correct
8 Correct 1402 ms 83640 KB Output is correct
9 Correct 567 ms 51516 KB Output is correct
10 Correct 601 ms 71832 KB Output is correct
11 Correct 578 ms 51684 KB Output is correct
12 Correct 595 ms 69912 KB Output is correct
13 Correct 594 ms 52412 KB Output is correct
14 Correct 597 ms 76344 KB Output is correct
15 Correct 662 ms 41920 KB Output is correct
16 Correct 406 ms 27600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 415 ms 46116 KB Output is correct
9 Correct 475 ms 44860 KB Output is correct
10 Correct 588 ms 46400 KB Output is correct
11 Correct 1056 ms 55612 KB Output is correct
12 Correct 913 ms 47424 KB Output is correct
13 Correct 1091 ms 57660 KB Output is correct
14 Correct 679 ms 38652 KB Output is correct
15 Correct 408 ms 25936 KB Output is correct
16 Correct 8 ms 512 KB Output is correct
17 Correct 7 ms 512 KB Output is correct
18 Correct 7 ms 512 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 1510 ms 74052 KB Output is correct
21 Correct 1216 ms 53312 KB Output is correct
22 Correct 924 ms 44228 KB Output is correct
23 Correct 408 ms 27600 KB Output is correct
24 Correct 131 ms 19292 KB Output is correct
25 Correct 147 ms 22488 KB Output is correct
26 Correct 146 ms 21636 KB Output is correct
27 Correct 658 ms 41556 KB Output is correct
28 Correct 406 ms 26960 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 8 ms 512 KB Output is correct
32 Correct 9 ms 640 KB Output is correct
33 Correct 568 ms 35664 KB Output is correct
34 Correct 843 ms 47832 KB Output is correct
35 Correct 1082 ms 57876 KB Output is correct
36 Correct 1402 ms 83640 KB Output is correct
37 Correct 567 ms 51516 KB Output is correct
38 Correct 601 ms 71832 KB Output is correct
39 Correct 578 ms 51684 KB Output is correct
40 Correct 595 ms 69912 KB Output is correct
41 Correct 594 ms 52412 KB Output is correct
42 Correct 597 ms 76344 KB Output is correct
43 Correct 662 ms 41920 KB Output is correct
44 Correct 406 ms 27600 KB Output is correct
45 Correct 211 ms 31568 KB Output is correct
46 Correct 286 ms 33412 KB Output is correct
47 Correct 596 ms 34524 KB Output is correct
48 Correct 1049 ms 55160 KB Output is correct
49 Correct 166 ms 25172 KB Output is correct
50 Correct 165 ms 25168 KB Output is correct
51 Correct 166 ms 25688 KB Output is correct
52 Correct 174 ms 26204 KB Output is correct
53 Correct 171 ms 26320 KB Output is correct
54 Correct 170 ms 26448 KB Output is correct