結果
| 問題 |
No.1170 Never Want to Walk
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-01-09 16:45:26 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 6,909 bytes |
| コンパイル時間 | 2,397 ms |
| コンパイル使用メモリ | 215,696 KB |
| 最終ジャッジ日時 | 2025-02-18 16:47:54 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 WA * 15 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <typename T> using min_priority_queue = priority_queue<T,vector<T>,greater<T>>;
template<typename T>
void printv(vector<T> &v){
bool b = false;
for(auto i : v){
if(b) cout << " ";
else b = true;
cout << i;
}
cout << endl;
}
int64_t rand64(){
int64_t l = rand();
int64_t r = rand();
return (l<<31)+r;
}
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <typename T>
struct monoid_min{
T val;
monoid_min(){
;
}
monoid_min(T x){
val = x;
}
monoid_min e(){
return monoid_min(numeric_limits<T>::max());
}
monoid_min operator*(monoid_min other){
if(val < other.val) return monoid_min(val);
return other;
}
bool operator==(monoid_min other){
return val == other.val;
}
bool operator!=(monoid_min other){
return val != other.val;
}
};
template <typename monoid>
struct mapping_add{
monoid val;
mapping_add(){
val = monoid().e();
}
mapping_add(monoid x){
val = x;
}
mapping_add id(){
return mapping_add();
}
monoid map(monoid s){
return monoid(s*val);
}
//写像 other*self
mapping_add composition(mapping_add other){
return mapping_add(val*other.val);
}
bool operator==(mapping_add other){
return val == other.val;
}
bool operator!=(mapping_add other){
return val != other.val;
}
};
template <typename monoid, typename mapping>
struct lazySegtree{
int sz;
vector<monoid> dat;
vector<mapping> lazy;
lazySegtree(int n){
sz = 1;
while(sz <= n) sz *= 2;
dat.resize(2*sz-1, monoid().e());
lazy.resize(2*sz-1, mapping().id());
}
void eval(int k, int l, int r){
if(lazy[k] != mapping().id()){
dat[k] = lazy[k].map(dat[k]);
if(r - l > 1){
lazy[2*k+1] = lazy[2*k+1].composition(lazy[k]);
lazy[2*k+2] = lazy[2*k+2].composition(lazy[k]);
}
}
lazy[k] = mapping().id();
return;
}
void sub_apply(int a, int b, mapping f, int k, int l, int r){
eval(k, l, r);
if(b <= l || r <= a) return;
if(a <= l && r <= b){
lazy[k] = lazy[k].composition(f);
eval(k, l, r);
}
else{
sub_apply(a, b, f, 2*k+1, l, (l+r)/2);
sub_apply(a, b, f, 2*k+2, (l+r)/2, r);
dat[k] = dat[2*k+1]*dat[2*k+2];
}
return;
}
void apply(int a, int b, mapping f){
sub_apply(a, b, f, 0, 0, sz);
return;
}
monoid sub_get(int a, int b, int k, int l, int r){
if(b <= l || r <= a) return monoid().e();
eval(k, l, r);
if(a <= l && r <= b) return dat[k];
monoid vl = sub_get(a, b, 2*k+1, l, (l+r)/2);
monoid vr = sub_get(a, b, 2*k+2, (l+r)/2, r);
return vl * vr;
}
monoid get(int a, int b){
return sub_get(a, b, 0, 0, sz);
}
};
struct UF {
vector<int> par;
vector<int> ran;
vector<int> sz;
UF(int n){
par.resize(n);
ran.resize(n);
sz.resize(n,1);
for(int i = 0; i < n; i++){
par[i] = i;
}
}
int find(int x){
if(par[x] == x) return x;
int rtn = find(par[x]);
par[x] = rtn;
return rtn;
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(ran[x] < ran[y]){
par[x] = y;
sz[y] += sz[x];
}
else{
par[y] = x;
sz[x] += sz[y];
if(ran[x] == ran[y]){
ran[x]++;
}
}
}
bool same(int x, int y){
return find(x) == find(y);
}
int siz(int x){
return sz[find(x)];
}
};
bool yn(bool b){
if(b) cout << "Yes" << endl;
else cout << "No" << endl;
return b;
}
bool debug;
bool randomInput;
bool debugOutput;
int numOfTestCase;
using ans_type = int;
void input(){
if(numOfTestCase > 1){
}
if(randomInput){
}
else{
}
return;
}
void output_input(){
;
}
ans_type calc(){
int N, A, B;
cin >> N >> A >> B;
int INF = 2001002003;
vector<int> X(N+2);
X[0] = -INF;
X[N+1] = INF;
for(int i = 1; i <= N; i++) cin >> X[i];
vector<tuple<int,int,int,int>> V(N);
for(int i = 0; i < N; i++){
int x = X[i+1];
auto itr1 = lower_bound(X.begin(), X.end(), x - B);
auto itr2 = prev(upper_bound(X.begin(), X.end(), x - A));
auto itr3 = lower_bound(X.begin(), X.end(), x + A);
auto itr4 = prev(upper_bound(X.begin(), X.end(), x + B));
int a1 = itr1 - X.begin() - 1;
int a2 = itr2 - X.begin() - 1;
int a3 = itr3 - X.begin() - 1;
int a4 = itr4 - X.begin() - 1;
V[i] = {a1, a2, a3, a4};
}
lazySegtree<monoid_min<int>, mapping_add<monoid_min<int>>> seg(N);
for(int i = N-1; i >= 0; i--){
auto[a, b, c, d] = V[i];
b++;
d++;
chmax(a, 0);
chmin(a, N);
chmax(b, 0);
chmin(b, N);
chmax(c, 0);
chmin(c, N);
chmax(d, 0);
chmin(d, N);
if(a < b) seg.apply(a, b, mapping_add<monoid_min<int>>(monoid_min<int>(i)));
if(c < d) seg.apply(c, d, mapping_add<monoid_min<int>>(monoid_min<int>(i)));
}
UF U(N);
for(int i = 0; i < N; i++){
int j = seg.get(i, i+1).val;
if(0 <= j && j < N) U.unite(i, j);
}
vector<int> ans(N);
for(int i = 0; i < N; i++){
ans[i] = U.siz(i);
}
for(int i : ans) cout << i << endl;
return ans_type();
}
ans_type calc_simple(){
return ans_type();
}
void output(ans_type ans){
return;
}
int main(){
debug = 0;
randomInput = 0;
debugOutput = 0;
numOfTestCase = 1;
srand(time(NULL));
cout << fixed << setprecision(12);
if(numOfTestCase == 0) cin >> numOfTestCase;
if(debug){
for(int i = 0; i < numOfTestCase; i++){
if(debugOutput) cout << string(16, '-') << endl;
input();
ans_type ans = calc();
ans_type ansSimple = calc_simple();
if(ans != ansSimple){
output_input();
output(ans);
output(ansSimple);
}
}
}
else{
for(int i = 0; i < numOfTestCase; i++){
input();
output(calc());
}
}
return 0;
}