HistoryOI R2T1 Solution
Cpp__5417__ · · 算法·理论
History OI R2T1 题解
R2 还能祝 99,R4 主角就成我了……
Solution
- 一个区间
[l,r] 被称为 心动区间,当且仅当这个区间内所有元素的异或和等于0 。
第一眼:前缀和。
第二眼:前缀异或和。
第三眼:前缀异或和没跑了。
首先处理出前缀异或和数组
注意:如果
::::info[证明]{open}
求区间前缀和的公式是
至于
:::success[习题答案]
注意到当
但——真这么做,TLE 等着你。考虑优化。
注意到 @Code_to_Win 的数据极水,
当然,桶数组用 「map、unordered_map、gp_hash_table、cc_hash_table」(C++)、「dict」(Python)、「TreeMap、HashMap」(Java)都可以,只是有概率被卡飞。毕竟除了 AlgoOI 的成员,没有人知道 @Code_to_Win 究竟有没有放 Hack 数据(基(jue)本(dui)没有),但是显然的,Python 和 Java 慢的一批。
但是由于我闲,发现 真是个好孩子。
再加上亿点点优化,AC 代码就变得十分极简。自行观看。
复杂度如下:
| Time | < | < | Space(桶数组 / Hash) | Space(Rb Tree) |
| :-: | :-: | :-: | :-: | :-:|
|
其中
Code
:::success[AC of C++] 6ms 284B,去掉了压行。
#include<bits/stdc++.h>
#define int Exint_t
using namespace std;
typedef long long Exint_t;
const int V=1e5+38;
int n,b,t,r;
int mp[V]={1};// 注意初始化 X[0]=1
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
for(cin>>n;n--?true:printf("%lld\n",r)&0;)cin>>t,r+=mp[b=t^=b]++;// 自行理解
return 38&17;
}
::: :::success[TLE of Python] 被 @Code_to_Win 的时间限制卡飞了。
V=int(1e5+38)
mp=[1]+[0 for _ in range(V+38)]
r,b=0,0
for i in range(int(input())):
b^=int(input())
r+=mp[b]
mp[b]+=1
print(r)
::: :::success[TLE of Java] 依旧 T 飞。
import java.util.Scanner;
public class Main{
static Scanner scan=new Scanner(System.in);
static void Cpp(){
int n=scan.nextInt(),b=0,t,r=0;
final int V=(int)1e5+38;
long[] mp=new long[V];
mp[0]=1;
while(n--!=0){
t=scan.nextInt();
r+=mp[b=t^=b]++;
}
System.out.println(r);
}
public static void main(String[] args){
long q=1;
// q=scan.nextLong();
while(q--!=0){Cpp();}
}
}
:::