Codeforces Round #546 (Div. 2)

Codeforces Round #546 (Div. 2)

http://codeforces.com/contest/1136

A

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 13000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 vector<pii>ve;
24 
25 int main(){
26     #ifndef ONLINE_JUDGE
27      //   freopen("1.txt","r",stdin);
28     #endif
29     std::ios::sync_with_stdio(false);
30     int n;
31     cin>>n;
32     int u,v;
33     for(int i=0;i<n;i++){
34         cin>>u>>v;
35         ve.pb({u,v});
36     }
37     sort(ve.begin(),ve.end());
38     int m;
39     cin>>m;
40     for(int i=0;i<ve.size();i++){
41         if(ve[i].first<=m&&ve[i].second>=m){
42             cout<<n-i<<endl;
43         }
44     }
45 }
View Code

 

B

找规律

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 13000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 vector<pii>ve;
24 
25 int main(){
26     #ifndef ONLINE_JUDGE
27      //   freopen("1.txt","r",stdin);
28     #endif
29     std::ios::sync_with_stdio(false);
30     int n,k;
31     cin>>n>>k;
32     cout<<n*3+min(k-1,n-k)<<endl;
33 }
View Code

 

C

通过模拟可以发现,不管子矩阵怎么变化,矩阵的对角线上的值的种类和个数不会变,只会改变他们的顺序

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 13000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 int n,m;
24 int ma1[505][505];
25 int ma2[505][505];
26 
27 int main(){
28     #ifndef ONLINE_JUDGE
29      //   freopen("1.txt","r",stdin);
30     #endif
31     std::ios::sync_with_stdio(false);
32     cin>>n>>m;
33     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>ma1[i][j];
34     for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>ma2[i][j];
35     int x1=1,x2=1,y1=1,y2=1;
36     int aa,bb,cc,dd;
37     vector<int>v1[1005];
38     vector<int>v2[1005];
39     int co=0;
40     while((x1!=n+1||x2!=n||y1!=m||y2!=m+1)){
41         aa=x1,bb=y1,cc=x2,dd=y2;
42         while(aa<=cc&&bb>=dd){
43             v1[co].pb(ma1[aa][bb]);
44             v2[co].pb(ma2[aa][bb]);
45             aa++,bb--;
46         }
47         co++;
48         if(y1==m) x1++;
49         else y1++;
50         if(x2==n) y2++;
51         else x2++;
52     }
53     for(int i=0;i<co;i++){
54         sort(v1[i].begin(),v1[i].end());
55         sort(v2[i].begin(),v2[i].end());
56         for(int j=0;j<v1[i].size();j++){
57             if(v1[i][j]!=v2[i][j]){
58                 cout<<"NO"<<endl;
59                 return 0;
60             }
61         }
62     }
63     cout<<"YES"<<endl;
64 }
View Code

 

D

贪心

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 13000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 int n,m;
24 int a[300005];
25 set<pii>se;
26 vector<int>ve;
27 
28 int main(){
29     #ifndef ONLINE_JUDGE
30      //   freopen("1.txt","r",stdin);
31     #endif
32     std::ios::sync_with_stdio(false);
33     cin>>n>>m;
34     for(int i=1;i<=n;i++) cin>>a[i];
35     int u,v;
36     for(int i=0;i<m;i++){
37         cin>>u>>v;
38         se.insert({u,v});
39     }
40     ve.pb(a[n]);
41     int flag;
42     int ans=0;
43     for(int i=n-1;i;i--){
44         flag=1;
45         for(int j=0;j<ve.size();j++){
46             if(!se.count({a[i],ve[j]})){
47                 flag=0;
48                 break;
49             }
50         }
51         if(flag)
52             ans++;
53         else ve.pb(a[i]);
54     }
55     cout<<ans<<endl;
56 }
View Code

 

E

线段树好题

