[Diary] 1.13

1.13

Number of columns of block

1. The number of columns of the block 1: Single Point Query Interval subtraction +

Ideas: Corner of violence, the entire overall mark with addition and subtraction lazy.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=5e4+20,P=1e9+7;
int bl_size,blnum[M],v[M],lazy[M];
void bl_init(int n){
    bl_size=sqrt(n);
    for(int i=1;i<=n;++i)
        blnum[i]=(i-1)/bl_size+1;
}
void bl_add(int l,int r,int c){
    //先加左边多余块
    for(int i=l;i<=min(blnum[l]*bl_size,r);++i)//l到本块最右边和r最小值
        v[i]+=c;
    //再加右边多余块
    if (blnum[l]!=blnum[r])
        for(int i=(blnum[r]-1)*bl_size+1;i<=r;++i)//r所在块第一个数到r
            v[i]+=c;
    //最后处理整块
    for(int i=blnum[l]+1;i<=blnum[r]-1;++i)
        lazy[i]+=c;
}
struct TTTT{
    int n,a[M];
    void init(){
        scanf("%d",&n);
        bl_init(n);
        for(int i=1;i<=n;++i)
            scanf("%d",&v[i]);
    }
    void run(){
        init();
        for(int i=1;i<=n;++i){
            int op,l,r,c;
            scanf("%d%d%d%d",&op,&l,&r,&c);
            if (op==0)
                bl_add(l,r,c);
            else
                printf("%d\n",v[r]+lazy[blnum[r]]);
        }
    }
}TTT;
int main(){
    TTT.run();
    return 0;
}

2. The number of columns in Block 2: + subtraction section interrogation number of elements less than x

Here inquiry need to ensure that each block are ordered, which can directly find binary number of elements less than x. Therefore, addition and subtraction when corners need for violence After modifying, sort the original array. Apparently the result of the sort of things need to open a new store. An integral part of the direct subtraction can be lazy.

For queries, the corner is still violence statistics, entire words, for each block lowerbound query about less than x number of elements in the interior, and finally add up.

Time complexity is \ (O (n \ log n \ sqrt {n \ log n}) \)

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
#define db(x) cout<<#x<<":"<<x<<endl;
#define lf(x) ((x)-1)*bl_size+1
#define rt(x,n) min((x)*bl_size,(n))
const int M=2e5+20,P=1e9+7;
int n,bl_size,blnum[M],lazy[M],v[M];
vector<int> blk[M];
inline void reset(int x){
    blk[x].clear();
    for(int i=lf(x);i<=rt(x,n);++i)
        blk[x].push_back(v[i]);
    sort(blk[x].begin(),blk[x].end());
}
inline void bl_init(int n){
    bl_size=sqrt(n/log(n));
    for(int i=1;i<=n;++i)
        blnum[i]=(i-1)/bl_size+1,blk[blnum[i]].push_back(v[i]);
    for(int i=blnum[1];i<=blnum[n];++i)
        sort(blk[i].begin(),blk[i].end());
}
inline void bl_add(int l,int r,int c){
    for(int i=l;i<=rt(blnum[l],r);++i)
        v[i]+=c;
    reset(blnum[l]);
    if (blnum[l]!=blnum[r]){
        for(int i=lf(blnum[r]);i<=r;++i)
            v[i]+=c;
        reset(blnum[r]);
    }
    for(int i=blnum[l]+1;i<=blnum[r]-1;++i)
        lazy[i]+=c;
}
inline int query(int l,int r,int c){
    int ans=0;
    for(int i=l;i<=rt(blnum[l],r);++i)
        if (v[i]+lazy[blnum[l]]<c)
            ++ans;
    if (blnum[l]!=blnum[r])
        for(int i=lf(blnum[r]);i<=r;++i)
            if (v[i]+lazy[blnum[r]]<c)
                ++ans;
    for(int i=blnum[l]+1;i<=blnum[r]-1;++i)
        ans+=lower_bound(blk[i].begin(),blk[i].end(),c-lazy[i])-blk[i].begin();
    return ans;
}
struct TTTT{
    void init(){
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",&v[i]);
        bl_init(n);
    }
    void run(){
        init();
        for(int i=1;i<=n;++i){
            int op,l,r,c;
            scanf("%d%d%d%d",&op,&l,&r,&c);
            if(op==0)
                bl_add(l,r,c);
            else
                printf("%d\n",query(l,r,c*c));
        }
    }
}TTT;
int main(){
    TTT.run();
    return 0;
}

Wannafly Day 2

A. Tommy string

Tommy has a string, he often play out. This day in English class, he learned the vowels A , E , i , O , U and semi-vowels the y- . "These letters are very important!" Tommy thought, "Well, if I take a random substring, which accounted vowel expect there will be?"

Thus, you obtain the string for Tommy, a randomly selected sub-string, a vowel ( A , E , I , O , U , Y ) representing a desired letter substring length ratio is.

Input formats:

