답안 #133447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
133447 2019-07-20T17:27:39 Z liwi 가로등 (APIO19_street_lamps) C++11
0 / 100
784 ms 524292 KB
#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 105

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;}

struct node{
	int l,r;
	ll lazy,sum;
	node *lft,*rgt;

	node(int l, int r, ll lazy, ll sum){
		this->l = l;
		this->r = r;
		this->lazy = lazy;
		this->sum = sum;
		lft = rgt = 0;
	}
};

struct segTree{
	node *rt;
	inline void push_down(node *n){
		int mid = (n->l+n->r)/2;
		if(!n->lft) n->lft = new node(n->l,mid,0,0);
		if(!n->rgt) n->rgt = new node(mid+1,n->r,0,0);
		n->lft->lazy+=n->lazy, n->rgt->lazy+=n->lazy;
		n->lft->sum+=n->lazy, n->rgt->sum+=n->lazy;
		n->lazy = 0;
	}
	void update(int l, int r, node *n, ll v){
		if(n->l == l && n->r == r){
			n->lazy+=v, n->sum+=v;
			return;
		}
		if(n->lazy) push_down(n);
		int mid = (n->l+n->r)/2;
		if(r <= mid){
			if(!n->lft) n->lft = new node(n->l,mid,0,0);
			update(l,r,n->lft,v);
		}else if(l > mid){
			if(!n->rgt) n->rgt = new node(mid+1,n->r,0,0);
			update(l,r,n->rgt,v);
		}else{
			if(!n->lft) n->lft = new node(n->l,mid,0,0);
			if(!n->rgt) n->rgt = new node(mid+1,n->r,0,0);
			update(l,mid,n->lft,v);
			update(mid+1,r,n->rgt,v);
		}
	}
	ll query(int pos, node *n){
		if(n->l == n->r) return n->sum;
		if(n->lazy) push_down(n);
		int mid = (n->l+n->r)/2;
		if(pos <= mid){
			if(!n->lft) n->lft = new node(n->l,mid,0,0);
			return query(pos,n->lft);
		}
		if(!n->rgt) n->rgt = new node(mid+1,n->r,0,0);
		return query(pos,n->rgt);
	}
};

int len,num_q,arr[MAXN];
segTree bit[MAXN],cnt[MAXN];
set<int> zeros;

inline void update(int xl, int xr, int l, int r, ll v){
	for(int i=xl; i<=len+1; i+=i&-i)
		bit[i].update(l,r,bit[i].rt,v);
	for(int i=xr+1; i<=len+1; i+=i&-i)
		bit[i].update(l,r,bit[i].rt,-v);
}

inline ll query(int a, int b){
	ll res = 0;
	for(int i=a; i>0; i-=i&-i)
		res+=bit[i].query(b,bit[i].rt);
	return res;
}

inline void cupdate(int xl, int xr, int l, int r, ll 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;
	for(int i=a; i>0; i-=i&-i)
		res+=cnt[i].query(b,cnt[i].rt);
	return res;
}

int main(){
	fastio; cin>>len>>num_q;
	for(int i=1; i<=len; i++){
		char c; cin>>c;
		arr[i] = c-'0';
	}
	for(int i=1; i<=len+1; i++){
		bit[i].rt = new node(1,len+1,0,0);
		cnt[i].rt = new node(1,len+1,0,0);
	}
	zeros.insert(0); zeros.insert(len+2);
	cupdate(1,len+1,1,len+1,1);
	for(int a=1; a<=len; a++){
		if(arr[a] == 0){
			zeros.insert(a);
			int l = *(--zeros.lower_bound(a));
			int r = len+2;
//			update(bit,l+1,a,a+1,r-1,i);
			cupdate(l+1,a,a+1,r-1,-1);
		}
	}
	for(int i=1; i<=num_q; i++){
		string op; int a; cin>>op>>a;
		if(op == "toggle"){
			int l = *(--zeros.lower_bound(a));
			int r = *zeros.upper_bound(a);
			if(arr[a] == 0){
				update(l+1,a,a+1,r-1,-i);
				cupdate(l+1,a,a+1,r-1,1);
				zeros.erase(a);
				//(previous value)+(t-i)
				//(previous value)+t-i
			}else{
				update(l+1,a,a+1,r-1,i);
				cupdate(l+1,a,a+1,r-1,-1);
				zeros.insert(a);
				//(previous value)-(t-i)
				//(previous value)-t+i
			}
		}else{
			int b; cin>>b;
			ll f = cquery(a,b)*i;
			ll s = query(a,b);
			cout<<f+s<<endl;
		}
	}
//	cout<<"lol"<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 784 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 709 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 784 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -