P6225 [eJOI2019] 异或橙子
题意:
给定一个1-N的数组,进行q次操作,如果是1,把第x位修改为y,如果是2,输出从L到U的异或异或和。例如(2,4)=2^3^4^(2^3)(3^4)(2^3^4)
思路:
本题的突破口: 异或的特殊性质:
-
a^0=a
-
a^a=0
- 当 l,u 奇偶性不同时,[l,u] 中所有元素都会对答案贡献偶数次 ,那么答案就是 0。
-
当 l,u 奇偶性相同时,[l,u] 中所有 l,ul,u 奇偶性相同的位置的值会对答案贡献奇数次,其他位置的值则会贡献偶数次,那么答案就是 A(l)^A(l+2)^……^A(u)
可用树状数组来做
代码:
#include<bits/stdc++.h> using namespace std; #define N 200005 int n,q,a[N]; int lowbit(int x){ return x&(-x); } struct bit{ int dat[N]; inline void update(int x,int p){ for (;p <= n ; p+= lowbit(p)) { dat[p]^=x; } } inline int sum(int p){ int x=0; for (; p ; p-= lowbit(p)) { x^=dat[p]; } return x; } }; bit tree[2]; int main(){ cin>>n>>q; for (int i = 1; i <=n ; ++i) { cin>>a[i]; tree[i&1].update(a[i],i); } while (q--){ int gg,x,y; cin>>gg>>x>>y; if(gg==1){ tree[x&1].update(a[x]^y,x),a[x]=y; } else{ if((x+y)&1)cout<<'0'<<endl; else cout<<(tree[x&1].sum(y)^tree[x&1].sum(x-1))<<endl; } }
}
------------
# 2.[P8720 [蓝桥杯 2020 省 B2] 平面切分](https://www.luogu.com.cn/problem/P8720?contestId=95104)
## 题意:
平面上有 N条直线, 其中第 i 条直线是 y=ax+b,计算平面被分成了几部分
## 思路:
每新加一个边,都会加一部分,如果这个边与其他边有几个不同的交点,则就会加几部分。
## 代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 500
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
int n;
int A[1010],B[1010];
int main(){
cin>>n;
set<PII>st;
for(int i=0,a,b;i<n;i++){
cin>>a>>b;
st.insert(PII(a,b));
}//去重边
int cnt=0;
for(auto p:st)A[cnt]=p.first,B[cnt++]=p.second;
int ans=2;//第一条边有俩部分
for(int i=1;i<st.size();i++){//必须记录下来
set<PDD>dian;//去掉重复的交点
for(int j=i-1;j>=0;j--){
int a1=A[i],b1=B[i];
int a2=A[j],b2=B[j];
if(a1==a2)continue;//平行无交点
dian.insert(PDD((b2-b1)*1.0/(a1-a2),((a1*b2-a2*b1)*1.0/(a1-a2))));//这条边与前面几条边有多少不同交点。
}
ans+=dian.size()+1;
}
cout<<ans;
}