随笔分类 - uva
摘要:简单的线段树的题;有两种方法写这个题,目前用的熟是这种慢点的;不过不知道怎么老是T;感觉网上A过的人的时间度都好小,但他们都是用数组实现的难道是指针比数组慢?好吧,以后多用数组写写吧!超时的代码: 1 #include 2 #include 3 #include 4 #define maxn 1000009 5 using namespace std; 6 7 struct node 8 { 9 int l,r; 10 int ma,mi,sum; 11 int ad,st; 12 bool flag1,flag2; 13 node ...
阅读全文
摘要:又是一道线段树区间更新的题; 1 #include 2 #include 3 #include 4 #define ll long long 5 #define maxn 500005 6 using namespace std; 7 ll sum[maxn]; 8 struct tree 9 { 10 int l,r; 11 int ml,mr; 12 int pre,suf; 13 tree *left,*right; 14 } tr[maxn*2]; 15 16 int trcount; 17 18 void build(tree *...
阅读全文
摘要:对于组合游戏的题;首先把问题建模成NIM等经典的组合游戏模型;然后打表找出,或者推出SG函数值;最后再利用SG定理判断是否必胜必败状态; 1 #include 2 #define ll long long 3 using namespace std; 4 5 ll sg(ll x) 6 { 7 return x%2==0 ? x/2 : sg(x/2); 8 } 9 10 int main()11 {12 int t;13 scanf("%d",&t);14 while(t--)15 {16 int n;17 ll a,...
阅读全文
摘要:splay的题;学习白书上和网上的代码敲的; 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 int n,m; 7 struct node 8 { 9 node *ch[2]; 10 int s,v; 11 int flip; 12 node(int v):v(v) 13 { 14 ch[1]=ch[0]=NULL; 15 s=1; 16 flip=0; 17 } 18 voi...
阅读全文
摘要:用可重集;首先按照x排好序,然后只要找到下界,插入,然后把y坐标大于它的都删掉就行; 1 #include 2 #include 3 using namespace std; 4 5 struct node 6 { 7 int a,b; 8 bool operators;16 multiset::iterator it;17 18 int main()19 {20 int t,n,ca=1;21 node p;22 scanf("%d",&t);23 while(t--)24 {25 s.clear();26 ...
阅读全文
摘要:基于hash的LCP算法; 1 #include 2 #include 3 #include 4 #define maxn 40010 5 using namespace std; 6 7 const int x=123; 8 int n,m,pos; 9 unsigned long long h[maxn],xp[maxn],hash[maxn];10 int rank[maxn];11 12 bool cmp(const int &a,const int &b)13 {14 return hash[a]=m)pos=max(pos,rank[i]);31 }32 ...
阅读全文
摘要:花了一个半小时的时间,终于把这个题目给A了;知道用后缀数组后,这个题目其实不难;就一个二分;用白书当模板其实还挺不错的! 1 #include 2 #include 3 #include 4 #define maxn 110009 5 using namespace std; 6 7 int s[maxn]; 8 int sa[maxn],t[maxn],t2[maxn],c[maxn],n; 9 10 void build_sa(int m) 11 { 12 int *x=t,*y=t2; 13 for(int i=0; i=0; i--)sa[--c[...
阅读全文
摘要:对于两只狗在经过拐点之前,他们的运动可以简化为一只狗不动,另一只狗以他们的相对速度运动; 1 #include 2 #include 3 #include 4 #define maxn 66 5 #define eps 1e-6 6 using namespace std; 7 8 struct node 9 {10 double x,y;11 node(double x=0,double y=0):x(x),y(y){ }12 };13 node operator-(node u,node v){return node(u.x-v.x,u.y-v.y);}14 node o...
阅读全文
摘要:计算几何边的旋转、直线相交的应用代码: 1 #include 2 #include 3 using namespace std; 4 struct node 5 { 6 double x,y; 7 node(double x=0,double y=0):x(x),y(y){} 8 }a,b,c,d,e,f; 9 node operator-(node u,node v){return node(u.x-v.x,u.y-v.y);}10 node operator+(node u,node v){return node(u.x+v.x,u.y+v.y);}11 node o...
阅读全文
摘要:计算几何;直线交点; 1 #include 2 using namespace std; 3 4 struct node 5 { 6 double x,y; 7 node(double x=0,double y=0):x(x),y(y){ } 8 }a,b,c,d,e,f,p,q,r; 9 node operator-(node u,node v){return node(u.x-v.x,u.y-v.y);}10 node operator+(node u,node v){return node(u.x+v.x,u.y+v.y);}11 node operator*(node...
阅读全文
摘要:简单的AC自动机; 1 #include 2 #include 3 #include 4 #define maxn 150005 5 using namespace std; 6 7 struct node 8 { 9 int cnt; 10 int id; 11 node *a[26],*tail; 12 }no[maxn]; 13 int nonocount; 14 node *newnode() 15 { 16 node *p=no+nonocount++; 17 p->cnt=0; 18 p->id=-1; 19 ...
阅读全文
摘要:简单KMP: 1 #include 2 #include 3 #define maxn 1000009 4 using namespace std; 5 6 char s[maxn]; 7 int next[maxn],n; 8 9 void getnext()10 {11 int j=-1,i=0;12 next[0]=-1;13 while(i0&&i%(i-next[i])==0)35 printf("%d %d\n",i,i/(i-next[i]));36 puts("");37 }38 ret...
阅读全文
摘要:字典树;不过数据量很大;4000*1000;我用的是传统的方法建树,虽然优化了一下,边插入边统计,不过还是T了;这是我的代码: 1 #include 2 #include 3 #define maxn 4000009 4 using namespace std; 5 int nonocount; 6 long long ans; 7 struct node 8 { 9 int num;10 node *a[62];11 } no[maxn];12 13 node* newnode()14 {15 node *p=no+nonocount++;16 p->num=...
阅读全文
摘要:一个简单的字典树上的dp;代码: 1 #include 2 #include 3 #include 4 #define maxn 400000 5 #define mod 20071027 6 using namespace std; 7 priority_queue,greater >q; 8 struct node 9 {10 bool f;11 node *a[26];12 } no[maxn];13 int nonocount;14 int d[maxn];15 bool vis[maxn];16 node *newnode()17 {18 node *p=no...
阅读全文
摘要:简单题; 1 #include 2 #include 3 using namespace std; 4 int t,n,k; 5 sets; 6 int wei[20]; 7 void next() 8 { 9 if(k==0)return;10 long long kk=(long long)k*k;11 k=0;12 int cnt=0;13 while(kk>0)14 {15 wei[cnt++]=kk%10;16 kk/=10;17 }18 for(int i=0;i<n;i++)19 ...
阅读全文
摘要:把每个位置可能出现的最大值存一下就行;代码: 1 #include 2 #include 3 #include 4 #define maxn 100005 5 using namespace std; 6 7 int a[maxn],b[maxn]; 8 int main() 9 {10 int n,t;11 scanf("%d",&t);12 while(t--)13 {14 scanf("%d",&n);15 memset(b,0,sizeof b);16 for(int i=0; i<n; i++)scan...
阅读全文
摘要:竟然用二分,真是想不到;偶数的情况很容易想到;不过奇数的就难了;奇数的情况下,一个从后向前拿,一个从前向后拿的分配方法实在太妙了!注:白书上的代码有一点点错误代码: 1 #include 2 #define maxn 100009 3 #include 4 using namespace std; 5 6 int p[maxn],right[maxn],left[maxn],n; 7 8 bool check(int m) 9 {10 int x=p[1],y=m-p[1];11 left[1]=x;right[1]=0;12 for(int i=2;i>1;43 ...
阅读全文
摘要:这个题目关键在于把无根树变成有根树;这个用dfs; 然后用贪心的方法,从最深的那层开始,每次找到节点的上k层,建一个服务器,然后用一个dfs把这个服务器能够覆盖的节点标记; #include#include#include#include#define maxn 1005using namespace std;vectornode[maxn],deep[maxn];bool vis[maxn];i...
阅读全文
摘要:简单题; 1 #include 2 using namespace std; 3 4 char s[13][13]; 5 6 int n; 7 8 int main() 9 {10 int ca=1,t;11 scanf("%d",&t);12 while(t--)13 {14 scanf("%d",&n);15 for(int i=0; i0&&s[i-1][j]==ch)flag=0;24 if(j>0&&s[i][j-1]==ch)flag=0;25 ...
阅读全文
摘要:最大值最小的题;直接用二分,比较简单;不过我的二分老是不用好。有时间总结一下! 1 #include 2 #include 3 #include 4 #include 5 #include 6 #define maxn 1006 7 using namespace std; 8 int t,n,b,x,y,cnt; 9 mapmp;10 struct node11 {12 int p;13 int v;14 };15 vectorcom[maxn];16 char s[23],ss[23];17 bool check(int a)18 {19 int sum=0;20 ...
阅读全文