[12.16] Diary

12.16

Hint

VScode years, Alt + Z line can change multiple lines, press again to change back.

Segment tree

  1. HDU4553:http://acm.hdu.edu.cn/showproblem.php?pid=4553

    Winter came, and went to Xiao Ming and goddesses dating season.
      Xiao Ming Although the grass root level code farmers, but very active, enthusiastic goddess who often reply "Oh," following a statement by a large segment of the Internet Xiaoming, so, Xiao Ming is a favorite and goddesses dating. At the same time, there are many gay friend asked him to open the black, because the number is too large, how to arrange a time Xiao Ming has become a major mind.
      There will be many or goddess-based Friends come to us known Xiao Ming Xiao Ming, a total of T idle time period.
      As an operating system once anger exam 71 points Great God, Xiao Ming think of an algorithm, that is, "first fit algorithm", according to the description of the operating system textbooks, is to find contiguous space for some of the most forward of the meet the requirements assigned to each request, thus Xiao Ming made a decision:
      when a gay friend came Xiaoming, according to Xiao Ming on "first-fit algorithm" to find some free time and based Friends of the appointment, if found, say "X, let's fly" (here, X is the start time), otherwise say "fly with yourself";
      when the goddess came to Bob, first use a "first fit algorithm", if not found, Xiao Ming would run the risk of ignoring all wood Jiji Cock silk-based Friends of the convention, again using the "ignore-based Friends of the first-fit algorithm", twice as long as one is found, say "X, do not put my gezi " ( here, X is the start time), otherwise say " wait for me "
      of course, we know who Bob is not a festival parade negative infinity if and goddesses dating finished, time remaining, and he will be based Friends of the original appointment to dota. (For example: Konishi (Cock wire) Xiaoming appointment play dota within 1 to 5 the unit of time period, at this time, the goddess to Xiaoming reservation length of period 3, the final is 1 to 3, Xiaoming to goddess dating, playing dota at 4-5 and get back Konishi)
      Xiao Ming occasionally want to learn new knowledge, at this time Xiao Ming will all have a certain predetermined time interval of time to learn all the empty and Shout "I am the hope of chinese chengxuyuan !!", but are generally Xiaoming heat for three minutes, then it was to reserve it, Xiao Ming will be unable to bear alone the time to learn new knowledge dispensed.

Ideas : This title is in trouble, no technical content. Set two intervals information, one is best left full-range 0, a full 01 is the leftmost section (not easy when all 0s to find the goddess section 01), and after thinking the same as the original. Here you can write inline function with the structure and then look to reduce the workload.

  1. HDU3333: ask each interval and the number of different. Offline.

Ideas: Like counting the number of different intervals. First, press the right sort endpoint, after the last position of each number appears set to the original number, other positions are 0, and to seek the interval. Here is a modified single-point + range summation, Fenwick tree can be. The same array or implemented last (here must unordered_map).

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int M=1e5+20;
struct Query{
    int l,r,no;
    bool operator<(const Query &x)const{return r<x.r;}
}qu[M];
int a[M],n;
LL c[M],ans[M];
unordered_map<int,int> last;
inline int lowbit(int x){return x&-x;};
inline void operate(int x,int k){
    for(int i=x;i<=n;i+=lowbit(i))
        c[i]+=k;
}
inline LL query(int x){
    LL ans=0;
    for(int i=x;i;i-=lowbit(i))
        ans+=c[i];
    return ans;
}
inline LL BITquery(int l,int r){return query(r)-query(l-1);}
int main(){
    int T;
    scanf("%d",&T);
    for(int z=1;z<=T;++z){
        scanf("%d",&n);
        last.clear();
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]),c[i]=0;
        int q;
        scanf("%d",&q);
        for(int i=1;i<=q;++i)
            scanf("%d%d",&qu[i].l,&qu[i].r),qu[i].no=i;
        sort(qu+1,qu+q+1);
        int p=0;
        for(int i=1;i<=q;++i){
            while(p<qu[i].r){
                ++p;
                if (last[a[p]])
                    operate(last[a[p]],-a[p]);
                operate(p,a[p]);
                last[a[p]]=p;
            }
            ans[qu[i].no]=BITquery(qu[i].l,qu[i].r);
        }
        for(int i=1;i<=q;++i)
            printf("%lld\n",ans[i]);
    }
    return 0;
}

CF

  1. 1157E:https://codeforces.com/problemset/problem/1157/E

Thinking: This is the 1700 title? ? This is too much water it. set in lowerbound can be.

#include<bits/stdc++.h>
using namespace std;
const int M=2e5+20;
int a[M];
multiset<int> s;
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;++i){
        int c;
        scanf("%d",&c),s.insert(c);
    }
    for(int i=1;i<=n;++i){
        multiset<int>::iterator c=s.lower_bound(n-a[i]);
        if (c==s.end())
            c=s.begin();
        printf("%d ",((*c)+a[i])%n);
        s.erase(c);
    }
    return 0;
}
  1. 1154E: ability value to the n and k are between 1-n, two coaches sequentially pick one. Each time the ability to select the current value of most people, the right and left of k and k a total of 2k + 1 individual to enter his team (if not the k, its worst so far). Two coaches turn up did not do so until people. The output of each person's final team. \ (the n-\ Leq 2E5 \) .

Ideas: For this issue with both sides seeking to delete but still people can use to record and delete the list. Set can not be used, because the left and right is approximately set after sorting, not the original sequence. After the re-use segment tree can maintain maximum.

