【整活】CF242E 题解
题目传送门:https://www.luogu.com.cn/problem/CF242E
题目翻译
不知道看题目翻译吗
好吧,还是贴一下。
You've got an array
- Calculate the sum of current array elements on the segment
[l,r] , that is, count valuea_{l}+a_{l+1}+...+a_{r} . - Apply the xor operation with a given number
x to each array element on the segment[l,r] , that is, execute . This operation changes exactlyr-l+1 array elements.
Expression means applying bitwise xor operation to numbers
You've got a list of
怎么,难道没有中文吗?英文都学不好那就 AFO 吧
中文题面:
- 给定 $n$ 个数的序列 $a$。$m$ 次操作,操作有两种:
1. 求 $\displaystyle\sum_{i=l}^r a_i$。
2. 把 $a_l\sim a_r$ 异或上 $x$。
- $1\le n\le 10^5$,$1\le m\le 5\times 10^4$,$0\le a_i\le 10^6$,$1\le x\le 10^6$。
- 给定
n 个数的序列a 。m 次操作,操作有两种:- 求
\displaystyle\sum_{i=l}^r a_i 。 - 把
a_l\sim a_r 异或上x 。
- 求
-
Q1:怎么做呢??
Q2:浴眼盯真鉴定为线段树
A:傻逼线段树,狗才用
Q1 & Q2:那用啥?
A:循环展开!!!!!!!!
Q1 & Q2:时间复杂度正确吗?
A:应该也许可能好像似乎是正确的吧,每次循环计算 好坏时间复杂度
Qinf:IAKIOI
那个 AKIOI 的,gui
Qinf:c……y……f……吃……枣……药……丸……
咳咳,回归正题
首先要知道之所以有循环,是为了将一些重复的东西折合在一起、节省码量,但会在两次循环的更迭时多出不必要的时间,循环展开则是把每两次循环的跨度拉大,在循环里手动多执行几个。
代(鸡)码(汤)来喽
温暖时刻
#include<bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b;
return 0;
}
呸,搞错了,正确的在这。
#include<bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello,World!";
return 0;
}
好吧,不逗你了。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, a[N];
int opt, l, r, x;
long long ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> m;
while(m--) {
cin >> opt >> l >> r;
if(opt == 2) {
cin >> x;
while(l <= r) {
a[l] ^= x, a[l + 1] ^= x, a[l + 2] ^= x, a[l + 3] ^= x, a[l + 4] ^= x, a[l + 5] ^= x, a[l + 6] ^= x, a[l + 7] ^= x;
l += 8;
}
while(--l > r) {
a[l] ^= x;
}
} else {
ans = 0;
while(l <= r) {
ans += a[l] + a[l + 1] + a[l + 2] + a[l + 3] + a[l + 4] + a[l + 5] + a[l + 6] + a[l + 7];
l += 8;
}
while(--l > r) {
ans -= a[l];
}
cout << ans << '\n';
}
}
return 0;
}