就把计算公式一顿化简,,代码混合了无修的F题的代码,所以看起来比较长QWQ
(1)直接把F题的代码粘过来计算未修改时的答案
(2)对于每次操作,按照化简后的公式计算他对答案的贡献
#define int ll
const int mod = 1e9 + 7;
int n, m;
char s[MAXN];
vector<int> v;
int c[2][MAXN];//一维为位置和,二维为个数和
void update(int x, int v,int k){ while (x <= n) { c[k][x] += v; x += (x & (-x)); }}
int que(int x,int k){int sum = 0;while (x){sum =(sum+ c[k][x])%mod; x -= (x & (-x));}return sum;}
signed main()
{
cin >> n ; scanf("%s",s+1); v.push_back(0);
int pre = -1;
rpp(i, n)
{
if (s[i] == '1')
{
update(i,i,0); update(i,1,1);
if (pre != -1) v.push_back(i - pre);
pre = i;
}
}
int ans = 0, k = sz(v);
rpp(i, k - 1) ans = (ans + ((k - i) * i * v[i]) % mod) % mod;
cout << ans << endl;
//以上为F题
int m;cin >> m;
rep(i, m)
{
int k, x;
cin >> k >> x;
if (k == 1) //赋值为1
{
ll a=que(x,0),b=que(x,1), c=que(n,0),d=que(n,1);
ans=(ans+(c-a*2)%mod+(2*b-d)*x%mod)%mod;
ans=(ans%mod+mod)%mod;
update(x,x,0); update(x,1,1);
cout<<ans<<endl;
}
else
{
update(x,-x,0);update(x,-1,1);
ll a=que(x,0),b=que(x,1), c=que(n,0),d=que(n,1);
ans=(ans-(c-a*2)%mod-(2*b-d)*x%mod)%mod;
ans=(ans%mod+mod)%mod;
cout<<ans<<endl;
}
}
return 0;
}