#include<bits/stdc++.h>
#define mid ((l+r)>>1)
using namespace std;
const int M=2e5+20;
int ans[M],n,k,a[M];
struct Node{
    int l,r;
};
struct List{
    Node list[M];
    int num;
    inline void init(int n){
        for(int i=1;i<=n;++i)
            list[i].l=i-1,list[i].r=i+1;
        list[n].r=-1,num=n;
    }
    inline void del(int x){
        list[list[x].l].r=list[x].r,list[list[x].r].l=list[x].l,--num;
    }
}l;
int v[M*4];
inline void pushup(int id){
    v[id]=max(v[id*2],v[id*2+1]);
}
void build(int id,int l,int r){
    if (l==r){
        v[id]=a[l];
        return;
    }
    build(id*2,l,mid),build(id*2+1,mid+1,r);
    pushup(id);
}
int query(int id,int l,int r){
    if (l==r)
        return l;
    if (v[id*2]>v[id*2+1])
        return query(id*2,l,mid);
    else
        return query(id*2+1,mid+1,r);
}
void operate(int id,int l,int r,int pos,int x){
    if (l==r){
        v[id]=x;
        return;
    }
    if (pos<=mid)
        operate(id*2,l,mid,pos,x);
    else
        operate(id*2+1,mid+1,r,pos,x);
    pushup(id);
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]);
    build(1,1,n);
    int t=2;
    l.init(n);
    while(l.num){
        t=(t==1?2:1);
        int c=query(1,1,n),ca=k;
        while(ca&&l.list[c].l)
            --ca,ans[l.list[c].l]=t,operate(1,1,n,l.list[c].l,0),l.del(l.list[c].l);
        ca=k;
        while(ca&&l.list[c].r!=-1)
            --ca,ans[l.list[c].r]=t,operate(1,1,n,l.list[c].r,0),l.del(l.list[c].r);
        ans[c]=t,operate(1,1,n,c,0),l.del(c);
    }
    for(int i=1;i<=n;++i)
        printf("%d",ans[i]);
    return 0;
}
  1. 1083A:https://codeforces.com/problemset/problem/1083/A

Given a tree, the right value of each point, each side have the right value, a path seeking, and so that the point on the right path - right side and maximum.

Thinking: tree DP, just specify a first root, mxp [i] = j i is expressed in the subtree rooted at, i is an endpoint to the path with the maximum of j. It is clear that mxp [i] can be transferred from all of his son.

After then, considering each node i, i considered as end points of the path of lca. That is the path to the subtree rooted at the maximum value of i. Clearly requires that all sons of the two largest mxp [i] - the right side, after val [i] + mx1 + mx2 is the current value of sub-optimal tree, take all values ​​is the biggest answers.

Time complexity \ (O (m) \) .

#include<bits/stdc++.h>
using namespace std;
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=1e6+20;
#define LL long long
struct Edge{
    int to,w,next;
}edge[M];
int head[M],cnt,fa[M];
inline void add(int u,int v,int w){
    edge[++cnt].to=v,
    edge[cnt].w=w,
    edge[cnt].next=head[u],
    head[u]=cnt;
}
LL mxp[M];
int val[M];
LL dfs1(int now){
    mxp[now]=val[now];
    for(int i=head[now];i;i=edge[i].next){
        if (edge[i].to==fa[now])
            continue;
        fa[edge[i].to]=now,
        mxp[now]=max(mxp[now],val[now]-edge[i].w+dfs1(edge[i].to));
    }
    return mxp[now];
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",&val[i]);
    for(int i=1;i<=n-1;++i){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w),add(u,v,w),add(v,u,w);
    }
    dfs1(1);
    LL ans=0;
    for(int i=1;i<=n;++i){
        LL mx1=0,mx2=0;
        for(int j=head[i];j;j=edge[j].next){
            if (edge[j].to==fa[i])
                continue;
            LL c=mxp[edge[j].to]-edge[j].w;
            if (c>mx1)
                mx2=mx1,mx1=c;
            else if (c>mx2)
                mx2=c;
        }
        ans=max(ans,val[i]+mx1+mx2);
    }
    printf("%lld\n",ans);
    return 0;
}

Graph Theory

Because the former chain need to learn to keep the tree star, so on the way to learn a bit spfa.

  1. P3371: spfa template title

Here before using the chain to the star.

#include<bits/stdc++.h>
using namespace std;
const int M=5e5+20,INF=2147483647;
struct Edge{
    int to,w,next;
}edge[M];
int head[M],cnt;
inline void add(int u,int v,int w){
    edge[++cnt].to=v,
    edge[cnt].w=w,
    edge[cnt].next=head[u],
    head[u]=cnt;
}
int n,dist[M],vis[M];
inline void spfa(int start){
    queue<int> q;
    for(int i=1;i<=n;++i)
        dist[i]=INF,vis[i]=0;
    dist[start]=0,q.push(start),vis[start]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop(),vis[u]=0;
        for(int i=head[u];i;i=edge[i].next){
            int v=edge[i].to;
            if (dist[v]>dist[u]+edge[i].w){
                dist[v]=dist[u]+edge[i].w;
                if (!vis[v])
                    vis[v]=1,q.push(v);
            }
        }
    }
}
int main(){
    int m,s;
    scanf("%d%d%d",&n,&m,&s);
    for(int i=1;i<=m;++i){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w),add(u,v,w);
    }
    spfa(s);
    for(int i=1;i<=n;++i)
        printf("%d ",dist[i]);
    putchar('\n');
    return 0;
}

to sum up

Today, it is first come here, do a lot of questions. Had also looked at LIS, we do not intend to write, dry point down to business.

Guess you like

Origin www.cnblogs.com/diorvh/p/12052220.html