hihoCoder挑战赛20 题解

打折机票

静态区间最大值SB题

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define eps (1e-5)
#define MAXN (1000000+10)
#define MAXLog (30)
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
class RMQ
{
public:
    int n,a[MAXN];
    RMQ(int _n=0):n(_n){}
    RMQ(int* _a,int _n=0):n(_n){memcpy(a,_a,sizeof(int)*_n);    }
    void mem(int* _a,int _n=0){n=_n;memcpy(a,_a,sizeof(int)*_n);    }
    int d[MAXN][MAXLog];
    void RMQ_init()
    {
        Rep(i,n) d[i][0]=a[i];
        for(int j=1;(1<<j)<=n;j++)
            for(int i=0;i + (1<<j) -1 < n; i++)
            {
                d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
            }
    } 
    int query(int L,int R)
    {
        if (L>R) swap(L,R);
        int k=floor(log(R-L+1)/log(2));
        return max(d[L][k],d[R-(1<<k)+1][k]);
    }
}S;
int n,m;
int a[MAXN];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
    n=read();m=read();
    MEM(a)
    For(i,n) {
        int t=read(),v=read();
        a[t]=max(a[t],v);
    }
    S.mem(a,100010);
    S.RMQ_init();
    For(i,m) {
        int l=read(),r=read();
        int p=S.query(l,r);
        if (p==0) puts("None");else printf("%d\n",p);
    }
    return 0;
}

筑地市场

求第k个有4或7的数
记得用__int128

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) 
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
typedef long long ll;
typedef  __int128 ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

ull g[50],p10[50];
 ll k;
void calc(int i,bool flag=0) {
    if (!i) return;
    Rep(p,10) {
        ull q=(p==4||p==7||flag)?(p10[i-1]):g[i-1];
        if (k>q) k-=q;
        else {
            cout<<p; calc(i-1,flag|(p==4||p==7)); break;
        }
    }
}
int main()
{
// freopen("C.in","r",stdin);
// freopen(".out","w",stdout);
    p10[0]=1;
    For(i,20) p10[i]=p10[i-1]*10LL; 
    g[0]=0;
    For(i,20) g[i]=g[i-1]*8+2*p10[i-1];
// For(i,20) cout<<g[i]<<endl;
    while(cin>>k){
        int len=1;
        while(g[len]<(ull)k) len++;
        calc(len);puts("");
    }
    return 0;
}

你可能感兴趣的:(hihoCoder挑战赛20 题解)