2015 Multi-University Training Contest 3(hdu 5316、5317、5319、5323、5325、5326)线段树+数学+yy+矩阵快速幂

50 篇文章 0 订阅
40 篇文章 0 订阅

Magician

Problem Description

Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from another being, often a god, spirit, or demon of some sort. Some wizards are depicted as having a special gift which sets them apart from the vast majority of characters in fantasy worlds who are unable to learn magic.

Magicians, sorcerers, wizards, magi, and practitioners of magic by other titles have appeared in myths, folktales, and literature throughout recorded history, with fantasy works drawing from this background.

In medieval chivalric romance, the wizard often appears as a wise old man and acts as a mentor, with Merlin from the King Arthur stories representing a prime example. Other magicians can appear as villains, hostile to the hero.

Mr. Zstu is a magician, he has many elves like dobby, each of which has a magic power (maybe negative). One day, Mr. Zstu want to test his ability of doing some magic. He made the elves stand in a straight line, from position 1 to position n, and he used two kinds of magic, Change magic and Query Magic, the first is to change an elf’s power, the second is get the maximum sum of beautiful subsequence of a given interval. A beautiful subsequence is a subsequence that all the adjacent pairs of elves in the sequence have a different parity of position. Can you do the same thing as Mr. Zstu ?

Input

The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m represent the number of elves and the number of time that Mr. Zstu used his magic.
(n,m <= 100000)
The next line has n integers represent elves’ magic power, magic power is between -1000000000 and 1000000000.
Followed m lines, each line has three integers like
type a b describe a magic.
If type equals 0, you should output the maximum sum of beautiful subsequence of interval [a,b].(1 <= a <= b <= n)
If type equals 1, you should change the magic power of the elf at position a to b.(1 <= a <= n, 1 <= b <= 1e9)

Output

For each 0 type query, output the corresponding answer.

Sample Input

1
1 1
1
0 1 1

Sample Output

1
题意:有两种操作,0表示求区间[L,R]中的最大的beautiful subsequence的和,所谓的beautiful subsequence是指序列中的相邻的数在原序列中的位置的奇偶性不同
思路:维护奇数开头奇数结尾,偶数开头偶数结尾,奇数开头偶数结尾,偶数开头奇数结尾四个值,就可以了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100010;
const int maxm=1010;
const int INF=0x3f3f3f3f;
int N,M;
LL a[maxn];
struct node
{
    LL oo,ee,oe,eo;
    node(LL a=-INF,LL b=-INF,LL c=-INF,LL d=-INF):oo(a),ee(b),oe(c),eo(d){}
};
struct IntervalTree
{
    LL oo[maxn<<2],ee[maxn<<2],eo[maxn<<2],oe[maxn<<2];
    void build(int o,int l,int r)
    {
        oo[o]=ee[o]=oe[o]=eo[o]=-INF;
        if(l==r)
        {
            if(l&1)oo[o]=a[l];
            else ee[o]=a[l];
            return ;
        }
        int mid=(l+r)>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        pushup(o);
    }
    void update(int o,int l,int r,int pos,int val)
    {
        if(l==r)
        {
            if(l&1)oo[o]=val;
            else ee[o]=val;
            //cout<<l<<endl;
            return ;
        }
        int mid=(l+r)>>1;
        if(pos<=mid)update(o<<1,l,mid,pos,val);
        else update(o<<1|1,mid+1,r,pos,val);
        pushup(o);
    }
    void pushup(int o)
    {
        oo[o]=max(oo[o<<1],oo[o<<1|1]);
        oo[o]=max(oo[o],max(oe[o<<1]+oo[o<<1|1],oo[o<<1]+eo[o<<1|1]));
        ee[o]=max(ee[o<<1],ee[o<<1|1]);
        ee[o]=max(ee[o],max(eo[o<<1]+ee[o<<1|1],ee[o<<1]+oe[o<<1|1]));
        oe[o]=max(oe[o<<1],oe[o<<1|1]);
        oe[o]=max(oe[o],max(oo[o<<1]+ee[o<<1|1],oe[o<<1]+oe[o<<1|1]));
        eo[o]=max(eo[o<<1],eo[o<<1|1]);
        eo[o]=max(eo[o],max(ee[o<<1]+oo[o<<1|1],eo[o<<1]+eo[o<<1|1]));
    }
    node solve(node &ans1,node &ans2)
    {
        node ans;
        ans.oo=max(ans1.oe+ans2.oo,ans1.oo+ans2.eo);
        ans.oo=max(ans.oo,max(ans1.oo,ans2.oo));
        ans.ee=max(ans1.eo+ans2.ee,ans1.ee+ans2.oe);
        ans.ee=max(ans.ee,max(ans1.ee,ans2.ee));
        ans.oe=max(ans1.oo+ans2.ee,ans1.oe+ans2.oe);
        ans.oe=max(ans.oe,max(ans1.oe,ans2.oe));
        ans.eo=max(ans1.ee+ans2.oo,ans1.eo+ans2.eo);
        ans.eo=max(ans.eo,max(ans1.eo,ans2.eo));
        return ans;
    }
    node query(int o,int l,int r,int q1,int q2)
    {
        if(q1<=l&&r<=q2)
        {
            return node(oo[o],ee[o],oe[o],eo[o]);
        }
        int mid=(l+r)>>1;
        node ans1,ans2,ans;
        if(q1<=mid)ans1=query(o<<1,l,mid,q1,q2);
        if(q2>mid)ans2=query(o<<1|1,mid+1,r,q1,q2);
        ans=solve(ans1,ans2);
        return ans;
    }
}tree;
int main()
{
    int T;
    int op,x,y;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&M);
        for(int i=1;i<=N;i++)scanf("%I64d",&a[i]);
        tree.build(1,1,N);
        while(M--)
        {
            scanf("%d%d%d",&op,&x,&y);
            if(op==0)
            {
                node ans=tree.query(1,1,N,x,y);
                printf("%I64d\n",max(max(ans.oo,ans.ee),max(ans.eo,ans.oe)));
            }
            else tree.update(1,1,N,x,y);
        }
    }
    return 0;
}

RGCDQ

Problem Description

Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know maxGCD(F(i),F(j)) (Li<jR)

Input

There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.

All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000

Output

For each query,output the answer in a single line.
See the sample for more details.

Sample Input

2
2 3
3 5

Sample Output

1
1

思路:因为2*3*5*7*11*13*19已经大于1000000了,所以最多有7种质因子,所以预处理出每种质因子的数量,然后找一下就行了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=1000010;
const int maxm=100010;
const int INF=0x3f3f3f3f;
int T,L,R;
int prime[maxn];
bool vis[maxn];
int sum[maxn][8];
void init()
{
    int k=0;
    memset(vis,0,sizeof(vis));
    memset(sum,0,sizeof(sum));
    vis[1]=1;
    for(int i=2;i<maxn;i++){
        if(!vis[i]) prime[k++]=i;
        for(int j=0;j<k&&i*prime[j]<maxn;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
    for(int i=1;i<maxn;i++)
    {
        int cnt=0;
        int x=i;
        if(!vis[x])
        {
            sum[i][1]++;
            continue;
        }
        for(int j=0;j<k&&prime[j]<x;j++)
        {
            if(x%prime[j])continue;
            cnt++;
            while(x%prime[j]==0)x/=prime[j];
            if(!vis[x])break;
        }
        if(!vis[x])cnt++;
        sum[i][cnt]++;

    }
    for(int i=1;i<maxn;i++)
        for(int j=0;j<8;j++)
        sum[i][j]+=sum[i-1][j];
}
int tmp[10];
int main()
{
    scanf("%d",&T);
    init();
    while(T--)
    {
        scanf("%d%d",&L,&R);
        for(int i=0;i<8;i++)
            tmp[i]=sum[R][i]-sum[L-1][i];
        int ans=1;
        for(int i=7;i>1;i--)
            if(tmp[i]>1)ans=max(ans,i);
        if(tmp[2]&&tmp[4])ans=max(ans,2);
        if(tmp[3]&&tmp[6])ans=max(ans,3);
        if(tmp[2]&&tmp[6])ans=max(ans,2);
        printf("%d\n",ans);
    }
    return 0;
}

Crazy Bobo

Problem Description
Bobo has a tree,whose vertices are conveniently labeled by 1,2,…,n.Each node has a weight wi. All the weights are distrinct.
A set with m nodes v1,v2,...,vm is a Bobo Set if:
- The subgraph of his tree induced by this set is connected.
- After we sort these nodes in set by their weights in ascending order,we get u1,u2,...,um ,(that is, wui<wui+1 for i from 1 to m-1).For any node x in the path from uitoui+1 (excluding uiandui+1 ),should satisfy wx<wui.
Your task is to find the maximum size of Bobo Set in a given tree.

Input
The input consists of several tests. For each tests:
The first line contains a integer n (1≤n≤500000). Then following a line contains n integers w1,w2,…,wn ( 1wi109, all the wi is distrinct).Each of the following n-1 lines contain 2 integers ai and bi,denoting an edge between vertices ai and bi (1≤ai,bi≤n).
The sum of n is not bigger than 800000.

Output
For each test output one line contains a integer,denoting the maximum size of Bobo Set.

Sample Input

7
3 30 350 100 200 300 400
1 2
2 3
3 4
4 5
5 6
6 7

Sample Output

5
思路:权值小的向大的连一条有向边,然后求出每个点能到达的有多少个点,取个大的,比赛的时候没想出来,这样做之所以对,是因为对于排序后,任意相邻的两个节点,他们之间的路径,可定都是从当前节点u连过去的,那么都是小于这两个节点的

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;
const int maxn=500010;
const int maxm=100010;
const int INF=0x3f3f3f3f;
int N,a[maxn],cnt[maxn];
vector<int> G[maxn];
void dfs(int u)
{
    cnt[u]=1;
    int len=G[u].size();
    for(int i=0;i<len;i++)
    {
        int v=G[u][i];
        if(!cnt[v])dfs(v);
        cnt[u]+=cnt[v];
    }
}
int main()
{
    int u,v;
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&a[i]);
            G[i].clear();
        }
        for(int i=1;i<N;i++)
        {
            scanf("%d%d",&u,&v);
            if(a[u]<a[v])G[u].push_back(v);
            else G[v].push_back(u);
        }
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=N;i++)
            if(!cnt[i])dfs(i);
        int ans=-1;
        for(int i=1;i<=N;i++)
            ans=max(ans,cnt[i]);
        printf("%d\n",ans);
    }
    return 0;
}

Solve this interesting problem

Problem Description

Have you learned something about segment tree? If not, don’t worry, I will explain it for you.
Segment Tree is a kind of binary tree, it can be defined as this:
- For each node u in Segment Tree, u has two values: Lu and Ru.
- If Lu=Ru, u is a leaf node.
- If Lu≠Ru, u has two children x and y,with Lx=Lu,Rx=⌊Lu+Ru2⌋,Ly=⌊Lu+Ru2⌋+1,Ry=Ru.
Here is an example of segment tree to do range query of sum.

Given two integers L and R, Your task is to find the minimum non-negative n satisfy that: A Segment Tree with root node’s value Lroot=0 and Rroot=n contains a node u with Lu=L and Ru=R.

Input

The input consists of several test cases.
Each test case contains two integers L and R, as described above.
0≤L≤R≤109
LR−L+1≤2015

Output

For each test, output one line contains one integer. If there is no such n, just output -1.

Sample Input

6 7
10 13
10 11

Sample Output

7
-1
12
这里写图片描述

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=2010;
const int maxm=100010;
const LL INF=1e18;
LL L,R;
LL ans;
void dfs(LL l,LL r)
{
    if(r>=ans||l<0)return ;
    if(l==0)
    {
        ans=r;
        return ;
    }
    if(r-l+1>l)return ;
    dfs(2*l-r-1,r);
    dfs(2*l-r-2,r);
    dfs(l,2*r-l);
    dfs(l,2*r-l+1);

}
int main()
{
    while(scanf("%I64d%I64d",&L,&R)!=EOF)
    {
        ans=INF;
        dfs(L,R);
        printf("%I64d\n",ans==INF?-1:ans);
    }
    return 0;
}

