跳到正文

题解

P8891 「UOI-R1」询问

Infinite_Eternity约 3 分钟
原文
文章信息与版本

文章信息

发布
2022年12月11日
收录
2026年2月7日
更新
2026年11月13日
篇幅
约 3 分钟
题目
P8891

版本

  1. v2当前
  2. v1

Description

「UOI-R1」询问

给定 nn 个数的整数序列 a1,a2,⋯ ,ana_1, a_2, \cdots, a_n,有 mm 次操作,每次操作给定 x,yx, y。对于所有满足 i⊕x=0i \oplus x = 0 的 ii,使得 ai←ai−ya_i \gets a_i - y。

数据范围:1≤n,m≤1061 \leq n, m \leq 10^6,−108≤y,ai≤108-10^8 \leq y, a_i \leq 10^{8},0≤x≤n0 \leq x \leq n。

Analysis

⊕\oplus 表示按位异或运算。其运算法则为:

将两个数用二进制进行表示后,对于每一位进行运算:若不同则为 11,相同则为 00。 即:

0⊕0=00⊕1=11⊕0=11⊕1=0\begin{aligned} 0 \oplus 0 = 0\\ 0 \oplus 1 = 1\\ 1 \oplus 0 = 1\\ 1 \oplus 1 = 0 \end{aligned}

该运算被也广泛运用来统计一个数中 11 的位数。


对于本题,我们便可从 i⊕x=0i \oplus x = 0 这个条件入手。根据 ⊕\oplus 的运算法则可得:当且仅当 i=xi = x 时,这个条件才能被满足。

因此,我们每次操作只需对 axa_x 进行操作即可。此外,因为本题的数据范围会爆 int,谨记要开 long long。

时间复杂度 O(m)\mathcal{O}(m)。

Code

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,x,y,a[1000001];
inline ll read()
{
    register char c;
    register int ans=0,z=1;
    while(!isdigit(c=getchar()))z-=2*(c=='-');
    do
    {
        ans=((ans<<3)+(ans<<1)+c-'0');
    }
    while(isdigit(c=getchar()));
    return ans*z;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;++i)
    	a[i]=read();
    while(m--)
    {
        x=read(),y=read();
        a[x]-=y;
    }
    for(int i=1;i<=n;++i)
    	printf("%lld ",a[i]);
	return 0;
}

彩蛋

原文
存档评论3 条抓取于