参考博客:https://www.cnblogs.com/pkgunboat/p/10527569.html

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define lson l,mid,rt<<1
  4 #define rson mid+1,r,rt<<1|1
  5 #define sqr(x) ((x)*(x))
  6 #define pb push_back
  7 #define eb emplace_back
  8 #define maxn 100005
  9 #define eps 1e-8
 10 #define pi acos(-1.0)
 11 #define rep(k,i,j) for(int k=i;k<j;k++)
 12 typedef long long ll;
 13 typedef pair<int,int> pii;
 14 typedef pair<long long,int>pli;
 15 typedef pair<int,char> pic;
 16 typedef pair<pair<int,string>,pii> ppp;
 17 typedef unsigned long long ull;
 18 const long long MOD=1e9+7;
 19 /*#ifndef ONLINE_JUDGE
 20         freopen("1.txt","r",stdin);
 21 #endif */
 22 
 23 int n,m;
 24 ll a[maxn];
 25 ll b[maxn];
 26 ll c[maxn];
 27 ll tree[maxn<<3],lazy[maxn<<3];
 28 
 29 
 30 void push_up(int rt){
 31     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
 32 }
 33 
 34 void push_down(int l,int r,int rt){
 35     if(lazy[rt]!=-0x3f3f3f3f3f3f3f3f){
 36         ll mid=l+r>>1;
 37         lazy[rt<<1]=lazy[rt];
 38         lazy[rt<<1|1]=lazy[rt];
 39         tree[rt<<1]=(mid-l+1)*lazy[rt]+(c[mid]-c[l-1]);
 40         tree[rt<<1|1]=(r-mid)*lazy[rt]+(c[r]-c[mid]);
 41         lazy[rt]=-0x3f3f3f3f3f3f3f3f;
 42     }
 43 }
 44 
 45 void build(int l,int r,int rt){
 46     lazy[rt]=-0x3f3f3f3f3f3f3f3f;
 47     if(l==r){
 48         tree[rt]=a[l];
 49         return;
 50     }
 51     int mid=l+r>>1;
 52     build(lson);
 53     build(rson);
 54     push_up(rt);
 55 }
 56 
 57 ll query(int L,int R,int l,int r,int rt){
 58     if(L<=l&&R>=r){
 59         return tree[rt];
 60     }
 61     int mid=l+r>>1;
 62     push_down(l,r,rt);
 63     ll ans=0;
 64     if(L<=mid) ans+=query(L,R,lson);
 65     if(R>mid) ans+=query(L,R,rson);
 66     return ans;
 67 }
 68 
 69 void update(int L,int R,ll v,int l,int r,int rt){
 70     if(L<=l&&R>=r){
 71         tree[rt]=(r-l+1)*v+(c[r]-c[l-1]);
 72         lazy[rt]=v;
 73         return;
 74     }
 75     int mid=l+r>>1;
 76     push_down(l,r,rt);
 77     if(L<=mid) update(L,R,v,lson);
 78     if(R>mid) update(L,R,v,rson);
 79     push_up(rt);
 80 }
 81 
 82 int main(){
 83     #ifndef ONLINE_JUDGE
 84      //   freopen("1.txt","r",stdin);
 85     #endif
 86     std::ios::sync_with_stdio(false);
 87     cin>>n;
 88     for(int i=1;i<=n;i++) cin>>a[i];
 89     for(int i=2;i<=n;i++){
 90         cin>>b[i];
 91         b[i]+=b[i-1];
 92     }
 93     for(int i=2;i<=n;i++){
 94         c[i]=c[i-1]+b[i];
 95     }
 96     build(1,n,1);
 97     cin>>m;
 98     char pos;
 99     int x,y;
100     for(int i=1;i<=m;i++){
101         cin>>pos>>x>>y;
102         if(pos=='+'){
103             int L=x,R=n,mid;
104             ll init=query(x,x,1,n,1),tmp;
105             while(L<=R){
106                 mid=L+R>>1;
107                 tmp=query(mid,mid,1,n,1);
108                 if(init+y+b[mid]-b[x]>tmp){
109                     L=mid+1;
110                 }
111                 else{
112                     R=mid-1;
113                 }
114             }
115             update(x,R,init+y-b[x],1,n,1);
116         }
117         else{
118             cout<<query(x,y,1,n,1)<<endl;
119         }
120     }
121 }
View Code

 

posted on 2019-03-14 15:05  Fighting_sh  阅读(127)  评论(0编辑  收藏  举报

导航