D.
题意:给2组数据a和b数组,每次有2种操作:(+,l,r,x)把a数组第l个到第r个元素全置为x,(?,l,r)查询[l,r]之间哪些位置满足a[i]>=b[i](i>=l && i<=r)并把这些位置的数量统计
一直想很久,没想到什么有效的方案,直到看到题解才明白过来,原来线段树套平衡树还有这种情况:里面其实不是平衡树,只是有序表。
然后这题就转换为区间查找数对应排名
由于此题不用对2个数组都修改,其中1个b树可作为固定的线段树套有序表以节省时间,另外1个表a树则单纯使用线段树的方法先修改,再更新对应b树结点的排名
这里查找排名如果全部logn查找会因为常数太大直接卡,注意每个结点都含有序表并且上下有包含关系
那咱们可以在b树自底向上更新父结点排名对应左右子树里的排名,用归并排序的方法,占用空间才o(nlogn),时间也是o(nlogn)
顺带把会改变的a树1个个结点查询b树查出排名,修改时先查出根结点对应位置,再根据位置子树表一边向下更新一边转移到子树对应位置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> using namespace std; typedef __int64 ll; int max(int a,int b){return a>b?a:b;} int min(int a,int b){return a<b?a:b;} int cnt; const int N=100010,M=262150,E=1768950; int n,m,i,a[N],b[N],x,l,r; int st[M],en[M],v[M],tag[M],pl[E],pr[E],pool[E],cur; ll ans,sum; void build(int x,int l,int r) { tag[x]=-1; if(l==r) { st[x]=cur+1; pool[++cur]=b[l]; en[x]=cur; v[x]=(a[l]>=b[l]); return; } int mid=((l+r)>>1); build(x<<1,l,mid); build((x<<1)|1,mid+1,r); v[x]=v[x<<1]+v[(x<<1)|1]; int al=st[x<<1],ar=en[x<<1],bl=st[(x<<1)|1],br=en[(x<<1)|1]; st[x]=cur+1; while(al<=ar&&bl<=br)pool[++cur]=pool[al]<pool[bl]?pool[al++]:pool[bl++]; while(al<=ar)pool[++cur]=pool[al++]; while(bl<=br)pool[++cur]=pool[bl++]; en[x]=cur; al=st[x<<1],bl=st[x<<1|1]; for(int i=st[x];i<=cur;i++) { while(al<=ar&&pool[al]<=pool[i])al++; while(bl<=br&&pool[bl]<=pool[i])bl++; pl[i]=al-1,pr[i]=bl-1; if(pl[i]<st[x<<1])pl[i]=0; if(pr[i]<st[(x<<1)|1])pr[i]=0; } } inline void rankpt(int x,int p) { v[x]=(p?p-st[x]+1:0); tag[x]=p; } inline void pushdown(int x) { if(tag[x]<0)return; int p=tag[x]; rankpt(x<<1,pl[p]); rankpt((x<<1)|1,pr[p]); tag[x]=-1; } void update(int x,int a,int b,int p) { if(l<=a && b<=r){rankpt(x,p);return;} pushdown(x); int mid=(a+b)>>1; if(l<=mid)update(x<<1,a,mid,pl[p]); if(r>mid)update((x<<1)|1,mid+1,b,pr[p]); v[x]=v[x<<1]+v[(x<<1)|1]; } void query(int x,int a,int b) { if(l<=a && b<=r) { ans+=v[x]; return; } pushdown(x); int mid=((a+b)>>1); if(l<=mid)query(x<<1,a,mid); if(r>mid)query((x<<1)|1,mid+1,b); v[x]=v[x<<1]+v[(x<<1)|1]; } inline int lower(int x){ //lower_bound(pool+st[1],pool+ed[1]+1,x); int l=st[1],r=en[1],mid,t=0; while(l<=r) if(pool[mid=(l+r)>>1]<=x)l=(t=mid)+1; else r=mid-1; return t; } int seeda, seedb, C = ~(1<<31), MM = (1<<16)-1; int rnd(int last) { seeda = (36969 + (last >> 3)) * (seeda & MM) + (seeda >> 16); seedb = (18000 + (last >> 3)) * (seedb & MM) + (seedb >> 16); return (C & ((seeda << 16) + seedb)) % 1000000000; } int main() { int t,ku; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&n,&m,&seeda,&seedb); for(i=1;i<=n;i++)scanf("%d",a+i); for(i=1;i<=n;i++)scanf("%d",b+i); ans=sum=cur=0; build(1,1,n); for(i=1;i<=m;i++) { l=rnd(ans)%n+1,r=rnd(ans)%n+1,ku=rnd(ans)+1; int kkk=lower(ku); if(l>r)l^=r^=l^=r; if((l+r+ku)&1)update(1,1,n,lower(ku)); else { ans=0; query(1,1,n); sum=(sum+(ll)i*ans)%1000000007; } } printf("%I64d ",sum); } return 0; }
F.
题意,有1个图(可能不是全部联通的)Gi表示去掉第i个点的图权值(1个连通图的权值=所有点的积 多个连通图的权值=连通图权值之和)
求1*G1+2*G2+3*G3+...+n*Gn
求点双联通分量,求出割点,然后求出此点以外点双联通连通图的权值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <ctype.h> #include <iostream> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> using namespace std; #define MAXN 100005 #define MOD 1000000007 int inv(int x) { int res=1, mod=MOD-2; do { if(mod & 1) res = (long long)res * x % MOD; x = (long long)x * x % MOD; }while(mod >>= 1); return res; } vector<int> G[MAXN]; int w[MAXN]; int z[MAXN]; int cz[MAXN]; int pre[MAXN]; bool iscut[MAXN]; int ncc_no[MAXN]; vector<int> ncc[MAXN]; int ncc_size[MAXN]; int ncc_cnt; int clock; int tarjan(int u, int fa) { ncc[ncc_no[u] = ncc_cnt].push_back(u); ncc_size[ncc_cnt] = ((long long)ncc_size[ncc_cnt] * w[u]) % MOD; int lowu = pre[u] = ++clock; int child, i, v, lowv, nowsize, tmp; for(child=0, i=G[u].size(); i--;) { if(!pre[v = G[u][i]]) { child++; nowsize = ncc_size[ncc_cnt]; lowv = tarjan(v, u); lowu = min(lowv, lowu); if(lowv >= pre[u]) { iscut[u] = true; tmp = (long long)ncc_size[ncc_cnt] * inv(nowsize) % MOD; z[u] = (z[u] + tmp) % MOD; cz[u] = (long long)cz[u] * tmp % MOD; } } else if(pre[v] < pre[u] && v != fa) lowu = min(lowu, pre[v]); } if(fa < 0 && child == 1) iscut[u] = false; return lowu; } int main() { int t, n, m, x, y, i, sum; scanf("%d", &t); while(t-- && scanf("%d%d", &n, &m) > 0) { for(i=1; i<=n; i++) { scanf("%d", w+i); G[i].clear(); cz[i] = 1; ncc[i].clear(); } while(m--) { scanf("%d%d", &x, &y); G[x].push_back(y); G[y].push_back(x); } memset(z, 0, sizeof z); memset(pre, 0, sizeof pre); memset(iscut, false, sizeof iscut); memset(ncc_no, 0, sizeof ncc_no); clock = ncc_cnt = 0; for(i=1, sum=0; i<=n; i++) if(!pre[i]) { ncc_size[++ncc_cnt] = 1; tarjan(i, -1); for(x=ncc[ncc_cnt].size(); x--;) if(iscut[y = ncc[ncc_cnt][x]] && y!=i) z[y] = (z[y] + (long long)ncc_size[ncc_cnt] * inv((long long)cz[y] * w[y] % MOD) % MOD) % MOD; sum = (sum + ncc_size[ncc_cnt]) % MOD; } for(i=1, y=0; i<=n; i++) { if(!iscut[i]) z[i] = ncc[ncc_no[i]].size() > 1 ? (long long)ncc_size[ncc_no[i]] * inv(w[i]) % MOD : 0; y = ((long long)y + ((long long)z[i] + (long long)(sum - ncc_size[ncc_no[i]] + MOD) % MOD) % MOD * i % MOD) % MOD; } printf("%d ", y); } return 0; }
G.