Vjudge Contest0 @2018-08-04

本文精选了几道算法竞赛题目并提供了详细的解题思路与参考代码,涵盖了动态规划、线段树、数学技巧等多个方面。

前言

XY六题刚去,吴老师又发题下来了。不过幸好不是愚蠢的OI赛制,个人感觉还算可以。


A Spheres CodeChef - KSPHERES

题目描述

给你一些半球,有 N N 上半球和M下半球。你可以选择半径相同的一对上下半球组成一个球。一个半径严格小于另一个半径的球可以套在大球中。我们称一个有 D+1 D + 1 个球嵌套起来排成的序列为 D D 序列。求对于D{1,2,...,C},不同序列的数量。如果两个序列在同样位置上的两个球不同,那么这两个序列不同。如果构成两个球的上半球或下半球不同,那么这两个球不同。答案对 109+7 10 9 + 7 取模。

约定
  • 1N,M105 1 ≤ N , M ≤ 10 5
  • 1C103 1 ≤ C ≤ 10 3
  • 1RC 1 ≤ R ≤ C ,其中 R R 表示球的半径

分析

我们可以用一个桶排先求出组成一个半径的球的方案数。我们设对于第i种半径的球,组成它的方案数为 way[i] w a y [ i ] 。我们令 DP[i][j] D P [ i ] [ j ] 表示我们在前 i i 种球中,选出一个j序列的方案数,则 DP[i][j]=DP[i1][j1]way[i]+DP[i1][j] D P [ i ] [ j ] = D P [ i − 1 ] [ j − 1 ] ∗ w a y [ i ] + D P [ i − 1 ] [ j ] ,接下来我们就可以很轻松的求解了。

参考程序

注意:本题用桶排即可,简单不易错。

// vjudge 244023 A
#include <cstdio>
#include <algorithm>
typedef long long LL;
const int MAXN = 100005;
const int MAXC = 1005;
const int MOD = 1000000007;

int N, M, C, bu[MAXN], bl[MAXN], DP[MAXC][MAXC], B[MAXC], totb = 0;

int main() {
    scanf("%d%d%d", &N, &M, &C);
    int i, Ui, Li;
    for (i = 0; i < N; i++) {
        scanf("%d", &Ui);
        bu[Ui]++;
    }
    for (i = 0; i < M; i++) {
        scanf("%d", &Li);
        bl[Li]++;
    }
    int tmp, j;
    for (i = 1; i <= 1000; i++) {
        tmp = (LL)bu[i] * bl[i] % MOD;
        if (tmp) B[++totb] = tmp;
    }
    DP[0][0] = 1;
    for (i = 1; i <= totb || i <= C; ++i)
        for (DP[i][0] = j = 1; j <= i; j++) DP[i][j] = ((LL)DP[i - 1][j - 1] * B[i] % MOD + DP[i - 1][j]) % MOD;
    for (i = 1; i <= C; ++i) printf("%d ", DP[totb][i + 1]);
    putchar('\n');
    return 0;
}

B Sereja and Commands CodeChef - SEACO

题目描述

