标签:Power 牛牛 ll pos int add getsum Link ans
思路
另线段树做法:https://www.cnblogs.com/fisherss/p/12287606.html
F题树状数组维护前缀即可
题目地址
https://ac.nowcoder.com/acm/contest/3004/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
int n;
char s[maxn];
const ll mod = 1e9+7;
struct bit{
ll c[maxn];
int lowbit(int x){
return x & -x;
}
void add(int pos,int v){
while(pos <= n){
c[pos] += v;
pos += lowbit(pos);
}
}
ll getsum(int pos){
ll res = 0;
while(pos){
res += c[pos];
pos -= lowbit(pos);
}
return res;
}
}pre,suf,cnt;
int main(){
cin>>n;
scanf("%s",s+1);
ll ans = 0;
for(int i=1;i<=n;i++){
if(s[i] == '1'){
cnt.add(i,1);
pre.add(i,i);
ans = (ans + i*cnt.getsum(i) - pre.getsum(i))%mod;
}
}
cout<<ans<<endl;
return 0;
}
G题 3棵树状数组维护前缀和、后缀和、个数
题目地址
https://ac.nowcoder.com/acm/contest/3004/G
70%,wa
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
ll n;
char s[maxn];
const ll mod = 1e9+7;
struct bit{
ll c[maxn];
ll lowbit(ll x){
return x & -x;
}
void add(ll pos,ll v){
while(pos <= n){
c[pos] += v;
pos += lowbit(pos);
}
}
ll getsum(ll pos){
ll res = 0;
while(pos){
res += c[pos];
pos -= lowbit(pos);
}
return res;
}
}pre,suf,cnt;
void print(){
for(int i=1;i<=n;i++){
cout<<suf.getsum(i)<<" ";
}
cout<<endl;
}
int main(){
cin>>n;
scanf("%s",s+1);
ll ans = 0;
for(ll i=1;i<=n;i++){
if(s[i] == '1'){
cnt.add(i,1);
pre.add(i,i);
suf.add(i,n-i+1);
ans = (ans + i*cnt.getsum(i) - pre.getsum(i))%mod;
}
}
cout<<ans%mod<<endl;
ll m;
cin>>m;
while(m--){
ll q,pos;
cin>>q>>pos;
if(q == 1){
//加点
cnt.add(pos,1);
pre.add(pos,pos);
suf.add(pos,n-pos+1);
//加上对前缀1的贡献影响
ans = (ans + (pos*cnt.getsum(pos) - pre.getsum(pos)))%mod;
//加上对后缀1的贡献影响
ans = (ans + ((n - pos + 1) * (cnt.getsum(n) - cnt.getsum(pos-1)) - (suf.getsum(n) - suf.getsum(pos-1))))%mod;
}else{
ans = (ans - (pos*cnt.getsum(pos) - pre.getsum(pos)))%mod;
ans = (ans - ((n - pos + 1) * (cnt.getsum(n) - cnt.getsum(pos-1)) - (suf.getsum(n) - suf.getsum(pos-1))))%mod;
cnt.add(pos,-1);
pre.add(pos,-pos);
suf.add(pos,-(n-pos+1));
}
cout<<ans%mod<<endl;
}
return 0;
}
标签:Power,牛牛,ll,pos,int,add,getsum,Link,ans 来源: https://www.cnblogs.com/fisherss/p/12657814.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。