답안 #136609

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
136609 2019-07-25T18:21:12 Z liwi 가로등 (APIO19_street_lamps) C++11
100 / 100
4619 ms 352492 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define all(a) a.begin(),a.end()
#define println printf("\n");
#define readln(x) getline(cin,x);
#define pb push_back
#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
#define mp make_pair
#define fastio cin.tie(0); cin.sync_with_stdio(0);

#define MAXN 300005
#define MAXS 22000000

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef unordered_map<int,int> umii;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef pair<int,pii> triple;
typedef int8_t byte;

mt19937 g1(time(0));

int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}

ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
ll fpow(ll  b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}

int cnt;

struct node{
//	int l,r;
	int lft,rgt;
//	ll lazy0,lazy1;
	int sum;
	short cnt; //lazy0 for sum, lazy1 for cnt
//	node *lft,*rgt;
//
//	node(int l, int r){
//		this->l = l;
//		this->r = r;
//		this->lazy0 = this->lazy1 = this->sum = this->cnt = 0;
//		lft = rgt = 0;
//	}
};

int len,num_q,arr[MAXN],rt[MAXN];
node seg[MAXS];
set<int> zeros;
//
//inline void build_needed(int rt){
//	if(!seg[rt].lft){
//		seg[rt].lft = ++cnt;
//		seg[cnt] = {0,0,0,0};
//	}
//	if(!seg[rt].rgt){
//		seg[rt].rgt = ++cnt;
//		seg[cnt] = {0,0,0,0};
//	}
//}

//inline void push_down(int rt){
//	build_needed(rt);
//	seg[seg[rt].lft].lazy0+=seg[rt].lazy0, seg[seg[rt].rgt].lazy0+=seg[rt].lazy0;
//	seg[seg[rt].lft].sum+=seg[rt].lazy0, seg[seg[rt].rgt].sum+=seg[rt].lazy0;
//
//	seg[seg[rt].lft].lazy1+=seg[rt].lazy1, seg[seg[rt].rgt].lazy1+=seg[rt].lazy1;
//	seg[seg[rt].lft].cnt+=seg[rt].lazy1, seg[seg[rt].rgt].cnt+=seg[rt].lazy1;
//
//	seg[rt].lazy0 = seg[rt].lazy1 = 0;
//}

void update(int sl, int sr, int pos, int rt, int sumv, int cntv){
	if(sl == sr){
		seg[rt].sum+=sumv;
		seg[rt].cnt+=cntv;
		return;
	}
//	if(seg[rt].lazy0 || seg[rt].lazy1) push_down(rt);
//	build_needed(rt);
	int mid = (sl+sr)>>1;
	if(pos <= mid){
		if(!seg[rt].lft){
			seg[rt].lft = ++cnt;
			seg[cnt] = {0,0,0,0};
		}
		update(sl,mid,pos,seg[rt].lft,sumv,cntv);
	}else{
		if(!seg[rt].rgt){
			seg[rt].rgt = ++cnt;
			seg[cnt] = {0,0,0,0};
		}
		update(mid+1,sr,pos,seg[rt].rgt,sumv,cntv);
	}
	seg[rt].sum = seg[seg[rt].lft].sum+seg[seg[rt].rgt].sum;
	seg[rt].cnt = seg[seg[rt].lft].cnt+seg[seg[rt].rgt].cnt;
}

pll query(int sl, int sr, int pos, int rt){
	if(sl == sr){
		return mp(seg[rt].sum,seg[rt].cnt);
//		if(s == 0) return seg[rt].sum;
//		return seg[rt].cnt;
	}
//	if(seg[rt].lazy0 || seg[rt].lazy1) push_down(rt);
//	build_needed(rt);
	int mid = (sl+sr)>>1;
	if(pos <= mid){
		if(!seg[rt].lft) return mp(0,0);
		return query(sl,mid,pos,seg[rt].lft);
	}
	if(!seg[rt].rgt) return mp(seg[seg[rt].lft].sum,seg[seg[rt].lft].cnt);
	pll t = query(mid+1,sr,pos,seg[rt].rgt);
	t.first+=seg[seg[rt].lft].sum;
	t.second+=seg[seg[rt].lft].cnt;
	return t;
}

inline void bupdate(int xl, int xr, int l, int r, int sv, int cv){
//	for(int i=xl; i<=xr; i++)
//		bit[i].update(l,r,bit[i].rt,v);
	for(int i=xl; i<=len+1; i+=i&-i){
		if(!rt[i]){
			rt[i] = ++cnt;
			seg[cnt] = {0,0,0,0};
		}
		update(1,len+1,l,rt[i],sv,cv);
		if(r+1 <= len+1)
			update(1,len+1,r+1,rt[i],-sv,-cv);
	}
//		bit[i].update(l,r,bit[i].rt,v,s);
//	if(r < len+1){
		for(int i=xr+1; i<=len+1; i+=i&-i){
			if(!rt[i]){
				rt[i] = ++cnt;
				seg[cnt] = {0,0,0,0};
			}
			update(1,len+1,l,rt[i],-sv,-cv);
			if(r+1 <= len+1)
				update(1,len+1,r+1,rt[i],sv,cv);
//		update(1,len+1,l,r,rt[i],-v,s);
		}
//	}
//		bit[i].update(l,r,bit[i].rt,-v,s);
}

