T1Merchant
get到新的stl,使用之后就A了
没啥可讲的,写个二分就行了
1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 inline int read() 5 { 6 int x=0,f=1; char ch=getchar(); 7 while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} 9 return x*f; 10 } 11 struct node{ 12 int k,b; 13 }a[1000050]; 14 int n,m,x; 15 int dp[1<<20]; 16 int flag=0; 17 int flag2=0; 18 int val[1000050]; 19 inline int getnum(int x) 20 { 21 int res=0; while(x) 22 { 23 if(x&1) ++res; 24 x>>=1; 25 }return res; 26 } 27 bool cmp(int a,int b) {return a>b;} 28 struct ef 29 { 30 inline bool check(int mid) 31 { 32 for(int i=1;i<=n;i++) 33 val[i]=mid*a[i].k+a[i].b; 34 nth_element(val+1,val+1+m,val+n+1,cmp); 35 int sum=0; 36 for(int i=1;i<=m;i++) 37 { 38 if(val[i]<0) 39 continue; 40 sum+=val[i]; 41 if(sum>=x) return 1; 42 } return 0; 43 } 44 inline void Mid1() 45 { 46 int l=0,ans=1e9,r=1e9; while(l<=r) 47 { 48 int mid=l+r>>1; 49 if(check(mid)){r=mid-1,ans=mid;} 50 else l=mid+1; 51 } 52 printf("%lld\n",ans); 53 } 54 }s; 55 signed main() 56 { 57 //freopen("a.in","r",stdin); 58 n=read();m=read();x=read();for(int i=1;i<=n;i++) 59 {a[i].k=read();a[i].b=read();if(a[i].k<0) flag=1;if(a[i].k>0) flag2=1;} 60 if(n<=20) 61 { 62 memset(dp,0x3f,sizeof(dp)); 63 for(int i=1;i<1<<n;i++) 64 { 65 if(getnum(i)>m){continue;} 66 int now=0,sum=0; 67 for(int j=1;j<=n;j++) 68 if(i&(1<<(j-1))){now+=a[j].k;sum+=a[j].b;} 69 int base=x-sum;if(base<=0) {dp[i]=0;} 70 else 71 { 72 if(now<=0) continue; 73 if(base%now) dp[i]=base/now+1; 74 else dp[i]=base/now; 75 } 76 }int ans=1e9; 77 for(int i=1;i<1<<20;i++) 78 ans=min(ans,dp[i]);printf("%lld\n",ans); 79 return 0; 80 } 81 if(flag2==0) {puts("0");return 0;} 82 s.Mid1(); 83 }
T2Equation
考场上想到思路的水题,不过时间不够了,没码
根据dep的奇偶性决定正负就好了
1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 inline int read() 5 { 6 int x=0,f=1; char ch=getchar(); 7 while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} 9 return x*f; 10 } 11 const int maxn=1e6+5; 12 struct node{ 13 int to,nxt; 14 }e[maxn*2]; 15 inline int pan(int dep){return dep&1?-1:1;} 16 int head[maxn],dfn[maxn],cnt,n,q,num,dep[maxn],size[maxn],fa[maxn],w[maxn],last[maxn]; 17 void add(int x,int y){e[++num]=(node){y,head[x]};head[x]=num;} 18 inline int lowbit(int x){return x&(-x);} 19 struct szsz{ 20 int c[maxn]; 21 inline void update(int x,int val) 22 { 23 while(x<=n) 24 { c[x]+=val; x+=lowbit(x); } 25 } 26 inline int query(int x) 27 { 28 int res=0; 29 while(x) { res+=c[x]; x-=lowbit(x); } 30 return res; 31 } 32 }s[2]; 33 void dfs(int x,int ss) 34 { 35 dfn[x]=++cnt; 36 s[dep[x]&1].update(dfn[x],ss); 37 s[dep[x]&1].update(dfn[x]+1,-ss); 38 for(int i=head[x];i;i=e[i].nxt) 39 { 40 int y=e[i].to; 41 dep[y]=dep[x]+1; 42 dfs(y,-ss+w[y]); 43 }last[x]=cnt; 44 } 45 signed main() 46 { 47 n=read();q=read(); 48 for(int i=2;i<=n;i++) 49 { 50 fa[i]=read(); w[i]=read(); 51 add(fa[i],i); 52 } 53 dfs(1,0); 54 for(int i=1;i<=q;i++) 55 { 56 int opt=read(); 57 if(opt==1) 58 { 59 int x=read(),y=read(),v=read(); 60 int xx=s[dep[x]&1].query(dfn[x]); 61 int yy=s[dep[y]&1].query(dfn[y]); 62 v-=xx+yy;int flag=pan(dep[x])+pan(dep[y]); 63 if(!flag&&!v){puts("inf");continue;} 64 if(!flag||(v%flag!=0)){puts("none");continue;} 65 else printf("%lld\n",v/flag); 66 } 67 else 68 { 69 int x=read(),y=read(); 70 int yy=y-w[x];w[x]=y; 71 s[dep[x]&1].update(dfn[x],yy); 72 s[dep[x]&1].update(last[x]+1,-yy); 73 s[(dep[x]&1)^1].update(dfn[x],-yy); 74 s[(dep[x]&1)^1].update(last[x]+1,yy); 75 } 76 } 77 }
T3Rectangle
神仙题,颓了一下午的题解才看懂
把所有的x相同的点放在一起,然后m方枚举两个x,
然后用两个单调指针+树状数组维护每个矩形对总答案作出的贡献;
1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 inline int read() 5 { 6 int x=0,f=1; char ch=getchar(); 7 while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} 9 return x*f; 10 } 11 const int maxn=2501; 12 const int mod=1e9+7; 13 struct node{int x,y;}a[10001]; 14 int n; 15 int vis[2501][2501],m[2501][2501]; 16 vector<int >q[2501]; 17 int c[2][2501][2501],ans=0; 18 inline int lowbit(int x){return x&(-x);} 19 inline void update(int k,int y,int x,int val) { while(x<=2500) { c[k][y][x]+=val;x+=lowbit(x);}} 20 inline int query(int k,int y,int x) {int res=0; while(x) { res+=c[k][y][x]; x-=lowbit(x);} return res;} 21 signed main() 22 { 23 n=read();for(int i=1;i<=n;i++) 24 { 25 a[i].x=read(); a[i].y=read(); 26 m[a[i].x][++m[a[i].x][0]]=a[i].y; 27 } 28 for(int i=1;i<=2500;i++) 29 { 30 sort(m[i]+1,m[i]+1+m[i][0]); 31 m[i][m[i][0]+1]=2501; 32 if(!m[i][0]) continue; 33 for(int j=1;j<=m[i][0];j++) if(!vis[i][m[i][j]]) 34 { 35 vis[i][m[i][j]]=1; 36 update(1,i,m[i][j],1); 37 update(0,i,m[i][j],m[i][j]); 38 } 39 for(int j=i-1;j>=1;j--) 40 { 41 if(!m[j][0]) continue; 42 int x1=1,x2=1,flag; 43 for(int k=1;k<=m[j][0];k++) if(!vis[i][m[j][k]]) 44 { 45 vis[i][m[j][k]]=1; 46 update(1,i,m[j][k],1); 47 update(0,i,m[j][k],m[j][k]); 48 }int maxx=max(m[i][1],m[j][1]); 49 while(m[i][x1+1]<=maxx) x1++; 50 while(m[j][x2+1]<=maxx) x2++; 51 while(x1<=m[i][0]&&x2<=m[j][0]) 52 { 53 flag=min(m[i][x1+1],m[j][x2+1]); 54 (ans+=(i-j)*((query(0,i,flag-1)-query(0,i,maxx-1))*query(1,i,min(m[i][x1],m[j][x2]))-((query(1,i,flag-1)-query(1,i,maxx-1))*query(0,i,min(m[i][x1],m[j][x2])))))%=mod; 55 maxx=flag; 56 if(m[i][x1+1]<=maxx) x1++; 57 if(m[j][x2+1]<=maxx) x2++; 58 } 59 } 60 } 61 cout<<ans<<endl; 62 }