Reading a length of not more than 106 string contains only lowercase letters, i.e., Tommy string.

Output Format

Output demand expectations, requires relative (absolute) error is less than 10-6.

Sample input:

legilimens

Sample output:

0.446746032

Ideas: statistical contribution of each vowel. If the vowel position i, and k = min (i + 1, len-i), it contains the length of the string is the number of 1-k 1-k is a contribution to \ (1 * 1 * 2 + 1/2 * 3 + 1/3 + ...... \) , the sum is k. Of length k + 1-len-k + 1 is the number of strings of k, contribution \ ([1 / (k + 1) + ... + 1 / (len-k + 1)] * K \) , fractional harmonic series may be pretreated. The last part is the number of k-1-1, respectively, but may be pretreated, time complexity \ (O (n-) \) .

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=1e6+20,P=1e9+7;
struct TTTT{
    int n,len;
    char s[M];
    double pre[M],pre2[M];
    void init(){
        scanf("%s",s);
        len=strlen(s);
        for(int i=1;i<=len;++i)
            pre[i]=pre[i-1]+1.0/i,pre2[i]=pre2[i-1]+1.0*i/(len-i+1);
    }
    void run(){
        init();
        double sum=0,ans=0;
        for(int i=1;i<=len;++i)
            sum+=i;
        for(int i=0;i<len;++i)
            if (s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='y'){
                int mn=min(len-i,i+1),mx=max(len-i,i+1);
                ans+=mn+(pre[mx]-pre[mn])*mn+pre2[mn-1];
            }
        printf("%.16lf\n",ans/sum);
    }
}TTT;
int main(){
    TTT.run();
    return 0;
}

B. equation Saab

Saab has a formula:

x1xorx2xorx3xor...xorxn=k

Wherein X O R & lt refer Bitwise exclusive OR symbol.

Saab while also limiting the range of each unknown 0 ≦ X I m I , I hope you count the number of the solution, the final answer to the outputs 109 + 7 mod.

Input formats:

This problem has a plurality of sets of data, to the end of the file processing, the number of data sets to ensure that no more than 100.

For each set of test data:

The first row is read two positive integers n- , K ( n- ≦ 50, K <231);

The second row is read n non-negative integer m . 1, m 2, m . 3, ..., m n ( m I <231).

Output Format

For each test, a number of outputs, to ask the subject.

Sample input:

7 127
64 32 16 8 4 2 1
6 127
64 32 16 8 4 2
4 5
1 2 3 4

      
    

Sample output:

1
0
6

Ideas: Digital DP.

C. satisfied that the new hundred stones game

D. Karabash string

Karabash is a string masters, the day of his idle boredom, he made a string of problems.
Given a string of length N S, i is the suffix of the suffix is defined starting from the i-th position, i.e. sisi + 1 ... sn, defined LCP (i, j) after
conjugation suffixes i and j longest common prefix.
Karabash want to know, every time he is given a set of i, j, can you quickly tell him lcp (i, j).
Karabash friend gourd string master, he thought that this title is too boring, so he wanted another question, suppose you have a
collection {lcp (i, j) | 1 ≤ i <j ≤ N}, he wanted MEX know the value of the collection is. MEX set a minimum value is not
non-negative integer appears in the collection.
This question Karabash too easy, so the gourd would like to know, for each prefix string, a set of corresponding
MEX value is.
Input
first line an integer T (. 1 ≤ T ≤ 105
), represents the number of data sets.
For each test, a total line, represents a string S (. 1 ≤ | S | ≤ 106
).
To ensure that all input data string length does not exceed 5 * 106. To ensure that the string contains only lowercase letters.
Output
For each test, the output | S | integer representing the value of each MEX corresponding prefix.
Example
Standard Standard Output INPUT
2
Ababa
BAA
0. 4. 3. 1 2
0 2. 1