inline pll bquery(int a, int b){
	pll res = mp(0,0); //sum,cnt
//	res = bit[a].query(b,bit[a].rt);
	for(int i=a; i>0; i-=i&-i){
		if(!rt[i]) continue;
		pll t = query(1,len+1,b,rt[i]);
		res.first+=t.first;
		res.second+=t.second;
	}
//		res+=bit[i].query(b,bit[i].rt,s);
	return res;
}

//inline void cupdate(int xl, int xr, int l, int r, ll v){
////	for(int i=xl; i<=xr; i++)
////		cnt[i].update(l,r,cnt[i].rt,v);
//	for(int i=xl; i<=len+1; i+=i&-i)
//		cnt[i].update(l,r,cnt[i].rt,v);
//	for(int i=xr+1; i<=len+1; i+=i&-i)
//		cnt[i].update(l,r,cnt[i].rt,-v);
//}
//
//inline ll cquery(int a, int b){
//	ll res = 0;
////	res = cnt[a].query(b,cnt[a].rt);
//	for(int i=a; i>0; i-=i&-i)
//		res+=cnt[i].query(b,cnt[i].rt);
//	return res;
//}

int main(){
//	freopen("28","r",stdin);
//	fastio; cin>>len>>num_q;
	scanf("%d %d",&len,&num_q);
	for(int i=1; i<=len; i++){
		char c; scanf(" %c",&c); // cin>>c;
		arr[i] = c-'0';
		if(arr[i] == 0) zeros.insert(i);
	}
//	for(int i=1; i<=len+1; i++){
//		rt[i] = ++cnt;
//		seg[cnt] = {0,0,0,0};
////		bit[i].rt = new node(1,len+1);
////		cnt[i].rt = new node(1,len+1,0,0);
////		bit[i].build(1,len+1,1);
////		cnt[i].build(1,len+1,1);
//	}
	zeros.insert(0); zeros.insert(len+1);
//	cupdate(1,len+1,1,len+1,1);
//	bupdate(1,len+1,1,len+1,0,1);
//	int lst = 0;
//	for(int a=1; a<=len; a++){
//		if(arr[a] == 0){
//			zeros.insert(a);
//			int l = lst, r = len+1;
////			update(bit,l+1,a,a+1,r-1,i);
////			cupdate(l+1,a,a+1,r,-1);
//			bupdate(l+1,a,a+1,r,0,-1);
//			lst = a;
////			printf("R1: %d   R2: %d    C1: %d    C2: %d    V: %d\n",l+1,a,a+1,r,-1);
//		}
//	}
	int lst = 0;
	for(int a=1; a<=len; a++){
		if(arr[a] == 0) lst = a;
		if(arr[a] == 1){
			int r = a+1;
			int l = lst;
			bupdate(l+1,a,a+1,r,0,1);
//				bupdate(l+1,a,a+1,r,1,1);
		}
	}
	for(int i=1; i<=num_q; i++){
		char op[10]; int a; scanf(" %s %d",op,&a);
//		string op; int a; cin>>op>>a;
		if(op[0] == 't'){
//			int l; // = *(--zeros.lower_bound(a));
			auto rit = zeros.upper_bound(a);
			if(arr[a] == 0){
				int r = *rit;
				int l = *(----rit);
				bupdate(l+1,a,a+1,r,-i,1);
//				bupdate(l+1,a,a+1,r,1,1);
				zeros.erase(a);
				//(previous value)+(t-i)
				//(previous value)+t-i
			}else{
				int r = *rit;
				int l = *(--rit);
				bupdate(l+1,a,a+1,r,i,-1);
//				bupdate(l+1,a,a+1,r,-1,1);
				zeros.insert(a);
				//(previous value)-(t-i)
				//(previous value)-t+i
			}
			arr[a] = !arr[a];
		}else{
			int b; scanf(" %d",&b); //cin>>b;
			pll t = bquery(a,b);
			ll f = t.second*i;
			ll s = t.first;
//			if(f+s < 0) assert(false);
//			cout<<f+s<<endl;
			printf("%lld\n",f+s);
		}
	}
//	cout<<"lol"<<endl;
}
/*
5 1
11111
query 1 2
ans=1

5 4
00001
toggle 3
toggle 2
toggle 4
query 2 6
ans=1
 */

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:201:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&len,&num_q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:203:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   char c; scanf(" %c",&c); // cin>>c;
           ~~~~~^~~~~~~~~~
