CF--1316F Battalion Strength(线段树)
题意:https://codeforces.com/problemset/problem/1316/F
给你n个数,有2^n个子集,每个子集的计算公式是a1a2+a2a3+⋯+ak−1ak(必须递增),当然子集大小小于1的没有值。
再给你m个操作,把第i个换成x。
思路:
发现答案是一些ai*aj的和,所以就算贡献,ai*aj出现的次数就是2^(i-1)*2^(n-j)次,所以线段树里记录前缀的2^(i-1)*ai,和后缀2^(n-j)*aj,区间合并更新答案 是关键。
还有,你得离散化。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr strcat 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 #include <cassert> 21 #include <iomanip> 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 25 //****************** 26 clock_t __START,__END; 27 double __TOTALTIME; 28 void _MS(){__START=clock();} 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 30 //*********************** 31 #define rint register int 32 #define fo(a,b,c) for(rint a=b;a<=c;++a) 33 #define fr(a,b,c) for(rint a=b;a>=c;--a) 34 #define mem(a,b) memset(a,b,sizeof(a)) 35 #define pr printf 36 #define sc scanf 37 #define ls rt<<1 38 #define rs rt<<1|1 39 typedef pair<int,int> PII; 40 typedef vector<int> VI; 41 typedef unsigned long long ull; 42 typedef long long ll; 43 typedef double db; 44 const db E=2.718281828; 45 const db PI=acos(-1.0); 46 const ll INF=(1LL<<60); 47 const int inf=(1<<30); 48 const db ESP=1e-9; 49 const int mod=(int)1e9+7; 50 const int N=(int)6e5+10; 51 ll qpow(ll a,ll b,ll mod){ 52 ll ans; 53 // a%=mod; 54 ans=1; 55 while(b!=0) 56 { 57 if(b&1) 58 ans=(ans*a)%mod; 59 b/=2; 60 a=(a*a)%mod; 61 } 62 return ans; 63 } 64 65 ll er[N]; 66 int n,k; 67 int a[N],b[N]; 68 int mark[N]; 69 void LS() 70 { 71 int m=0; 72 for(int i=1;i<=n+k;++i) 73 b[++m]=a[i]; 74 sort(b+1,b+1+m); 75 // m=unique(b+1,b+1+m)-b-1; 76 for(int i=1;i<=n+k;++i) 77 { 78 int temp=lower_bound(b+1,b+1+m,a[i])-b; 79 a[i]=temp+mark[temp]; 80 mark[temp]++; 81 } 82 return ; 83 } 84 85 struct node 86 { 87 ll sum; 88 ll per,ber; 89 ll ans; 90 // ll l,r; 91 }tr[N<<2]; 92 93 void up(int rt) 94 { 95 tr[rt].sum=(tr[ls].sum+tr[rs].sum)%mod; 96 tr[rt].per=(tr[ls].per+tr[rs].per*er[tr[ls].sum]%mod)%mod; 97 tr[rt].ber=(tr[ls].ber*er[tr[rs].sum]+tr[rs].ber)%mod; 98 tr[rt].ans=(tr[ls].ans*er[tr[rs].sum]%mod+tr[rs].ans*er[tr[ls].sum]%mod)%mod+(tr[ls].per*tr[rs].ber)%mod; 99 tr[rt].ans%=mod; 100 } 101 void Build(int l,int r,int rt) 102 { 103 tr[rt]=node(); 104 if(l==r)return; 105 int mid=(l+r)>>1; 106 107 Build(l,mid,rt<<1); 108 Build(mid+1,r,rt<<1|1); 109 up(rt); 110 } 111 void update_dot(int pos,ll V,int l,int r,int rt) 112 { 113 if(l==r) 114 { 115 if(V==-1) 116 { 117 tr[rt]=node(); 118 return; 119 } 120 tr[rt].sum=1; 121 tr[rt].per=V; 122 tr[rt].ber=V; 123 // tr[rt].l=tr[rt].r=V; 124 tr[rt].ans=0; 125 return; 126 } 127 int mid=(l+r)>>1; 128 if(pos<=mid) 129 update_dot(pos,V,l,mid,rt<<1); 130 else 131 update_dot(pos,V,mid+1,r,rt<<1|1); 132 up(rt); 133 } 134 void check(int pos,int l,int r,int rt) 135 { 136 if(l==r) 137 { 138 pr("%d: ans: %lld sum: %lld\n",pos,tr[rt].ans,tr[rt].sum); 139 return ; 140 } 141 int mid=(l+r)>>1; 142 143 if(pos<=mid) 144 check(pos,l,mid,rt<<1); 145 else 146 check(pos,mid+1,r,rt<<1|1); 147 } 148 int pos[N]; 149 int pre[N]; 150 151 int main() 152 { 153 er[0]=1; 154 for(int i=1;i<N;++i)er[i]=er[i-1]*2%mod; 155 sc("%d",&n); 156 for(int i=1;i<=n;++i)sc("%d",&a[i]); 157 sc("%d",&k); 158 for(int i=1;i<=k;++i)sc("%d%d",&pos[i],&a[i+n]); 159 // _MS(); 160 LS(); 161 for(int i=1;i<=n;++i) 162 pre[i]=a[i]; 163 Build(1,n+k,1); 164 for(int i=1;i<=n;++i) 165 update_dot(a[i],b[a[i]],1,n+k,1); 166 // _ME(); 167 pr("%lld\n",tr[1].ans*qpow(er[n],mod-2,mod)%mod); 168 for(int i=1;i<=k;++i) 169 { 170 update_dot(pre[pos[i]],-1,1,n+k,1); 171 update_dot(a[i+n],b[a[i+n]],1,n+k,1); 172 pre[pos[i]]=a[i+n]; 173 pr("%lld\n",tr[1].ans*qpow(er[n],mod-2,mod)%mod); 174 } 175 return 0; 176 } 177 178 /**************************************************************************************/