Ideas: This title is really thinking subject, really hard to figure, but the code is well written.

  1. Need special sentenced zero. If you enter the letters are all the same, then output 0.
  2. Otherwise, the set of elements which is continuous from 0-mex-1. In fact seeking is a collection of maximum +1.
  3. Consider the case transferred from i to i + 1's, lcp increase of those, certainly the entire suffix are lcp, that is, the entire suffix is ​​another prefix. In this case, the entire suffix must be endpos> (the last time in other occurrence in the corresponding position of the other string lcp) 1 a. Therefore, after the biggest increase lcp length maxlen (fa (last)). This is because the increase did not necessarily belong to the last node.
  4. Then each test to see if maxlen (fa (last)) mex greater than the current, if it is to mex = that. And you can only find some mex +1 each time. Specifically to see the solution to a problem cyy it.
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=1e6+20;
char s[M],s2[M];
struct SAM{
    int last,cnt,ch[M<<1][26],fa[M<<1],len[M<<1];
    int mex;
    void ins(int c){
        int p=last,np=++cnt;
        last=np,len[np]=len[p]+1;
        for(;p&&!ch[p][c];p=fa[p])
            ch[p][c]=np;
        if(!p)
            fa[np]=1;
        else{
            int q=ch[p][c];
            if(len[p]+1==len[q])
                fa[np]=q;
            else{
                int nq=++cnt;
                len[nq]=len[p]+1;
                memcpy(ch[nq],ch[q],sizeof(ch[q]));
                fa[nq]=fa[q],fa[q]=fa[np]=nq;
                for(;ch[p][c]==q;p=fa[p])
                    ch[p][c]=nq;
            }
        }
    }
    void build(){
        scanf("%s",s+1);
        int lenn=strlen(s+1);
        last=cnt=1;
        printf("0"),ins(s[1]-'a');
        int p=2;
        while(p<=lenn&&s[p]==s[1])
            ins(s[p]-'a'),printf(" 0"),++p;
        if (p>lenn){
            putchar('\n');
            for(int i=1;i<=cnt;++i)
                fa[i]=len[i]=0,memset(ch[i],0,sizeof(ch[i]));
            return;
        }
        mex=p-2;
        for(int i=p;i<=lenn;i++){
            ins(s[i]-'a');
            if(len[fa[last]]>mex)
                ++mex;
            printf(" %d",mex+1);
        }
        putchar('\n');
        for(int i=1;i<=cnt;++i)
            fa[i]=len[i]=0,memset(ch[i],0,sizeof(ch[i]));
    }
}sam;
int main(){
    int T;
    scanf("%d",&T);
    for(int i=1;i<=T;++i)
        sam.build();
    return 0;
}

K. break Intuit head of the anonymous letter

Thinking: generally provides the conditions and time does not exceed a certain value, there will be a nature different lengths up to sqrt (n) number.

This question is in fact a dp on AC automaton. AC built for all modes string automaton, after a string of text directly take up the race on a node went to jump all of its match (explained later). This is because all current must match a prefix of suffix text string, can be transferred from the front, since different lengths of up to sqrt (n) number, a node for the same depth of only one match, the match is a jumping must not exceed the number of sqrt (n) th, thus demonstrating the time complexity.

Note that if you fail to jump naked tree, will T, because the trees are not always fail termination node, this time on the need to optimize the use match, match-up indicates that the current node and all fail the tree his father, the most by termination node under. Jump match, it is the trie [trie [cur] .fail] .match to jump. For details, see code. Processing match and fail processing are performed simultaneously.

dp [i] represents the text string 1-i minimal cost of synthesis, when the first went i + 1 points, jump all match. Terminating node can have a dp [i + 1] = min (dp [i + 1], dp [i + 1-depth] + expense) to.

Reflection:

  1. String title do too little, more recently, there is no review, no. very sad.
  2. AC automaton change it to the board, the board is too abstract ZBH, easy to forget initialization trie [0] .fail = -1.
#include <bits/stdc++.h>
using namespace std;
#define LETTER 26
#define LL long long
const int M=5e5+90;
struct Trie{
    int v,fail,depth,match,next[LETTER];
}pool[M];
Trie* const trie=pool+1;
int cnt;
char str[M],pstr[M];
int insert(char *s,int pri){
    int cur=0;
    for (int i=0;s[i];++i){
        int &pos=trie[cur].next[s[i]-'a'];
        if (!pos)
            pos=++cnt;
        cur=pos,trie[cur].depth=i+1;
    }
    if (trie[cur].v==0||trie[cur].v>pri)
        trie[cur].v=pri;
    return cur;
}
void build(){
    queue <int>q;q.push(0);
    while(!q.empty()){
        int t=q.front();q.pop();
        for(int i=0;i<LETTER;i++){
            int &cur=trie[t].next[i];
            if(cur){
                q.push(cur),
                trie[cur].fail=trie[trie[t].fail].next[i],
                trie[cur].match=trie[cur].v?cur:trie[trie[cur].fail].match;
            }
            else
                cur=trie[trie[t].fail].next[i];
        }
    }
}
LL dp[M];
void search(char *s){
    int cur=0;
    for(int i=0;s[i];++i){
        cur=trie[cur].next[s[i]-'a'];
        for(int j=trie[cur].match;j;j=trie[trie[j].fail].match)
            dp[i+1]=min(dp[i+1],dp[i+1-trie[j].depth]+trie[j].v);
    }
}
int main(){
    int n;
    trie[0].fail=-1;
    scanf("%d",&n);
    for (int i=1;i<=n;++i){
        int c;
        scanf("%s%d",pstr,&c),insert(pstr,c);
    }
    build();
    scanf("%s",str);
    int len=strlen(str);
    for(int i=1;i<=len;++i)
        dp[i]=1e15;
    search(str);
    if (dp[len]==1e15)
        printf("-1\n");
    else
        printf("%lld\n",dp[len]);
    return 0;
}

F. mushroom carat Liz

Back to fix

Guess you like

Origin www.cnblogs.com/diorvh/p/12190114.html
Recommended