The Goddess Of The Moon
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 432 Accepted Submission(s): 147

Problem Description
Chang’e (嫦娥) is a well-known character in Chinese ancient mythology. She’s the goddess of the Moon. There are many tales about Chang’e, but there’s a well-known story regarding the origin of the Mid-Autumn Moon Festival. In a very distant past, ten suns had risen together to the heavens, thus causing hardship for the people. The archer Yi shot down nine of them and was given the elixir of immortality as a reward, but he did not consume it as he did not want to gain immortality without his beloved wife Chang’e.

However, while Yi went out hunting, Fengmeng broke into his house and forced Chang’e to give up the elixir of immortality to him, but she refused to do so. Instead, Chang’e drank it and flew upwards towards the heavens, choosing the moon as residence to be nearby her beloved husband.

Yi discovered what had transpired and felt sad, so he displayed the fruits and cakes that his wife Chang’e had liked, and gave sacrifices to her. Now, let’s help Yi to the moon so that he can see his beloved wife. Imagine the earth is a point and the moon is also a point, there are n kinds of short chains in the earth, each chain is described as a number, we can also take it as a string, the quantity of each kind of chain is infinite. The only condition that a string A connect another string B is there is a suffix of A , equals a prefix of B, and the length of the suffix(prefix) must bigger than one(just make the joint more stable for security concern), Yi can connect some of the chains to make a long chain so that he can reach the moon, but before he connect the chains, he wonders that how many different long chains he can make if he choose m chains from the original chains.

Input
The first line is an integer T represent the number of test cases.
Each of the test case begins with two integers n, m.
(n <= 50, m <= 1e9)
The following line contains n integer numbers describe the n kinds of chains.
All the Integers are less or equal than 1e9.

Output
Output the answer mod 1000000007.

Sample Input

2
10 50
12 1213 1212 1313231 12312413 12312 4123 1231 3 131
5 50
121 123 213 132 321

Sample Output

86814837
797922656
Hint
11 111 is different with 111 11

题意:如果A的后缀跟B的前缀相同,那么它们可以拼接在一块,问拼接M次,能得到多少不同的链
思路:矩阵快速幂。这个跟AC自动机的那个构造很像

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100;
const int maxm=100010;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
int N,M;
int a[maxn];
struct Matrix
{
    int mat[55][55];
    int n,m;
    Matrix(int n)
    {
        this->n=n;
        memset(mat,0,sizeof(mat));
    }
    Matrix operator*(Matrix &a)
    {
        Matrix res(n);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                for(int k=0;k<n;k++)
                (res.mat[i][j]+=1LL*mat[i][k]*a.mat[k][j]%MOD)%=MOD;
        }
        return res;
    }
};
Matrix pow_mul(Matrix A,LL x)
{
    Matrix res(A.n);
    for(int i=0;i<res.n;i++)
        res.mat[i][i]=1;
    while(x)
    {
        if(x&1)res=res*A;
        A=A*A;
        x>>=1;
    }
    return res;
}
bool judge(int i,int j)
{
    char A[20],B[20];
    sprintf(A,"%d",a[i]);
    sprintf(B,"%d",a[j]);
    int len1=strlen(A),len2=strlen(B);
    for(int i=0;i<len1;i++)
    {
        int k=0;
        while(i+k<len1&&k<len2&&A[i+k]==B[k])k++;
        if(i+k==len1&&k>1)
            return true;
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&M);
        for(int i=0;i<N;i++)scanf("%d",&a[i]);
        sort(a,a+N);
        N=unique(a,a+N)-a;
        if(N==0||M==0)
        {
            printf("0\n");
            continue;
        }
        Matrix A(N);
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
               if(judge(i,j))A.mat[i][j]=1;
        A=pow_mul(A,M-1);
        int ans=0;
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
            (ans+=A.mat[i][j])%=MOD;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值