Sereja有一个长度为 n n 的序列A1,A2,...,An。初始时所有 Ai=0 A i = 0
Sereja在纸上写下了 m m 个操作,编号为1~ m m 。共有两类操作:

  • 1lr(1lrn):将下标在 [l,r] [ l , r ] 内的元素的值加1;

  • 2lr(1lrm 2 l r ( 1 ≤ l ≤ r ≤ m ):执行编号在 [l,r] [ l , r ] 内的所有操作,保证 r r 小于当前操作的编号。

请帮 Sereja 执行所有操作。
最终序列元素对109+7取模。

约定
  • 1n,m105 1 ≤ n , m ≤ 10 5

分析

乍一看这是一道线段树的题,但是我们并不要求在线查询,所以用差分即可。但是这个2号操作比较繁杂,如果直接模拟复杂度是不可想象的。但既然我们已经对数组用上了差分,为何不对操作也用一下差分呢?我们如果从前向后进行操作,那么当进行到2操作的时候,我们并不方便处理它要操作的操作区间;但若我们从后向前扫描这些操作,把差分的过程逆转过来,我们就可以统计出当前操作要被进行几次了,这样就成功转化了这个问题。

参考程序

// vjudge 244023 B
#include <cstdio>
#include <cstring>
const int MAXN = 100005;
const int MOD = 1000000007;
struct Opt {
    int t, l, r;
} O[MAXN];

int N, M, T, dop[MAXN], da[MAXN];

inline void plus(int & x, int dl) { x += dl; if (x >= MOD) x -= MOD; }
inline void subtrc(int & x, int dl) { x -= dl; if (x < 0) x += MOD; }
void solve();

int main() {
    scanf("%d", &T);
    while (T--) solve();
    return 0;
}

void solve() {
    scanf("%d%d", &N, &M);
//  初始化差分数组,dop是操作的差分数组,da是序列的差分数组
    memset(dop, 0, sizeof(int) * (M + 2));
    memset(da, 0, sizeof(int) * (N + 2));
    int i;
    for (i = 1; i <= M; ++i) scanf("%d%d%d", &O[i].t, &O[i].l, &O[i].r);
    int t = 1;
    for (i = M; i > 0; --i) {
        plus(t, dop[i]);
//      因为我们是倒着来的,所以堆操作的差分也要反过来做,这个很好理解
        if (O[i].t == 2) subtrc(dop[O[i].l - 1], t), plus(dop[O[i].r], t);
        else plus(da[O[i].l], t), subtrc(da[O[i].r + 1], t);
    }
    int Ai = 0;
    for (i = 1; i <= N; i++) {
        plus(Ai, da[i]);
        printf("%d ", Ai);
    }
    putchar('\n');
}

C Blocked websites CodeChef - WSITES01

这篇题解独立出来写。
传送门


D Cooking Schedule CodeChef - SCHEDULE

题目描述

大厨是个名扬四海的大厨,所有人都想吃他做的菜。
如你所知,做菜并不是件简单的事情,大厨每天都得做菜,累到不行。因此,大厨决定给自己放几天假。
大厨为接下来的 N N 天定制了一个计划。在第i天,如果 Ai=1 A i = 1 ,那么大厨会在那天烹饪一道美味佳肴;如果 Ai=0 A i = 0 ,那么大厨就会休息。
在大厨定下计划之后,他意识到他的计划并不完美:有些连着的日子里,大厨天天都得做菜,还是会很累;同时,也有时候大厨会连着休息好几天,这几天什么都不干,大厨也会觉得无聊。
因此大厨决定对这份计划做一些修改,但他也不想改太多地方,因此他决定最多修改 K K 天的安排。大厨会选出最多K天,对于选出的每一天 i i ,如果Ai=1,则将其改成 0;否则将其改成 1。
请你帮大厨写一个程序以决定修改哪些天的安排。修改之后应当保证,具有相同安排(即 Ai A i 相等)的连续一段的日子天数最少。

约定
  • 1T11,000 1 ≤ T ≤ 11 , 000
  • 1N1,000,000 1 ≤ N ≤ 1 , 000 , 000
  • 输入中每组数据的 N N 之和1,000,000
  • 0K1,000,000 0 ≤ K ≤ 1 , 000 , 000
  • 0Ai1 0 ≤ A i ≤ 1

分析

我们考虑动态规划,但是显然并不好进行转移。注意到题目要求是求最大值最小,那么我们考虑二分。那我们如何检查答案呢?我们可以预处理出原序列中连续的安排相同的天数,对于一个二分出来的天数 lim l i m ,我们对于原来序列中的每一块,每个 lim l i m 个改变那天的安排即可。(若要改变那一段最后一天则改变倒数第二天,并且对于 lim=1 l i m = 1 时进行特判)这样就解决了这个问题。

参考程序

// vjudge 244023 D
#include <cstdio>
#include <algorithm>
const int MAXN = 1000005;

int N, K, A[MAXN], M;
char input[MAXN];

void solve();
bool check(int lim);
bool check1();  // check1()是对1的情况特殊判断

int main() {
    int T;
    scanf("%d", &T);
    while (T--) solve();
    return 0;
}

void solve() {
    scanf("%d%d%s", &N, &K, input);
    char ch = input[0];
    int i, len = 1, lb = 1, ub = 0;
//  分块
    for (i = 1, M = 0; i < N; i++)
        if (input[i] == ch) ++len;
        else A[M++] = len, ub = std::max(ub, len), ch = input[i], len = 1;
    A[M++] = len, ub = std::max(ub, len);
    if (check1()) { puts("1"); return; }
    int mid;
//  二分,当前二分区间为(lb, ub]
    while (ub - lb > 1) {
        mid = lb + ub >> 1;
        if (check(mid)) ub = mid;
        else lb = mid;
    }
    printf("%d\n", ub);
}

bool check(int lim) {
    int res = 0;
    ++lim;
    for (int i = 0; i < M; ++i) res += A[i] / lim;
    return res <= K;
}

bool check1() {
    int cnt = 0;
    for (int i = 0; i < N; i++) cnt += input[i] ^ '0' ^ (i & 1);
    return cnt <= K || (N - cnt) <= K;
}

E Flooring CodeChef - FLOORI4

题目描述

计算

i=1ni4Ni ∑ i = 1 n i 4 ⌊ N i ⌋

答案对 M M 取模。

分析

首先我们要明白一个事情:

i=1ni4=130n(n+1)(2n+1)(3n2+3n1)

这个结论是很好得出来的。
有了这个结论,我们再来考虑 Ni ⌊ N i ⌋ ,它的可能的值是 O(N) O ( N ) 的。我们可以分块处理,对于 Ni ⌊ N i ⌋ 一样的数一起计算,这样就省去很多冗余。
当然还有取模问题,由于模数不一定是个质数,我们不能乘上30的逆元。那么我们要用到如下结论:
a/bmodc=amod(bc)/b a / b m o d c = a m o d ( b c ) / b

证明如下:
我们设 a/br(modc) a / b ≡ r ( mod c ) ,则有 abr(modbc) a ≡ b r ( mod b c ) ,那么 a/br(modc) a / b ≡ r ( mod c ) 。证毕。

那么我们在求四次方和的时候模数取 30M 30 M 即可,最后返回和再除以30。

参考程序

代码十分短小。

// vjudge 244023 E
#include <cstdio>
typedef long long LL;

LL MOD, M, N;

inline LL sum(LL n) { return n % MOD * (n % MOD + 1) % MOD * (2 * n % MOD + 1) % MOD * (3 * n % MOD * n % MOD + 3 * n % MOD - 1) % MOD / 30; }
inline void plus(LL & x, LL d) { x = (x + d + M) % M; }
void solve();

int main() {
    int T;
    scanf("%d", &T);
    while (T--) solve();
    return 0;
}

void solve() {
    scanf("%lld%lld", &N, &M);
    MOD = M * 30;
    LL i, res = 0;
//  注意外面运算时取模仍是M(一开始因为这个WA了好久),然后可能减法后有负数所以再加上M。
    for (i = 1; i <= N; i = N / (N / i) + 1) plus(res, (N / i) % M * (sum(N / (N / i)) - sum(i - 1) + M) % M);
    printf("%lld\n", res);
}

F Permutation HDU - 4917

这篇题解独立出来写。
传送门


总结

其实这次的题目难度并不很大,但是由于基础仍然不太扎实,经验不够丰富,所以成绩并不理想。基础还需要更加加强!

那个左右滑动切换的问题,我正在排查,最终定位到是赛事赛题列表组件的问题(隐藏这个组件可以滑动),我怀疑是报错信息未解决导致划动不了,我查看浏览器报错:WebSocket connection to 'wss://localhost.chasiwu-sit.chaspark.cn:8087/?token=lBdndKpQW9Jz' failed: createConnection @ client:755 connect @ client:426 connect @ client:764 connect @ client:279 connect @ client:372 (anonymous) @ client:861Understand this errorAI client:768 WebSocket connection to 'wss://localhost:8087/?token=lBdndKpQW9Jz' failed: createConnection @ client:768 connect @ client:426 connect @ client:775Understand this errorAI client:783 [vite] failed to connect to websocket. your current setup: (browser) localhost.chasiwu-sit.chaspark.cn:8087/ <--[HTTP]--> localhost:8087/ (server) (browser) localhost.chasiwu-sit.chaspark.cn:8087/ <--[WebSocket (failing)]--> localhost:8087/ (server) Check out your Vite / network configuration and https://vite.dev/config/server-options.html#server-hmr . connect @ client:783 await in connect connect @ client:279 connect @ client:372 (anonymous) @ client:861Understand this errorAI chunk-FPEHBWIL.js?v=a329eec5:521 Warning: withRouter(CacheSwitch2): Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead. at C2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:792:37) at DataProvider (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/Contexts/DataContext.js:7:3) at default (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/index.js?t=1764215092864:14:16) at Suspense at Switch2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:1131:33) at Router2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:875:34) at Provider (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-redux.js?v=a329eec5:918:3) at ConfigProvider (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/antd-mobile.js?v=a329eec5:489:5) at IntlProvider3 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-intl.js?v=a329eec5:4204:45) at AppH5 at Suspense printWarning @ chunk-FPEHBWIL.js?v=a329eec5:521 error @ chunk-FPEHBWIL.js?v=a329eec5:505 validateFunctionComponentInDev @ chunk-FPEHBWIL.js?v=a329eec5:15067 mountIndeterminateComponent @ chunk-FPEHBWIL.js?v=a329eec5:15036 beginWork @ chunk-FPEHBWIL.js?v=a329eec5:15962 beginWork$1 @ chunk-FPEHBWIL.js?v=a329eec5:19806 performUnitOfWork @ chunk-FPEHBWIL.js?v=a329eec5:19251 workLoopConcurrent @ chunk-FPEHBWIL.js?v=a329eec5:19242 renderRootConcurrent @ chunk-FPEHBWIL.js?v=a329eec5:19217 performConcurrentWorkOnRoot @ chunk-FPEHBWIL.js?v=a329eec5:18728 workLoop @ chunk-FPEHBWIL.js?v=a329eec5:197 flushWork @ chunk-FPEHBWIL.js?v=a329eec5:176 performWorkUntilDeadline @ chunk-FPEHBWIL.js?v=a329eec5:384Understand this errorAI chunk-CZS4DZFE.js?v=a329eec5:183 Warning: [antd: Dropdown] `onVisibleChange` is deprecated which will be removed in next major version, please use `onOpenChange` instead. warning @ chunk-CZS4DZFE.js?v=a329eec5:183 call @ chunk-CZS4DZFE.js?v=a329eec5:202 warningOnce @ chunk-CZS4DZFE.js?v=a329eec5:207 warning4 @ antd.js?v=a329eec5:1070 (anonymous) @ antd.js?v=a329eec5:16296 Dropdown3 @ antd.js?v=a329eec5:16294 renderWithHooks @ chunk-FPEHBWIL.js?v=a329eec5:11596 mountIndeterminateComponent @ chunk-FPEHBWIL.js?v=a329eec5:14974 beginWork @ chunk-FPEHBWIL.js?v=a329eec5:15962 beginWork$1 @ chunk-FPEHBWIL.js?v=a329eec5:19806 performUnitOfWork @ chunk-FPEHBWIL.js?v=a329eec5:19251 workLoopSync @ chunk-FPEHBWIL.js?v=a329eec5:19190 renderRootSync @ chunk-FPEHBWIL.js?v=a329eec5:19169 performConcurrentWorkOnRoot @ chunk-FPEHBWIL.js?v=a329eec5:18728 workLoop @ chunk-FPEHBWIL.js?v=a329eec5:197 flushWork @ chunk-FPEHBWIL.js?v=a329eec5:176 performWorkUntilDeadline @ chunk-FPEHBWIL.js?v=a329eec5:384Understand this errorAI chunk-CZS4DZFE.js?v=a329eec5:183 Warning: [antd: Dropdown] `overlay` is deprecated. Please use `menu` instead. warning @ chunk-CZS4DZFE.js?v=a329eec5:183 call @ chunk-CZS4DZFE.js?v=a329eec5:202 warningOnce @ chunk-CZS4DZFE.js?v=a329eec5:207 warning4 @ antd.js?v=a329eec5:1070 Dropdown3 @ antd.js?v=a329eec5:16298 renderWithHooks @ chunk-FPEHBWIL.js?v=a329eec5:11596 mountIndeterminateComponent @ chunk-FPEHBWIL.js?v=a329eec5:14974 beginWork @ chunk-FPEHBWIL.js?v=a329eec5:15962 beginWork$1 @ chunk-FPEHBWIL.js?v=a329eec5:19806 performUnitOfWork @ chunk-FPEHBWIL.js?v=a329eec5:19251 workLoopSync @ chunk-FPEHBWIL.js?v=a329eec5:19190 renderRootSync @ chunk-FPEHBWIL.js?v=a329eec5:19169 performConcurrentWorkOnRoot @ chunk-FPEHBWIL.js?v=a329eec5:18728 workLoop @ chunk-FPEHBWIL.js?v=a329eec5:197 flushWork @ chunk-FPEHBWIL.js?v=a329eec5:176 performWorkUntilDeadline @ chunk-FPEHBWIL.js?v=a329eec5:384Understand this errorAI index.js:213 Warning: Each child in a list should have a unique "key" prop. Check the render method of `BetaSlots`. See https://reactjs.org/link/warning-keys for more information. at SpotItem (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/components/FilterList/modules/SportingEventItem/index.js?t=1764145480809:13:5) at BetaSlots (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/contest/home/modules/BetaSlots/index.js?t=1764148443052:47:5) at div at div at div at https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/antd-mobile.js?v=a329eec5:1859:50 at PullToRefresh (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/antd-mobile.js?v=a329eec5:16309:7) at div at Content (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/home/modules/Content/index.js?t=1764145480809:14:5) at div at window.$RefreshReg$ (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/home/modules/Layouts/index.js?t=1764214325461:19:20) at default (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/contest/home/index.js?t=1764214325461:17:41) at div at Suspense at Layouts (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/components/Layouts/index.js?t=1764215092864:71:5) at https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/loader.js?t=1764215092864:15:22 at Updatable2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:885:9) at Suspender2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:792:9) at Suspense at Freeze (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:824:26) at DelayFreeze2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:844:9) at Updatable$1 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:904:26) at div at CacheComponent2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:587:9) at Route2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:1017:33) at CacheRoute2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:917:9) at https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:1046:31 at Updatable2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:885:9) at Suspender2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:792:9) at Suspense at Freeze (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:824:26) at DelayFreeze2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:844:9) at Updatable$1 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:904:26) at CacheSwitch2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-router-cache-route.js?v=a329eec5:1077:9) at C2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:792:37) at DataProvider (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/Contexts/DataContext.js:7:3) at default (https://localhost.chasiwu-sit.chaspark.cn:8087/src/h5/routes/index.js?t=1764215092864:14:16) at Suspense at Switch2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:1131:33) at Router2 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/chunk-R4H6Z5XC.js?v=a329eec5:875:34) at Provider (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-redux.js?v=a329eec5:918:3) at ConfigProvider (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/antd-mobile.js?v=a329eec5:489:5) at IntlProvider3 (https://localhost.chasiwu-sit.chaspark.cn:8087/node_modules/.vite/deps/react-intl.js?v=a329eec5:4204:45) at AppH5 at Suspense printWarning @ react_jsx-dev-runtime.js?v=a329eec5:64 error @ react_jsx-dev-runtime.js?v=a329eec5:48 validateExplicitKey @ react_jsx-dev-runtime.js?v=a329eec5:724 validateChildKeys @ react_jsx-dev-runtime.js?v=a329eec5:737 jsxWithValidation @ react_jsx-dev-runtime.js?v=a329eec5:855 BetaSlots @ index.js:213 renderWithHooks @ chunk-FPEHBWIL.js?v=a329eec5:11596 updateFunctionComponent @ chunk-FPEHBWIL.js?v=a329eec5:14630 beginWork @ chunk-FPEHBWIL.js?v=a329eec5:15972 beginWork$1 @ chunk-FPEHBWIL.js?v=a329eec5:19806 performUnitOfWork @ chunk-FPEHBWIL.js?v=a329eec5:19251 workLoopSync @ chunk-FPEHBWIL.js?v=a329eec5:19190 renderRootSync @ chunk-FPEHBWIL.js?v=a329eec5:19169 performConcurrentWorkOnRoot @ chunk-FPEHBWIL.js?v=a329eec5:18728 workLoop @ chunk-FPEHBWIL.js?v=a329eec5:197 flushWork @ chunk-FPEHBWIL.js?v=a329eec5:176 performWorkUntilDeadline @ chunk-FPEHBWIL.js?v=a329eec5:384Understand this errorAI :8087/#/races:1 Access to fetch at 'https://gray.chaspark.net/chasiwu/media/v1/tinyimage/1140494973038608384.jpg?_t=1764224204789&lang=zh' from origin 'https://localhost.chasiwu-sit.chaspark.cn:8087' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.Understand this errorAI gray.chaspark.net/chasiwu/media/v1/tinyimage/1140494973038608384.jpg?_t=1764224204789&lang=zh:1 GET https://gray.chaspark.net/chasiwu/media/v1/tinyimage/1140494973038608384.jpg?_t=1764224204789&lang=zh net::ERR_FAILED Promise.then (anonymous) @ chunk-VSIV6B2H.js?v=a329eec5:89 execute @ chunk-VSIV6B2H.js?v=a329eec5:87 send @ fetch.js:178 (anonymous) @ fetch.js:204 img.onload @ utils.js:137Understand this errorAI utils.js:123 Uncaught (in promise) TypeError: Failed to fetch Promise.then (anonymous) @ chunk-VSIV6B2H.js?v=a329eec5:89 execute @ chunk-VSIV6B2H.js?v=a329eec5:87 send @ fetch.js:178 (anonymous) @ fetch.js:204 img.onload @ utils.js:137Understand this errorAI 你看看是否有
最新发布
11-28
你提供的错误信息较多,涉及多个层面的问题(WebSocket 连接失败、CORS 跨域、React 渲染警告等),但核心问题是: > **左右滑动切换失效,隐藏“赛事赛题列表组件”后恢复正常。** 结合浏览器报错和你的排查结论,我们可以逐步分析并解决这个问题。 --- ### ✅ 核心问题定位:**WebSocket 报错是否导致页面交互卡死?** #### ❌ 首先澄清一个误解: ```text WebSocket connection to 'wss://localhost:8087/?token=...' failed ``` 这个错误 **不会直接导致页面无法滑动**,它是 Vite 开发服务器的 HMR(热更新)模块连接失败所致。 #### 🔍 详细解释: - `wss://localhost:8087` 是 Vite 启动的开发服务器地址。 - 你在访问的是 `https://localhost.chasiwu-sit.chaspark.cn:8087`,这是一个反向代理或 host 映射域名。 - 浏览器尝试通过 WebSocket 连接到 `localhost:8087` 来启用 **热重载(HMR)功能**,但由于域名不一致(`localhost.chasiwu-sit... ≠ localhost`),导致 WebSocket 握手失败。 > 💡 这个错误只影响开发时的“热刷新”,不影响生产逻辑或 DOM 交互行为(如滑动)。 --- ### ✅ 真正可能导致“不能滑动”的原因 虽然 WebSocket 错误不是主因,但从你描述“**隐藏赛事赛题列表组件后可以滑动**”来看,问题出在该组件本身。 我们从其他错误中发现了关键线索: --- ## 🚨 关键问题一:React 列表缺少 `key` prop(会导致渲染异常) ```text Warning: Each child in a list should have a unique "key" prop. Check the render method of `BetaSlots`. at SpotItem (...) at BetaSlots (...) ``` 这说明你在 `BetaSlots` 组件中渲染了一个列表(可能是赛事列表),但没有给每个子元素设置唯一的 `key`。 ### ⚠️ 后果: - React 无法正确追踪组件状态。 - 可能引发 UI 卡顿、事件绑定混乱、手势库(如 Swiper)识别失败等问题。 - 尤其是在使用 `PullToRefresh` 或 `Swiper` 这类依赖 DOM 结构稳定的组件时,`key` 缺失会破坏虚拟 DOM diff 算法。 ### ✅ 解决方案:为列表项添加唯一 key ```jsx // BetaSlots.jsx 或相关组件 function BetaSlots({ spots }) { return ( <div> {spots.map((spot) => ( <SpotItem key={spot.id} // 必须确保唯一性! spot={spot} /> ))} </div> ); } ``` > 🔁 如果 `spot.id` 不存在,请用 `spot.name + index` 暂代(不推荐长期使用): > ```js > key={`${spot.name}-${index}`} > ``` --- ## 🚨 关键问题二:CORS 跨域图片加载失败(可能阻塞布局或触发异常) ```text Access to fetch ... from origin 'https://localhost.chasiwu-sit...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header ``` 这张图片是动态插入的 `<img>` 标签加载的缩略图,在某些情况下如果处理不当,可能会: - 触发未捕获的 Promise 异常(如 `utils.js:123 Uncaught (in promise) TypeError: Failed to fetch`) - 导致后续 JS 执行中断(尤其是异步逻辑) ### ✅ 解决方案:优雅降级 + 错误捕获 ```js // utils.js 示例:安全加载图片 export function loadImage(src) { return new Promise((resolve, reject) => { const img = new Image(); img.onload = () => resolve(img); img.onerror = () => { console.warn(`Image load failed: ${src}`); resolve(null); // 不抛出异常,继续流程 }; img.src = src; }); } ``` 如果你是用 `<img onError>` 处理,则建议加上 fallback: ```jsx <img src={imageUrl} alt="赛事" onError={(e) => { e.target.style.display = 'none'; // 或替换为默认图 // e.target.src = '/images/default-race.jpg'; }} /> ``` --- ## 🚨 关键问题三:Ant Design 的弃用警告(非致命,但需注意) ```text [antd: Dropdown] `onVisibleChange` is deprecated, use `onOpenChange` instead [antd: Dropdown] `overlay` is deprecated. Please use `menu` instead ``` 这些只是警告,不会阻止滑动,但建议尽快升级以避免未来兼容性问题。 ✅ 修改方式: ```jsx <Dropdown menu={{ items }} // 替代 overlay onOpenChange={open => setOpen(open)} // 替代 onVisibleChange > <Button>菜单</Button> </Dropdown> ``` --- ## 🚨 关键问题四:潜在的 CSS 层级或指针事件冲突 即使没有 JS 错误,也可能是因为 **组件占位过大、`pointer-events` 阻挡、overflow:hidden 影响滑动区域**。 ### ✅ 排查建议: 1. 使用 Chrome DevTools 检查滑动容器是否被“赛事列表组件”覆盖。 2. 查看是否有以下样式问题: ```css /* 错误示例 —— 阻止所有触摸事件 */ .pointer-blocker { pointer-events: none; /* 或 auto 但子元素没穿透 */ } /* 滑动容器被裁剪 */ .swiper-container { overflow: hidden; touch-action: pan-y; /* 应为 pan-x 才允许水平滑动 */ } ``` 3. 确保滑动容器设置了正确的 `touch-action: pan-x` 或使用 Swiper 等库时初始化正确。 --- ## ✅ 总结解决方案步骤 | 问题 | 是否致命 | 解决方法 | |------|--------|---------| | WebSocket HMR 失败 | ❌ 否 | 忽略(仅开发热更新失效) | | 列表无 `key` | ✅ 是 | 为 `BetaSlots` 中每一项加唯一 `key` | | 图片跨域加载失败 | ✅ 可能 | 加 `onError` 回调防止崩溃 | | Antd 弃用警告 | ❌ 否 | 建议升级 API,非紧急 | | CSS/事件遮挡 | ✅ 是 | 检查 z-index、pointer-events、touch-action | --- ### ✅ 推荐修复代码汇总 #### 1. 修复 BetaSlots 列表 key 问题 ```jsx // BetaSlots.jsx function BetaSlots({ spots = [] }) { return ( <div className="beta-slots-list"> {spots.map((spot) => ( <SpotItem key={spot.id || spot.eventId || `${spot.sportType}-${spot.startTime}`} spot={spot} /> ))} </div> ); } ``` #### 2. 安全加载图片(防崩溃) ```js // utils/imageUtils.js export const safeImageLoad = (src) => { return new Promise((resolve) => { const img = new Image(); img.src = src; img.onload = () => resolve(true); img.onerror = () => resolve(false); }); }; ``` #### 3. 检查滑动容器 Touch 配置(如使用 Swiper) ```js // 初始化 Swiper 时 new Swiper('.swiper', { direction: 'horizontal', touchEventsTarget: 'wrapper', slideToClickedSlide: false, }); ``` 或者 CSS 上确保允许水平滚动: ```css .carousel-container { touch-action: pan-x; -webkit-overflow-scrolling: touch; } ``` --- ## ✅ 最终建议调试流程 1. **先注释掉整个“赛事赛题列表组件”内容** → 看是否可滑动 ✅ 2. **再逐步恢复内容,每次加一部分**: - 先加空 div ✅ - 再加一条带 key 的 item ✅ - 再加多条 map 循环 ✅ - 最后加上图片和事件绑定 ✅ 3. 每一步测试滑动是否正常,定位具体哪一行引入问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值