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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj2823 线段树模板题 点修改(也可以用单调队列)

發(fā)布時間:2025/3/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj2823 线段树模板题 点修改(也可以用单调队列) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這道題吧 沒計算時間 因為給了那么多 一算還可以

就直接寫了線段樹,劉汝佳那本模板

然后!poj的g++比C++慢大約500ms。。。。。。。g++tle,C++就過了

Sliding Window
Time Limit: 12000MS?Memory Limit: 65536K
Total Submissions: 67576?Accepted: 19163
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1?3?-1?-3?5?3?6?7], and k is 3. Window positionMinimum valueMaximum value
[1??3??-1]?-3??5??3??6??7?-13
?1?[3??-1??-3]?5??3??6??7?-33
?1??3?[-1??-3??5]?3??6??7?-35
?1??3??-1?[-3??5??3]?6??7?-35
?1??3??-1??-3?[5??3??6]?7?36
?1??3??-1??-3??5?[3??6??7]37

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.

Sample Input

8 3 1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3 3 3 5 5 6 7

Source

POJ Monthly--2006.04.28, Ikki

上代碼 通俗易懂哦~~~~~~看我的就行了

#include<iostream> #include<cstdio> #include<cstring>using namespace std;int Max[5000100]; int Min[5000100]; int pinf = 2000000000; int ninf = -2000000000; int n,k;void build(int l,int r,int cur){if(l == r){scanf("%d",&Max[cur]);Min[cur] = Max[cur];return;}int mid = (l + r)/2;build(l,mid,cur*2);build(mid + 1,r,cur*2 + 1);Min[cur] = min(Min[cur*2],Min[cur*2 + 1]);Max[cur] = max(Max[cur*2],Max[cur*2 + 1]);return; }int ql,qr; int queryMax(int l,int r,int cur){if(ql <= l&&r <= qr){return Max[cur];}int mid = (l + r)/2;int ans = ninf;if(ql <= mid){ans = max(ans,queryMax(l,mid,cur*2));}if(qr > mid){ans = max(ans,queryMax(mid + 1,r,cur*2 +1));}return ans; } int queryMin(int l,int r,int cur){if(ql <= l && r <= qr){return Min[cur];}int mid = (l + r)/2;int ans = pinf;if(ql <= mid){ans = min(ans,queryMin(l,mid,cur*2));}if(qr > mid){ans = min(ans,queryMin(mid + 1,r,cur*2 + 1));}return ans; }int main(){while(scanf("%d%d",&n,&k) != EOF){build(1,n,1);ql = 1;qr = k;printf("%d",queryMin(1,n,1));ql++;qr++; while(qr <= n){printf(" %d",queryMin(1,n,1));ql++;qr++;}printf("\n");ql = 1;qr = k;printf("%d",queryMax(1,n,1));ql++;qr++; while(qr <= n){printf(" %d",queryMax(1,n,1));ql++;qr++;}printf("\n");}return 0; }

?

轉載于:https://www.cnblogs.com/xuyanqd/p/9073920.html

總結

以上是生活随笔為你收集整理的poj2823 线段树模板题 点修改(也可以用单调队列)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。