版本比较
P14638 [NOIP2025] 序列询问 / query(官方数据)题解
正文
增加 91 行删除 2 行
10 行未改动11121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061076 行未改动1141151161172911811912012112287 行未改动210211212213125214215216217218104 行未改动全部展开10 行未改动精细实现可以做到 $\mathcal{O}(n^2+nq \log n)$。
由于 $f$ 数组的大小是 $n^2$ 的,该做法没有什么前途。
::::info[code $\mathcal{O}(n^2q)$ 40 pts]```cpp#include<bits/stdc++.h>using namespace std;using ll = long long;using ull = unsigned long long;using ust = unsigned short;const int N = 5e4 + 5, M = 1e4 + 5;const ll INF = 1e18;int n, q, a[N], vis2[N], v[N];ll sum[N], b[N];ull ans[N];bitset<N> vis;struct Node{ int l, r;}c[N];struct node{ ust a[10005]; int head = 1, tail;}que[M];struct node2{ ust a[N]; int head = 1, tail;}que2[1028];int main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for(int i = 1; i <= n; i++){ cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } cin >> q; for(int i = 1; i <= q; i++){ cin >> c[i].l >> c[i].r; for(int j = c[i].l; j <= c[i].r; j++)vis[j] = 1; } int cnt = 0; for(int i = 1; i <= n; i++){ if(vis[i]){ vis2[i] = ++cnt; v[cnt] = i; } } if(cnt > 1024){ for(int i = 1; i <= n; i++){ for(int j2 = 1; j2 <= cnt; j2++){ int j = v[j2]; while(que[j2].head <= que[j2].tail && que[j2].a[que[j2].head] <= i - j)que[j2].head++; if(i + j - 1 <= n){ ll x = sum[i + j - 1] - sum[i - 1]; while(que[j2].head <= que[j2].tail && sum[que[j2].a[que[j2].tail] + j - 1] - sum[que[j2].a[que[j2].tail] - 1] <= x)que[j2].tail--; if(i + j - 1 <= n)que[j2].a[++que[j2].tail] = i; } int x = que[j2].a[que[j2].head]; b[j2] = sum[x + j - 1] - sum[x - 1]; } for(int j = 1; j <= q; j++){ ll mx = -INF; for(int k = vis2[c[j].l]; k <= vis2[c[j].r]; k++)mx = max(mx, b[k]); ans[j] ^= (ull)mx * (ull)i; } } }else{ for(int i = 1; i <= n; i++){ for(int j2 = 1; j2 <= cnt; j2++){ int j = v[j2]; while(que2[j2].head <= que2[j2].tail && que2[j2].a[que2[j2].head] <= i - j)que2[j2].head++; if(i + j - 1 <= n){ ll x = sum[i + j - 1] - sum[i - 1]; while(que2[j2].head <= que2[j2].tail && sum[que2[j2].a[que2[j2].tail] + j - 1] - sum[que2[j2].a[que2[j2].tail] - 1] <= x)que2[j2].tail--; if(i + j - 1 <= n)que2[j2].a[++que2[j2].tail] = i; } int x = que2[j2].a[que2[j2].head]; b[j2] = sum[x + j - 1] - sum[x - 1]; } for(int j = 1; j <= q; j++){ ll mx = -INF; for(int k = vis2[c[j].l]; k <= vis2[c[j].r]; k++)mx = max(mx, b[k]); ans[j] ^= (ull)mx * (ull)i; } } } for(int i = 1; i <= q; i++)cout << ans[i] << '\n'; return 0;}```::::
## F2
由于询问放一起做没有前途,考虑将每一组询问独立去做。
6 行未改动每次将 $L \gets 2L$ 并重复一次就可以了。
时间复杂度 $\mathcal{O}(nq\log n)$。
::::info[code(60 pts)]::::info[code 60 pts]```cpp#include<bits/stdc++.h>using namespace std;using ll = long long;87 行未改动::::
加上两个优化后极限数据下大概是 3s。
::::info[code(仅使用第一个优化)]::::info[code 仅使用第一个优化]```cpp#include<bits/stdc++.h>using namespace std;const int N = 5e4 + 5, L = 16, M = 1e4 + 5;104 行未改动全部展开