日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

联想杯 - Gentle Jena(单调栈)

發(fā)布時間:2024/4/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 联想杯 - Gentle Jena(单调栈) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Problem G. Gentle Jena Input file: Standard Input Time limit: 2 seconds Output file: Standard Output Memory limit: 512 megabytes ... Why don’t you come to the planetarium? The beautiful twinkling of eternity that will never fade, no matter what. All the stars in the sky are waiting for you... A planetarium, it was once where people went to soothe their hearts by gazing at a starry sky. And Yumemi, a planetarium guide who has been waiting for a visitor for a long time, is looking up at the stars. Stars in the sky can be defined by their brightness, denoted by S = {b1, b2, · · · , bn}. Yumemi found that stars always appear in groups, and she thinks the beauty of the starry night depends on the darkest one, so she defined the beauty value as B(S) = 1l r≤|S| f(l, r) mod 998244353 where f(l, r) = min lir {bi} But the night sky is not always the same. The movement of celestial bodies will make more and more stars in the night sky. The following events will happen in order every second: 1. There are exactly i stars in the sky so S = {b1, b2, · · · , bi}. A star with brightness bi+1 appears, and it will be appended to the end of the sequence S, so it will be S = {b1, b2, · · · , bi+1}. 2. Yumemi records the value (i.e., She calculates the value of B(S)). At the beginning, there is no star in the sky, so S = {} initially. As time goes by, because she is not as good at calculation as before, the task will be given to you. Note that the sequence is given in a modified way. Input To avoid huge input, we use Linear Congruential Method to generate input data, and your solution should be on-line. The first line contains six integers n, p, x, y, z, b1(1 n 107, 2 p 109, 0 x, y, z, b1 < p), indicating the number of the stars and the parameters of the data generator. You need to generate the sequence {b1, b2, · · · , bn}by yourself using the following formula: bi+1 = (x × Ai + y × bi + z) mod p where Ai is the value of B(S) when |S| = i . Page 13 of 272020 “Lenovo Cup” USST Campus Online Invitational Contest May 30, 2020 Output To avoid huge output, you only need to output 1 in Ai , where ”” denotes the bitwise XOR operation (https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Samples Standard Input Standard Output 5 13 2 0 1 3 27 Note In the first second, S = {b1} = {3}, so A1 = f(1, 1) mod 998244353 = 3. It can be inferred that b2 = (2A1 + 1) mod 13 = 7. In the second second, S = {b1, b2} = {3, 7}, so A2 = [f(1, 1) + f(2, 2) + f(1, 2)] mod 998244353 = 13. It can be inferred that b3 = (2A2 + 1) mod 13 = 1. Keep calculating, and you will get A3 = 16, b4 = 7, A4 = 26, b5 = 1, A5 = 31. So you should output A1 A2 A3 A4 A5 = 27. 題目大意:給出一個長度為 1e7 的序列,求出所有長度下的 B( S ) 的異或和,函數(shù) B 在題意中已經(jīng)定義,要求算法強制在線 題目分析:如果直接去考慮區(qū)間可能會有些困難,所以我們不妨可以從最小值出發(fā),計算出每個 b[ i ] 所可以控制的區(qū)間個數(shù),這樣就能不難計算出總貢獻了 如果是這樣想的話,不難想到用單調(diào)棧來維護了,考慮一般情況,當序列 S?在加入元素 b[ i ] 后,新增加了 i 個后綴區(qū)間,分別是 [ 1 , i ] , [ 2 , i ] .... [ i - 1 , i ] , [ i , i?] 這 i 個區(qū)間,利用單調(diào)棧找到當前元素左側(cè)首個小于自己的元素 pos,這樣 [ pos + 1 , i ] , [ pos + 2 , i ] , ... [ i , i ] 這 i - pos 個區(qū)間的最小值顯然就是 b[ i ] 了,現(xiàn)在我們需要快速計算得到 [ 1 , i ] , [ 2 , i ] ... [ pos?, i ] 這 pos 個區(qū)間的貢獻 如果我們想要利用單調(diào)棧去維護左側(cè)首個小于自己元素的位置,就需要維護一個大頂棧,這個大頂棧的特點是,不僅棧內(nèi)元素滿足單調(diào)遞增,同時相應(yīng)位置的下標也滿足單調(diào)遞增(根據(jù)單調(diào)棧的原理顯然可知),那么我們就可以將一直維護的這個大頂棧,想象成?[ 1 , i ] , [ 2 , i ] .... [ i - 1 , i ] , [ i , i?] 這 i 個區(qū)間分別對應(yīng)的最小值,因為對于每個位置 i ,在將非法元素出棧后,最后一步是需要將位置 i 入棧,所以此時整個棧內(nèi)的位置將區(qū)間 [ 1 , i ] 分割成了數(shù)個相連接的區(qū)間,假設(shè)我們可以遍歷棧中的元素,舉個簡單的例子: 假設(shè) st[ 0 ] = 0 , st[ 1 ] = 3 , st[ 2 ] = 4 , st[ 3 ] = 6 ,同時 top = 3 ,此時剛好遍歷完位置 6 ,并且位置 6 已經(jīng)入棧 那么 [ 1 , i ] 的每個位置所代表的的區(qū)間都可以根據(jù)棧內(nèi)元素分類:
  • [ 1 , 6 ] , [ 2 , 6 ] , [ 3 , 6 ] 的最小值為 b[ 3 ]
  • [ 4 , 6 ]? 的最小值為 b[ 4 ]
  • [ 5 , 6 ] , [ 6 , 6 ] 的最小值為 b[ 6 ]
  • 這樣每次維護一個 pre 代表在加入 b[ i ] 后,i 個區(qū)間貢獻之和,對于在加入 b[ i + 1 ] 時,彈棧的過程可以完成實時更新

    因為每個元素至多入棧和出棧一次,所以時間復(fù)雜度是 O( n ) 級別的

    代碼:
    ?

    #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e7+100;const int mod=998244353;LL n,p,x,y,z,b[N];int st[N],top;int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);scanf("%lld%lld%lld%lld%lld%lld",&n,&p,&x,&y,&z,&b[1]);LL _xor=0,ans=0,pre=0;st[0]=0;for(int i=1;i<=n;i++){while(top>=1&&b[st[top]]>=b[i]){pre=((pre-(st[top]-st[top-1])*b[st[top]])%mod+mod)%mod;top--;}int l=st[top];st[++top]=i;pre=(pre+b[i]*(i-l))%mod;ans=(ans+pre)%mod;b[i+1]=(x*ans+y*b[i]+z)%p;_xor^=ans;}printf("%lld\n",_xor);return 0; }

    ?

    總結(jié)

    以上是生活随笔為你收集整理的联想杯 - Gentle Jena(单调栈)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。