很好的题,让我看完题解幡然醒悟。
既然乘积会很大,那我们想办法不让乘积那么大就可以了,这一点对于乱搞和正解是通用的。
乱搞
其实不能严格算作乱搞,和正解的思路还是有相通之处的。
注意到 ,所以如果答案小于 ,大于等于 的数字最多有 个,不然就会炸。考虑开一个桶记录 的数字各有几个,再把大于等于 的数字塞到一个 vector 里。修改的时候动态维护,具体地,判断修改前后的数字是小于等于 还是大于 ,并且动态修改 vector 和桶。你问我怎么把元素从 vector 中删掉?维护一个 表示原数组下标为 的数在 vector 中下标为几,删掉时将要删的数和最后一个交换,然后 pop_back 即可。
注意到 ,所以求答案的时候暴力扫描即可,最多不会超过 次。当然你也可以维护一个 的幂表来优化它,不过毕竟是乱搞,懒得写了。或者你还可以考虑把 质因数分解,可以进一步优化,不过也懒得写了。时间复杂度 。
code
#include<bits/stdc++.h>
#define ll long long
#define debug(x) cerr<<"debug "<<__LINE__<<":"<<#x<<" = "<<x<<"\n"
#define For(i,a,b) for(int i = a;i<=b;i++)
char buf[1 << 20], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
inline
int read() {
int x = 0; char c = getchar();
for (; c < '0' || c > '9'; c = getchar());
for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar());
return x;
}
inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x>9)write(x/10);putchar(x%10+'0');}
using namespace std;
int c[15],a[5000005],ans,pos[5000005];
vector<int> vc;
int main(){
int n = read(),m = read(),v = read();
For(i,1,n){
a[i] = read();
if(a[i]>=8) vc.push_back(i),pos[i] = (int)vc.size()-1;
else if(a[i]!=0) c[a[i]]++,pos[i] = 0x3f3f3f3f;
else pos[i] = 0x3f3f3f3f;
}
bool f = 0;
ll res = 1;
if((int)vc.size()<10){
For(i,2,8){
For(j,1,c[i]){
res*=i;
if(res>=v){
f = 1;
res = 0;
break;
}
}
if(f == 1) break;
}
for(auto it:vc){
res*=a[it];
if(res>=v){
f = 1;
res = 0;
break;
}
}
}
else f = 1,res = 0;
ans^=res;
while(m--){
int x = read(),y = read();
f = 0;
if(a[x]>=8){
if(y<8){
pos[vc[(int)vc.size()-1]] = pos[x];
swap(vc[pos[x]],vc[(int)vc.size()-1]);
pos[x] = 0x3f3f3f3f;
vc.pop_back();
c[y]++;
}
}
else{
if(y>=8){
vc.push_back(x);
pos[x] = vc.size()-1;
c[a[x]]--;
}
else{
c[a[x]]--;
c[y]++;
}
}
a[x] = y;
res = 1;
if((int)vc.size()<10){
For(i,2,8){
For(j,1,c[i]){
res*=i;
if(res>=v){
f = 1;
res = 0;
break;
}
}
if(f == 1) break;
}
for(auto it:vc){
res*=a[it];
if(res>=v){
f = 1;
res = 0;
break;
}
}
}
else f = 1,res = 0;
ans^=res;
}
write(ans);
return 0;
}
//初升的新月将日景仰羡,因为她的光芒遍照世间;沉落的暮日却向往月弦,因为她有万千美梦作伴。正解
一个一个将非 的数乘到答案里,如果乘出来比 大就把这个数的下标压进栈里并标记,因为值有可能被修改。
修改时,若这个元素未被标记,那么需要从答案中先除掉原贡献,再判断答案乘上新贡献是否大于等于 ,如果是就把下标压入栈,否则将答案乘上新贡献。然后再修改该元素。
若这个元素已被标记,那么不用管,直接修改即可。
查询答案时,从栈顶向下一个一个乘进答案里,一旦乘完了大于等于 ,就跳出循环。到最后如果栈空就说明答案小于 ,将其计入总答案。
时间复杂度 。
code
#include<bits/stdc++.h>
#define ll long long
#define debug(x) cerr<<"debug "<<__LINE__<<":"<<#x<<" = "<<x<<"\n"
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
char buf[1 << 20], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
inline
int read() {
int x = 0; char c = getchar();
for (; c < '0' || c > '9'; c = getchar());
for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar());
return x;
}
inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x>9)write(x/10);putchar(x%10+'0');}
using namespace std;
stack<int> st;
ll a[5000005],ans,res = 1;
bool vis[5000005];
int main(){
int n = read(),m = read(),v = read();
For(i,1,n){
a[i] = read();
if(a[i] != 1){
if(res*a[i]>=v) st.push(i),vis[i] = 1;
else res*=a[i];
}
}
if(res != 1) ans = res;
while(m--){
int x = read(),y = read();
if(!vis[x]){
res/=a[x];
if(res*y>=v){
st.push(x);
vis[x] = 1;
}
else res*=y;
}
a[x] = y;
while(!st.empty() && res*a[st.top()]<v){
res*=a[st.top()];
vis[st.top()] = 0;
st.pop();
}
if(st.empty()) ans^=res;
}
write(ans);
return 0;
}
//「你的画非常美丽,我很爱你,我很骄傲」。
//「要好好和姐姐妹妹相处,她们都很爱你」。一起打模拟赛的同学怎么还有取自然对数的做法、双模哈希的做法、线段树卡常的做法、Ad-hoc 的做法等等(?)吓哭了。