XORの問題の場合、それらの多くは接頭辞と一緒に置かれます。たとえば、この質問では、間隔と交差していないXOR値の数を見つけることができます
接頭辞の使用と間隔の表現を考えるのは簡単です。次に、交差しないようにする方法と計算を繰り返さない方法を検討してください
1. 2次元サイクル、最初の次元は1から始まり、2番目の次元は統計に使用され、1つは更新に使用されます
統計では、iからnまで、このセクションのすべての区間のXOR値がチェックされ、以前に等しいかどうかが確認され、次に計算されます。
1-iから更新する場合、以前のすべての間隔値を追加します
こうすれば重くなりません
#include <iostream> #include <map> #include < string > #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 5e5 + 10 ; int s [N]; int a [N]; int cnt [N]; int main(){ int n; cin >> n; int i; for(i = 1 ; i <= n; i ++ ){ cin >>a [i]; s [i] = s [i- 1 ] ^ a [i]; } ll res = 0 ; for(i = 1 ; i <= n; i ++ ){ int j; for(j = i; j <= n; j ++ ){ res + = cnt [s [j] ^ s [i- 1 ]]; } for(j = 1 ; j <= i; j ++ ){ cnt [s [i] ^ s [j- 1 ]] ++ ; } } cout << res << endl; }