【整活】CF242E 题解

· · 休闲·娱乐

题目传送门:https://www.luogu.com.cn/problem/CF242E

题目翻译

不知道看题目翻译吗

好吧,还是贴一下。

You've got an array a , consisting of n integers a_{1},a_{2},...,a_{n} . You are allowed to perform two operations on this array:

  1. Calculate the sum of current array elements on the segment [l,r] , that is, count value a_{l}+a_{l+1}+...+a_{r} .
  2. Apply the xor operation with a given number x to each array element on the segment [l,r] , that is, execute . This operation changes exactly r-l+1 array elements.

Expression means applying bitwise xor operation to numbers x and y . The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as "^", in Pascal — as "xor".

You've got a list of m operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.

怎么,难道没有中文吗?英文都学不好那就 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$。
\text{自己放}$[$\text{云剪切板}$](https://www.luogu.com.cn/paste)$\text{去}\color{yellow}\text{待会会给}

Q1:怎么做呢??

Q2:浴眼盯真鉴定为线段树

A:傻逼线段树,狗才用

Q1 & Q2:那用啥?

A:循环展开!!!!!!!!

Q1 & Q2:时间复杂度正确吗?

A:应该也许可能好像似乎是正确的吧,每次循环计算 8 个,总时间复杂度为 \mathcal{O}\left(\dfrac{\sum\left(r-l+1\right)\times{?}}{k}\right),最好坏时间复杂度 \large\mathcal{O}\left(\dfrac{nm\times{?}}{k}\right)!!!\colorbox{black}{\color{black}可能}不会超时!!!

\scriptsize{\text{可能是 CF 数据太水的缘故吧}}

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;
}