@Plutorabbit
2017-02-19T10:48:39.000000Z
字数 5217
阅读 1860
BZOJ
2016
OI
BZOJ 4519~BZOJ 4524
KD-tree裸题
当时学校做练习的时候并不会这个东西QAQ
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int NN = 100005;
int n,k,rt,q0,q1,cmp_d;
struct TR
{
int d[2],mn[2],mx[2],lc,rc;
}tr[NN];
priority_queue<LL,vector<LL>,greater<LL> >que;
bool cmp(TR xx,TR yy) { return xx.d[cmp_d] < yy.d[cmp_d]; }
LL sqr(LL xx) { return xx*xx; }
void push_up(int k)
{
int l = tr[k].lc, r = tr[k].rc;
for (int i=0;i<2;++i)
{
if (l) tr[k].mx[i] = max(tr[k].mx[i],tr[l].mx[i]), tr[k].mn[i] = min(tr[k].mn[i],tr[l].mn[i]);
if (r) tr[k].mx[i] = max(tr[k].mx[i],tr[r].mx[i]), tr[k].mn[i] = min(tr[k].mn[i],tr[r].mn[i]);
}
}
void Build(int &k,int l,int r,int D)
{
int mid = (l+r)>>1; k=mid, cmp_d=D;
nth_element(tr+l,tr+mid,tr+r+1,cmp);
for (int i=0;i<2;++i) tr[k].mx[i] = tr[k].mn[i] = tr[k].d[i];
if (l<mid) Build(tr[k].lc,l,mid-1,D^1);
if (r>mid) Build(tr[k].rc,mid+1,r,D^1);
push_up(k);
}
LL Ask(int k)
{
if (!k) return 0;
return max(sqr(tr[k].mn[0]-q0),sqr(tr[k].mx[0]-q0)) + max(sqr(tr[k].mn[1]-q1),sqr(tr[k].mx[1]-q1));
}
void Query(int k)
{
LL dis = sqr(tr[k].d[0]-q0) + sqr(tr[k].d[1]-q1);
if (dis>que.top()) que.pop(), que.push(dis);
LL Dl = Ask(tr[k].lc), Dr = Ask(tr[k].rc);
if (Dl > Dr)
{
if (Dl > que.top()) Query(tr[k].lc);
if (Dr > que.top()) Query(tr[k].rc);
}
else
{
if (Dr > que.top()) Query(tr[k].rc);
if (Dl > que.top()) Query(tr[k].lc);
}
}
int main()
{
scanf("%d%d",&n,&k);
for (int i=1;i<=n;++i) scanf("%d%d",&tr[i].d[0],&tr[i].d[1]);
Build(rt,1,n,0);
for (int i=1;i<=(k<<1);++i) que.push(0);
for (int i=1;i<=n;++i) q0 = tr[i].d[0], q1 = tr[i].d[1], Query(rt);
printf("%lld\n",que.top());
return 0;
}
数位DP
表示 len 当前数值 重复 4/8 ==a[len]
语文越来越差了却还要考学考
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#define LL long long
using namespace std;
LL l,r,ans,f[15][11][5][5][3];
int a[20],tmp[20];
int Xun(int xx,int yy)
{
if (xx+yy>=3) return 3;
else if (xx) return yy+1;
else return 1;
}
int P(int xx,int yy)
{
if (xx==4) return yy|1;
else if (xx==8) return yy|2;
else return yy;
}
LL solve(LL xx)
{
int len=0;
memset(a,0,sizeof(a)); memset(tmp,0,sizeof(tmp));
memset(f,0,sizeof(f));
while (xx) { tmp[++len]=xx%10; xx=xx/10;}
for (int i=1;i<=len;++i) a[i]=tmp[len-i+1];
for (int i=1;i<=a[1];++i) f[1][i][1][P(i,0)][i==a[1]]++;
for (int i=2;i<=len;++i)
{
for (int j=0;j<=9;++j)
for (int k=1;k<=3;++k)
for (int w=0;w<3;++w)
{
if (f[i-1][j][k][w][0])
for (int t=0;t<=9;++t)
f[i][t][Xun(t==j,k)][P(t,w)][0]+=f[i-1][j][k][w][0];
if (f[i-1][j][k][w][1])
for (int t=0;t<=a[i];++t)
f[i][t][Xun(t==j,k)][P(t,w)][t==a[i]]+=f[i-1][j][k][w][1];
}
}
ans=0;
for (int i=0;i<=9;++i)
for (int j=0;j<3;++j) ans+=f[len][i][3][j][0]+f[len][i][3][j][1];
return ans;
}
int main()
{
scanf("%lld%lld",&l,&r);
if (l==10000000000ll) printf("%lld",solve(r));
else printf("%lld",solve(r)-solve(l-1));
return 0;
}
数论模板大综合
要是考场上考泼辣的肉肯定自爆
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define T 10007
LL e,n,c,p,q,r,inv;
LL Mul(LL xx,LL yy,LL mod)
{
LL sum = 0ll;
for (xx%=mod;yy;xx = (xx+xx) % mod, yy >>= 1) if (yy & 1) sum = (sum + xx) % mod;
return sum;
}
LL KSM(LL xx,LL yy,LL mod)
{
LL sum=1ll;
for (xx%=mod;yy;xx = Mul(xx,xx,mod)%mod,yy >>= 1) if (yy&1) sum = Mul(sum,xx,mod)%mod;
return sum;
}
void exGCD(LL a,LL b,LL &x,LL &y)
{
if (b == 0) { x = 1, y = 0; return; }
exGCD(b,a%b,y,x), y -= a/b*x;
}
LL Get_inv(LL n,LL mod)
{
LL x,y;
exGCD(n,mod,x,y);
return (x%mod + mod) % mod;
}
LL GCD(LL a,LL b)
{
while (a) a ^= b ^= a ^= b %= a;
return b;
}
LL Pollard_Rho(LL n)
{
LL x = rand() % (n-1) + 1,y = x,cnt = 1, k = 2;
while (1)
{
++cnt;
x = (Mul(x,x,n) + T) % n;
LL gcd = GCD(abs(x-y),n);
if (gcd > 1 && gcd < n) return gcd;
if (x == y) return n;
if (cnt == k) y = x, k <<= 1;
}
}
int main()
{
srand(T);
scanf("%lld%lld%lld",&e,&n,&c);
p = Pollard_Rho(n), q = n / p;
r = (p - 1) * (q - 1);
inv = Get_inv(e,r);
printf("%lld %lld\n",inv,KSM(c,inv,n));
return 0;
}
感觉现在看到各种01的题目都想着暴力上trie什么的QAQ
反正这题就是暴力上trie
用一个单调栈,使得能够匹配的表项出现的时间随着长度的增加而增加
#include <bits/stdc++.h>
using namespace std;
const int NN = 10005,MM = 31000005;
int X[5],rt,tot,ch[MM][2],v[MM],cnt,top,stk[NN],n,l,r;
char op[11];
void Insert(int l,int t)
{
int now = rt;
for (int i=1;i<=4;++i)
for (int j=7;~j;--j)
{
if (!l)
{
v[now] = t;
return;
}
--l;
if (!ch[now][(X[i]>>j)&1]) ch[now][(X[i]>>j)&1] = ++tot;
now = ch[now][(X[i]>>j)&1];
}
v[now] = t;
}
int Query(int l,int r)
{
int now = rt;
top = 0;
for (int i=1;i<=4;++i)
for (int j=7;~j;--j)
{
now = ch[now][(X[i]>>j)&1];
if (!now)
{
int res = top;
for (int k=1;k<=top;++k) if (stk[k] < l) res --;
return res;
}
if (v[now] <= r && v[now])
{
for (;top&&stk[top] > v[now];--top);
stk[++top] = v[now];
}
}
int res = top;
for (int k=1;k<=top;++k) if (stk[k] < l) -- res;
return res;
}
int main()
{
scanf("%d",&n);
rt = tot = 1;
while (n--)
{
scanf("%s",op);
if (op[0] == 'A')
{
scanf("%d.%d.%d.%d/%d",&X[1],&X[2],&X[3],&X[4],&l);
Insert(l,++cnt);
}
else
{
scanf("%d.%d.%d.%d%d%d",&X[1],&X[2],&X[3],&X[4],&l,&r);
printf("%d\n",Query(l,r));
}
}
return 0;
}
泥怎么还用STL啊
按顺序乱搞用堆维护就可以了QAQ
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
struct REC
{
LL v;
int t,pre,p;
}tmp;
bool operator <(REC xx,REC yy) { return xx.v < yy.v; }
priority_queue <REC> que;
LL n;
int k,pri[55],f[205],tot=0;
int main()
{
scanf("%lld%d",&n,&k);
for (int i=2;i<=128;++i)
if (!f[i])
{
pri[++tot] = i;
for (int j=i+i;j<=128;j+=i) f[j] = 1;
}
while (!que.empty()) que.pop();
for (int i=1;i<=tot;++i)
for (LL t=1,j=1;(LL)(t*pri[i]) <= n;++j)
t *= (LL)pri[i], tmp.v = t, tmp.t = (int)j, tmp.pre = i-1, tmp.p = i, que.push(tmp);
while (k--)
{
tmp = que.top(), que.pop();
if (tmp.t > 1)
{
for (int i=tmp.pre;i;--i)
que.push((REC){(LL)tmp.v/pri[tmp.p]*pri[i],tmp.t-1,i,tmp.p});
}
}
printf("%lld\n",tmp.v);
return 0;
}