## Codeforces Round #546 (Div. 2) 题解

Codeforces Round #546 (Div. 2)

A. Nastya Is Reading a Book

```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
int n;
int l[N],r[N];
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>l[i]>>r[i];
int k;
cin>>k;
int p = lower_bound(l+1,l+n+1,k)-l;
if(l[p]==k) cout<<n-p+1;
else cout<<n-p+2;
return 0;
}```
View Code

B. Nastya Is Playing Computer Games

1.左右移动一格；2.将当前硬币上面的石头扔向另外一个格子（可以自己选择）；3.捡起当前的硬币。

```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
int n,k;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>k;
cout<<n + n+1 + n-1+min(n-k,k-1);
//捡硬币，扔石头，和移动
return 0;
}```
View Code

C. Nastya Is Transposing Matrices

```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 505;
int n,m;
int a[N][N];
int b[N][N];
vector <int> vec1,vec2;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>b[i][j];
int last=0;
for(int k=2;k<=n+m;k++){
vec1.clear();vec2.clear();
int i,j;
if(k<=1+m) i=1;
else i=k-m;
j = k-i;
//cout<<endl<<endl;
while(i<k && i<=n && j>=1){
//cout<<i<<" "<<j<<endl;
vec1.push_back(a[i][j]);
vec2.push_back(b[i][j]);
i++;j--;
}
sort(vec1.begin(),vec1.end());
sort(vec2.begin(),vec2.end());
int s=vec1.size();
for(i=0;i<s;i++){
if(vec1[i]!=vec2[i]){
cout<<"NO";
return 0;
}
}
}
cout<<"YES";
return 0;
}```
View Code

```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+5;
int n,m;
int a[N],cnt[N],p[N];
vector <int> g[N];
for(auto v:g[u]) cnt[v]++;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i],p[a[i]]=i;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
g[v].push_back(u);
}
int ans = 0;
for(int i=n-1;i>=1;i--){
if(cnt[a[i]]==n-i-ans) ans++;
}
cout<<ans;
return 0;
}```
View Code

E. Nastya Hasn't Written a Legend

```#include <bits/stdc++.h>
#define INF 999999999999999
using namespace std;
typedef long long ll;
const int N = 2e5+5;
int n;
ll a[N],b[N],t[N],k[N],pre[N];
struct Seg_Tre{
int l,r;
ll lazy,sum,mx;
}tre[N<<2];
void push_up(int rt){
tre[rt].sum=tre[rt<<1].sum+tre[rt<<1|1].sum;
tre[rt].mx=max(tre[rt<<1].mx,tre[rt<<1|1].mx);
}
void push_down(int rt){
if(tre[rt].lazy!=INF){
ll lzy=tre[rt].lazy;
tre[rt<<1].sum=lzy*(tre[rt<<1].r-tre[rt<<1].l+1);
tre[rt<<1|1].sum=lzy*(tre[rt<<1|1].r-tre[rt<<1|1].l+1);
tre[rt<<1].mx=lzy;
tre[rt<<1|1].mx=lzy;
tre[rt].lazy=INF;
tre[rt<<1].lazy=lzy;tre[rt<<1|1].lazy=lzy;
}
}
void build(int rt,int l,int r){
tre[rt].l=l;tre[rt].r=r;tre[rt].lazy=INF;
if(l==r){
tre[rt].sum=tre[rt].mx=b[l];
return ;
}
int mid = l+r>>1;
build(rt<<1,l,mid);build(rt<<1|1,mid+1,r);
push_up(rt);
}
ll query_sum(int rt,int l,int r){
int L=tre[rt].l,R=tre[rt].r;int mid=L+R>>1;
if(l<=L&&R<=r){
return tre[rt].sum;
}
push_down(rt);
ll ans = 0;
if(l<=mid) ans+=query_sum(rt<<1,l,r);
if(r>mid) ans+=query_sum(rt<<1|1,l,r);
push_up(rt);
return ans ;
}
ll query_pos(int rt,ll val){
int L=tre[rt].l,R=tre[rt].r;
int mid=L+R>>1;
if(L==R) return L;
ll p;
push_down(rt);
if(tre[rt<<1].mx>=val) p=query_pos(rt<<1,val);
else p=query_pos(rt<<1|1,val);
return p;
}
void update(int rt,int l,int r,ll val){
int L=tre[rt].l,R=tre[rt].r;
int mid=L+R>>1;
if(l<=L&&R<=r){
tre[rt].lazy=val;
tre[rt].sum=val*(R-L+1);
tre[rt].mx=val;
return ;
}
push_down(rt);
if(l<=mid) update(rt<<1,l,r,val);
if(r>mid) update(rt<<1|1,l,r,val);
push_up(rt);
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<n;i++){
cin>>k[i];
k[i]+=k[i-1];
pre[i]=pre[i-1]+k[i];
}
for(int i=1;i<=n;i++) b[i]=a[i]-k[i-1];
build(1,1,n);
int q,x,y;char c;
cin>>q;
while(q--){
//getchar();
cin>>c>>x>>y;
if(c=='s'){
ll ans = query_sum(1,x,y);
cout<<ans+pre[y-1]-(x>=2?pre[x-2]:0)<< '\n';
}else{
ll st = query_sum(1,n,n);
ll now = query_sum(1,x,x)+y;
ll r = query_pos(1,now);
if(r==n && st<now) r=n+1;
update(1,x,r-1,now);
//cout<<x<<" "<<r<<" "<<now+y<< '\n';
}
}
return 0;
}```
View Code

0条评论