답안 #928857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
928857 2024-02-17T07:06:11 Z FanOfWilliamLin Election (BOI18_election) C++17
0 / 100
3 ms 4444 KB
#include <bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define ll long long
#define ld long double
#define ar array
#define vt vector
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define lla(v) v.rbegin(), v.rend()

ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
	while(lb<rb) {
		ll mb=(lb+rb)/2;
		f(mb)?rb=mb:lb=mb+1; 
	} 
	return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
	while(lb<rb) {
		ll mb=(lb+rb+1)/2;
		f(mb)?lb=mb:rb=mb-1; 
	} 
	return lb;
}

const ll INF=1000000000000000000LL;
const ll MOD=1e9+7;
const ll M=(1ll<<61)-1;
//const ll MOD=998244353; //sometimes need to be used

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
const ll base=uniform_int_distribution<ll>(0, M-1)(rng);

ll bpow(ll a, ll b, ll DOM) {
	a%=DOM;
	ll res=1;
	while(b>0) {
		if(b&1)
			res=res*a%DOM;
		a=a*a%DOM;
		b>>=1;
	}
	return res;
}

int gcd_x, gcd_y, gcd_d;

int gcd(int a, int b) {
	if (!b) {
        gcd_x=1;
        gcd_y=0;
        return a;
    }
    gcd_d=gcd(b, a%b);
	int tmp=gcd_x;
	gcd_x=gcd_y;
	gcd_y=tmp-(a/b)*gcd_y;
    return gcd_d;
}

ll floor_div(ll x, ll y) {
	assert(y!=0);
	if (y<0) {
		y=-y;
		x=-x;
	}
	if (x>=0) return x/y;
	return (x+1)/y-1;
}
ll ceil_div(ll x, ll y) {
	assert(y!=0);
	if (y<0) {
		y=-y;
		x=-x;
	}
	if (x<=0) {
		return x/y;
	}
	return (x-1)/y+1;
}

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

const int mxN=5e5+1;
int n, q, pref[mxN], suf[mxN];
string s;

// CAP : +1
// TONY: -1

struct Nodes {
	int mn;
	int pos;
} ST1[1<<21], ST2[1<<21];

Nodes combine1(const Nodes& a, const Nodes& b) {
	Nodes res;
	if(a.mn>b.mn) {
		res.mn=b.mn;
		res.pos=b.pos;
	}
	else if(a.mn<b.mn) {
		res.mn=a.mn;
		res.pos=a.pos;
	}
	else {
		res.mn=a.mn;
		res.pos=min(a.pos, b.pos);
	}
	return res;
}

Nodes combine2(const Nodes& a, const Nodes& b) {
	Nodes res;
	res.mn=min(a.mn, b.mn);
	if(a.mn>b.mn) {
		res.mn=b.mn;
		res.pos=b.pos;
	}
	else if(a.mn<b.mn) {
		res.mn=a.mn;
		res.pos=a.pos;
	}
	else {
		res.mn=a.mn;
		res.pos=max(a.pos, b.pos);
	}
	return res;
}

void bld1(int id=1, int l=1, int r=n) {
	if(l==r) {
		ST1[id].mn=pref[l];
		ST1[id].pos=l;
		return;
	}
	int mid=(l+r)/2;
	bld1(2*id, l, mid);
	bld1(2*id+1, mid+1, r);
	ST1[id]=combine1(ST1[2*id], ST1[2*id+1]);
}

void bld2(int id=1, int l=1, int r=n) {
	if(l==r) {
		ST2[id].mn=suf[l];
		ST2[id].pos=l;
		return;
	}
	int mid=(l+r)/2;
	bld2(2*id, l, mid);
	bld2(2*id+1, mid+1, r);
	ST2[id]=combine2(ST2[2*id], ST2[2*id+1]);
}

Nodes qury1(int u, int v, int id=1, int l=1, int r=n) {
	if(u>r||l>v) {
		return {INF, -1};
	}
	if(l>=u&&v>=r) {
		return ST1[id];
	}
	int mid=(l+r)/2;
	return combine1(qury1(u, v, 2*id, l, mid), qury1(u, v, 2*id+1, mid+1, r));
}

Nodes qury2(int u, int v, int id=1, int l=1, int r=n) {
	if(u>r||l>v) {
		return {INF, -1};
	}
	if(l>=u&&v>=r) {
		return ST2[id];
	}
	int mid=(l+r)/2;
	return combine2(qury2(u, v, 2*id, l, mid), qury2(u, v, 2*id+1, mid+1, r));
}

void solve() {
	//solve here
	cin >> n;
	cin >> s;
	s=" "+s;
	for(int i=1; i<=n; ++i) {
		if(s[i]=='C') {
			pref[i]=pref[i-1]+1;
		}
		else {
			pref[i]=pref[i-1]-1;
		}
	}
	for(int i=n; i>=1; --i) {
		if(s[i]=='C') {
			suf[i]=suf[i+1]+1;
		}
		else {
			suf[i]=suf[i+1]-1;
		}
	}
	bld1();
	bld2();
	cin >> q;
	while(q--) {
		int l, r;
		cin >> l >> r;
		int pos1=qury1(l, r).pos;
		int pos2=qury2(l, r).pos;
		int mn1=qury1(l, r).mn-pref[l-1];
		int mn2=qury2(l, r).mn-suf[r+1];
		if(mn1<0&&mn2<0) {
			cout << max(abs(mn1), abs(mn2)) << "\n";
		}
		else if(mn1<0) {
			cout << abs(mn1) << "\n";
		}
		else if(mn2<0) {
			cout << abs(mn2) << "\n";
		}
		else {
			cout << 0 << "\n";
		}
	}
}
 
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
 
	//freopen("template.inp", "r", stdin);
	//freopen("template.out", "w", stdout);	

	int TC=1;
	//cin >> TC;
	while(TC--) {
		solve();
	}
}

Compilation message

election.cpp: In function 'void solve()':
election.cpp:213:7: warning: unused variable 'pos1' [-Wunused-variable]
  213 |   int pos1=qury1(l, r).pos;
      |       ^~~~
election.cpp:214:7: warning: unused variable 'pos2' [-Wunused-variable]
  214 |   int pos2=qury2(l, r).pos;
      |       ^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4444 KB Output isn't correct
2 Halted 0 ms 0 KB -