QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#125259 | #6739. Teleport | upsolveupsolve | AC ✓ | 441ms | 113892kb | C++20 | 4.1kb | 2023-07-16 13:20:02 | 2023-07-16 13:20:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=5005,INF=1<<29;
// https://www.dropbox.com/s/1zxohqaxrb87uft/Gifted_Infants_The_University_of_Tokyo___erated_files-job_14.pdf?dl=0
using uint = unsigned int ; using ll = long long ; using ull = unsigned long long ; template < class T > using V = vector <T>; template < class T > using VV = V <V<T>>;
int popcnt ( uint x ) { return __builtin_popcount(x); } int popcnt ( ull x ) { return __builtin_popcountll(x); } int bsr ( uint x ) { return 31 - __builtin_clz(x); } int bsr ( ull x ) { return 63 - __builtin_clzll(x); } int bsf ( uint x ) { return __builtin_ctz(x); } int bsf ( ull x ) { return __builtin_ctzll(x); }
struct FastSet { static constexpr uint B = 64 ; int n, lg; VV<ull> seg; FastSet( int _n) : n(_n) { do { seg.push_back(V<ull>((_n + B - 1 ) / B)); _n = (_n + B - 1 ) / B; } while (_n > 1 ); lg = seg.size(); } bool operator []( int i) const { return (seg[ 0 ][i / B] >> (i % B) & 1 ) != 0 ; } void set ( int i) { for ( int h = 0 ; h < lg; h++) { seg[h][i / B] |= 1ULL << (i % B); i /= B; } } void reset ( int i) { for ( int h = 0 ; h < lg; h++) { seg[h][i / B] &= ~( 1ULL << (i % B)); if (seg[h][i / B]) break ; i /= B; } } int next ( int i) { for ( int h = 0 ; h < lg; h++) { if (i / B == seg[h].size()) break ; ull d = seg[h][i / B] >> (i % B); if (!d) { i = i / B + 1 ; continue ; } i += bsf(d); for ( int g = h - 1 ; g >= 0 ; g--) { i *= B; i += bsf(seg[g][i / B]); } return i; } return n; } int prev ( int i) { i--; for ( int h = 0 ; h < lg; h++) { if (i == -1 ) break ; ull d = seg[h][i / B] << ( 63 - i % 64 ); if (!d) { i = i / B - 1 ; continue ; } i += bsr(d) - (B - 1 ); for ( int g = h - 1 ; g >= 0 ; g--) { i *= B; i += bsr(seg[g][i / B]); } return i; } return -1 ; } };
int dis[MAX][MAX];
vector<int> dh={0,1,0,-1},dw={1,0,-1,0};
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int N,K;cin>>N>>K;
vector<string> S(N);
for(int i=0;i<N;i++) cin>>S[i];
queue<pair<int,int>> Q;
for(int i=0;i<MAX;i++) for(int j=0;j<MAX;j++) dis[i][j]=INF;
vector<int> L(N+N+2),R(N+N+2);
for(int w=0;w<N;w++) L[w-0+N]=0;
for(int h=0;h<N;h++) L[h-0+N]=h;
for(int w=0;w<N;w++) R[w-(N-1)+N]=N-1;
for(int h=0;h<N;h++) R[(N-1)-h+N]=h;
vector<FastSet> SE(N+N+2,N);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(S[i][j]=='.') SE[j-i+N].set(i);
}
}
dis[0][0]=0;
Q.push(mp(0,0));
while(!Q.empty()){
auto [h,w]=Q.front();Q.pop();
int d=dis[h][w];
if(h==N-1&&w==N-1){
cout<<d<<endl;
return 0;
}
for(int k=0;k<4;k++){
int toh=h+dh[k],tow=w+dw[k];
if(toh<0||toh>=N||tow<0||tow>=N) continue;
if(S[toh][tow]=='*') continue;
if(chmin(dis[toh][tow],dis[h][w]+1)){
Q.push(mp(toh,tow));
SE[tow-toh+N].reset(toh);
}
}
int can1=(K+1)/2;
int can2=K/2;
{
int sh=w+1,sw=h,now=sh;
while(1){
int x=SE[sw-sh+N].next(now);
if(x==N||x>=sh+can1) break;
dis[x][sw-sh+x]=d+1;
Q.push(mp(x,sw-sh+x));
SE[sw-sh+N].reset(x);
now=x;
}
}
{
int sh=h+1,sw=w+1,now=sh;
while(1){
int x=SE[sw-sh+N].next(now);
if(x==N||x>=sh+can2) break;
dis[x][sw-sh+x]=d+1;
Q.push(mp(x,sw-sh+x));
SE[sw-sh+N].reset(x);
now=x;
}
}
}
cout<<-1<<endl;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 101320kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 101364kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 25ms
memory: 102864kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 10ms
memory: 102888kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 22ms
memory: 102852kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 4ms
memory: 102932kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 12ms
memory: 102848kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: 0
Accepted
time: 441ms
memory: 113892kb
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...
output:
3354
result:
ok 1 number(s): "3354"
Test #9:
score: 0
Accepted
time: 59ms
memory: 113224kb
input:
2905 1023 .........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........
output:
6
result:
ok 1 number(s): "6"
Test #10:
score: 0
Accepted
time: 56ms
memory: 113872kb
input:
2978 104 .*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...
output:
58
result:
ok 1 number(s): "58"