street_lamps.cpp:241:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   char op[10]; int a; scanf(" %s %d",op,&a);
                       ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:265:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    int b; scanf(" %d",&b); //cin>>b;
           ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 268 ms 2124 KB Output is correct
2 Correct 356 ms 2244 KB Output is correct
3 Correct 726 ms 5392 KB Output is correct
4 Correct 3289 ms 192284 KB Output is correct
5 Correct 3378 ms 218984 KB Output is correct
6 Correct 3720 ms 195840 KB Output is correct
7 Correct 273 ms 17260 KB Output is correct
8 Correct 3628 ms 352132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 828 KB Output is correct
2 Correct 5 ms 760 KB Output is correct
3 Correct 5 ms 760 KB Output is correct
4 Correct 5 ms 888 KB Output is correct
5 Correct 4126 ms 228696 KB Output is correct
6 Correct 3513 ms 224728 KB Output is correct
7 Correct 2847 ms 220968 KB Output is correct
8 Correct 2991 ms 352196 KB Output is correct
9 Correct 111 ms 4060 KB Output is correct
10 Correct 120 ms 4404 KB Output is correct
11 Correct 122 ms 4600 KB Output is correct
12 Correct 273 ms 19428 KB Output is correct
13 Correct 2972 ms 352492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 4 ms 760 KB Output is correct
3 Correct 5 ms 764 KB Output is correct
4 Correct 6 ms 760 KB Output is correct
5 Correct 1231 ms 169196 KB Output is correct
6 Correct 2207 ms 186576 KB Output is correct
7 Correct 3162 ms 197324 KB Output is correct
8 Correct 4619 ms 205464 KB Output is correct
9 Correct 421 ms 3820 KB Output is correct
10 Correct 343 ms 3320 KB Output is correct
11 Correct 423 ms 3936 KB Output is correct
12 Correct 342 ms 3328 KB Output is correct
13 Correct 423 ms 4052 KB Output is correct
14 Correct 343 ms 3448 KB Output is correct
15 Correct 275 ms 19276 KB Output is correct
16 Correct 2967 ms 350992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 268 ms 2124 KB Output is correct
9 Correct 356 ms 2244 KB Output is correct
10 Correct 726 ms 5392 KB Output is correct
11 Correct 3289 ms 192284 KB Output is correct
12 Correct 3378 ms 218984 KB Output is correct
13 Correct 3720 ms 195840 KB Output is correct
14 Correct 273 ms 17260 KB Output is correct
15 Correct 3628 ms 352132 KB Output is correct
16 Correct 5 ms 828 KB Output is correct
17 Correct 5 ms 760 KB Output is correct
18 Correct 5 ms 760 KB Output is correct
19 Correct 5 ms 888 KB Output is correct
20 Correct 4126 ms 228696 KB Output is correct
21 Correct 3513 ms 224728 KB Output is correct
22 Correct 2847 ms 220968 KB Output is correct
23 Correct 2991 ms 352196 KB Output is correct
24 Correct 111 ms 4060 KB Output is correct
25 Correct 120 ms 4404 KB Output is correct
26 Correct 122 ms 4600 KB Output is correct
27 Correct 273 ms 19428 KB Output is correct
28 Correct 2972 ms 352492 KB Output is correct
29 Correct 4 ms 632 KB Output is correct
30 Correct 4 ms 760 KB Output is correct
31 Correct 5 ms 764 KB Output is correct
32 Correct 6 ms 760 KB Output is correct
33 Correct 1231 ms 169196 KB Output is correct
34 Correct 2207 ms 186576 KB Output is correct
35 Correct 3162 ms 197324 KB Output is correct
36 Correct 4619 ms 205464 KB Output is correct
37 Correct 421 ms 3820 KB Output is correct
38 Correct 343 ms 3320 KB Output is correct
39 Correct 423 ms 3936 KB Output is correct
40 Correct 342 ms 3328 KB Output is correct
41 Correct 423 ms 4052 KB Output is correct
42 Correct 343 ms 3448 KB Output is correct
43 Correct 275 ms 19276 KB Output is correct
44 Correct 2967 ms 350992 KB Output is correct
45 Correct 103 ms 2760 KB Output is correct
46 Correct 173 ms 2848 KB Output is correct
47 Correct 1395 ms 65648 KB Output is correct
48 Correct 2961 ms 194420 KB Output is correct
49 Correct 128 ms 4220 KB Output is correct
50 Correct 131 ms 4228 KB Output is correct
51 Correct 132 ms 4316 KB Output is correct
52 Correct 132 ms 4216 KB Output is correct
53 Correct 131 ms 4216 KB Output is correct
54 Correct 133 ms 4244 KB Output is correct