QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#392302 | #6739. Teleport | Kir1same | TL | 412ms | 113052kb | C++17 | 6.5kb | 2024-04-17 14:17:25 | 2024-04-17 14:17:26 |
Judging History
answer
#include<bits/stdc++.h>
//#pragma GCC optimize(3,"Ofast","inline")
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<db,db> pdd;
typedef pair<ll,ll> pll;
void init();
void debug();
#define Clear(a) memset(a,0,sizeof(a))
#define pb(x) push_back(x)
#define INF 1e9+10
const int N = 5e3+10;
const int M = 13210;
const int maxn = 3e5 +10;
const ll mod = 998244353;
struct Tag {
ll mn = 1e9;
void apply(const Tag &t) {
mn = min(mn ,t.mn);
}
};
struct Info {
ll mn=1e9;
void apply(const Tag &t) {
mn = min(mn, t.mn);
}
};
Info operator+(const Info &a, const Info &b) {
Info ret;
ret.mn = min(a.mn, b.mn);
return ret;
}
struct F{
bool operator()(Info info){
return 1;
}
};
/**
* @brief 单点修改,区间用Tag修改,区间询问 -带Lazy Tag线段树\n
* with binary search
*/
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
/**
* @brief [l,r) 询问
*/
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
/**
* @brief 区间[l,r)找到第一个满足条件F的点
* @param pred:functor 有bool operator(Info)
*/
template<class F>
int findFirst(int l, int r, F pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F pred) {
return findLast(1, 0, n, l, r, pred);
}
};
LazySegmentTree<Info, Tag> tr[N*2];
ll dp[N][N];
pll vec[] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
char ma[N][N];
int main()
{
int n,k;
cin>>n>>k;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
cin>>ma[i][j], dp[i][j] = 1e9;
for (int i=1;i<2*n;i++)
{
tr[i] = LazySegmentTree<Info, Tag>(n);
}
dp[0][0] = 0;
for (int x=0;x<n;x++)
{
for (int y=0;y<=x;y++)
{
int i=y, j=x-y;
if (ma[i][j] == '*') continue;
dp[i][j] = min(dp[i][j], tr[i-j+n].rangeQuery(i,i+1).mn);
for (auto[a,b]: vec)
{
if (i+a>=0 && i+a<n && j+b>=0 && j+b<n)
dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + 1);
}
tr[i-j+n].rangeApply(i, min(i+k/2,n-1)+1, {dp[i][j]+1});
tr[j+1-i+n].rangeApply(j+1, min(j+1+(k-1)/2,n-1)+1, {dp[i][j]+1});
}
}
for (int x=1; x<n; x++)
{
for (int y=0; x+y<n; y++)
{
int i=x+y, j=n-1-y;
if (ma[i][j] == '*') continue;
dp[i][j] = min(dp[i][j], tr[i-j+n].rangeQuery(i,i+1).mn);
for (auto[a,b]: vec)
{
if (i+a>=0 && i+a<n && j+b>=0 && j+b<n)
dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + 1);
}
tr[i-j+n].rangeApply(i, min(i+k/2,n-1)+1, {dp[i][j]+1});
tr[j+1-i+n].rangeApply(j+1, min(j+1+(k-1)/2,n-1)+1, {dp[i][j]+1});
}
}
if (dp[n-1][n-1] == 1e9){
cout<<-1<<endl;
}
else{
cout<<dp[n-1][n-1]<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6228kb
input:
3 2 .*. .*. ...
output:
3
result:
ok 1 number(s): "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 6460kb
input:
3 3 .*. .*. ...
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 290ms
memory: 109564kb
input:
961 4 ...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....
output:
540
result:
ok 1 number(s): "540"
Test #4:
score: 0
Accepted
time: 412ms
memory: 112760kb
input:
975 434 .*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...
output:
5
result:
ok 1 number(s): "5"
Test #5:
score: 0
Accepted
time: 318ms
memory: 110116kb
input:
966 19 ..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....
output:
104
result:
ok 1 number(s): "104"
Test #6:
score: 0
Accepted
time: 392ms
memory: 112624kb
input:
973 199 ..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 380ms
memory: 113052kb
input:
984 95 .....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....
output:
21
result:
ok 1 number(s): "21"
Test #8:
score: -100
Time Limit Exceeded
input:
2996 2 ..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...