ACM-ICPC 2018 徐州赛区网络预赛(部分

太菜了,没办法,只做出3题,F题是赛后a的,改了一行代码就过了,I题水题就不放上来了,注意只有一位的情况(wa了九次),F题自己lca不熟wa了两次,花了两个小时写,明明15分钟就能写好的,菜的不行。

H

两块线段树

一个存和,一个存i*sum[n-i];

每次求的就是红色那一部分的东西(自己意会吧

#include <cstdio>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
int const MAX = 1e5 + 5;
int n;
ll arr[MAX];
ll sum[MAX<<2];
ll sumn[MAX<<2];

void PushUp(ll sum[], int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; 
}
 

 
void Build(int l, int r, int rt)
{

    if(l==r){ sum[rt] = arr[l]; return; }

    int mid = (l + r)>>1;
    Build(lson);
    Build(rson);
    PushUp(sum, rt);
}

void Build2(int l, int r, int rt)
{

    if(l==r){ sumn[rt] = arr[l]*(n+1-l); return; }

    int mid = (l + r)>>1;
    Build2(lson);
    Build2(rson);
    PushUp(sumn, rt);
}
void Update(int pos, int val, int l, int r, int rt)
{
    if(l==r)
    {
        sum[rt] = val;
        return;
    }
    int mid = (l + r) >> 1;
    if(pos<=mid)
        Update(pos, val, lson);
    else
        Update(pos, val, rson);
    PushUp(sum, rt); 
}
void Update2(int pos, int val, int l, int r, int rt)
{
    if(l==r)
    {
        sumn[rt] = (ll)val*(n+1-l);
        return;
    }
    int mid = (l + r) >> 1;
    if(pos<=mid)
        Update2(pos, val, lson);
    else
        Update2(pos, val, rson);
    PushUp(sumn, rt); 
}
 
ll Query(int L, int R, int l, int r, int rt)
{
    if(L<=l && r<=R)
        return sum[rt];
    int mid = (l + r) >> 1;
    ll ans = 0;
    if(L<=mid)
        ans += Query(L, R, lson);
    if(mid<R)
        ans += Query(L, R, rson);
    return ans;
}

ll Query2(int L, int R, int l, int r, int rt)
{
    if(L<=l && r<=R)
        return sumn[rt];
    int mid = (l + r) >> 1;
    ll ans = 0;
    if(L<=mid)
        ans += Query2(L, R, lson);
    if(mid<R)
        ans += Query2(L, R, rson);
    return ans;
}
int main()
{
	int q;
    scanf("%d %d", &n, &q);
    for(int i=1;i<=n;i++) scanf("%lld", &arr[i]);
    Build(1, n, 1);
    Build2(1, n, 1);

    while(q--)
    {
    	int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        if(a==1) printf("%lld\n", Query2(b, c, 1, n, 1)- (n-c)*Query(b, c, 1, n, 1));
        else
        	Update(b, c, 1, n, 1), Update2(b, c, 1, n, 1);
    }
    
    return 0;
}

F

题意:找出相邻帧数重复出现的特征

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
const int maxn=1e5+100;
typedef pair<string,string> P;
map<P,int> maze;
vector<int>g[maxn];

int main()
{
 	int T;scanf("%d",&T);
    while(T--)
    {
        maze.clear();
        for(int i=0;i<maxn;i++)
            g[i].clear();
        int cnt=0;
        int n;scanf("%d",&n);
        while(n--)
        {
			int d;scanf("%d",&d);
            while(d--){
				string a,b;
                cin>>a>>b;
                P p(a,b);
                if(!maze[p])
                    maze[p]=++cnt;
                int wz=maze[p];
                int wc=g[wz].size()-1;
                //printf("%d %d\n",wz,wc);
                if(wc==-1||g[wz][wc]!=n)
               		g[wz].push_back(n);
            }
        }
        int maxx=0; 
        for(int i=1;i<=cnt;i++)
        {
            int now=1;
			for(int j=1;j<g[i].size();j++)
            {
                //printf("%d ",g[i][j]);
				if(g[i][j]==g[i][j-1]-1)now++;
                else now=1;
                maxx=max(maxx,now);
            }
           // printf("\n");
        }
        if(maxx>1&&cnt>1)printf("%d\n",maxx);
        else printf("0\n");
    }
    return 0;
}

J

题解:最大生成树+lca

#include <cstdio>
#include <cstring>
#include <iostream>
#include<queue>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn=510*510;

struct line
{
    int u,v,cost;
    bool operator<(const line&a)const
    {
        return a.cost>cost;
    }  
};

struct note
{
    int u,v,w;
    ll lca,next;
}edge[maxn*2],edge1[maxn];
 
int head[maxn],ip,head1[maxn],ip1;
int m,n;
int father[maxn],vis[maxn];
ll  ance[maxn],dir[maxn];
priority_queue<line>que; 
void init()
{
    memset(vis,0,sizeof(vis));
    memset(dir,0,sizeof(dir));
    memset(head,-1,sizeof(head));
    memset(head1,-1,sizeof(head1));
    ip=ip1=0;
    for(int i=0;i<=n*m;i++)
    	father[i]=i;
}
 
void addedge(int u,int v,int w)
{
    edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}
 
void addedge1(int u,int v)
{
    edge1[ip1].u=u,edge1[ip1].v=v,edge1[ip1].lca=-1,edge1[ip1].next=head1[u],head1[u]=ip1++;
}
 
int  Find(int x)
{
    if(father[x]==x)
        return x;
    return father[x]=Find(father[x]);
}
int find(int x)
{
	return x==father[x]?x:father[x]=find(father[x]);
}
void Union(int x,int y)
{
    x=Find(x);
    y=Find(y);
    if(x!=y)
        father[y]=x;
}
 
void tarjan(int u)
{
    vis[u]=1;
    ance[u]=father[u]=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(!vis[v])
        {
           dir[v]=dir[u]+w;
           tarjan(v);
           Union(u,v);
        }
    }
    for(int i=head1[u];i!=-1;i=edge1[i].next)
    {
        int v=edge1[i].v;
        if(vis[v])
        {
            edge1[i].lca=edge1[i^1].lca=ance[Find(v)];
        }
    }
}
void krush()
{
    while(!que.empty())
    {
        line tmp = que.top();
        que.pop();
        int u=tmp.u,v=tmp.v;  
        int x=find(u),y=find(v);
        //printf("%d %d %d %d\n",u,v,x,y);
        if(x==y)continue;
        //printf("%d %d\n",u,v);
        father[x]=y;
        addedge(u,v,1);
        addedge(v,u,1);
    }
}
int main()
{ 
       scanf("%d%d",&n,&m);
       init();
       for(int i=0;i<n;i++)
       {
           for(int j=1;j<=m;j++)
           {
               char a,c;
               ll b,d;
               scanf(" %c%lld %c%lld",&a,&b,&c,&d);
               if(a=='D'){
                   line tmp=(line){j+i*n,j+(i+1)*n,b};
                   que.push(tmp);
               }
               if(c=='R'){
                   line tmp=(line){j+i*n,j+i*n+1,d};
                   que.push(tmp);
               }
                        //printf("1\n");  
           }
       }
       krush();
       for(int i=0;i<=n*m;i++)
           father[i]=0;
       int qry;scanf("%d",&qry);
       for(int i=0;i<qry;i++)
       {
           int u,u1,v,v1;
           scanf("%d%d%d%d",&u,&u1,&v,&v1);
           u=(u-1)*n+u1;
           v=(v-1)*n+v1;
           addedge1(u,v);
           addedge1(v,u);
       }
       dir[1]=0;
       tarjan(1);
       for(int i=0;i<qry;i++)
       {
           int s=i*2,u=edge1[s].u,v=edge1[s].v;
           ll lca=edge1[s].lca;
           printf("%lld\n",dir[u]+dir[v]-2*dir[lca]);
       }
   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值