BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树
题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1012
题意:
题解:
不用printf输出就RE…
代码:
代码一: 单调队列
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){ 10 ll x=0,f=1;char ch=getchar(); 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 ////////////////////////////////////////////////////////////////////////// 16 const int maxn = 250001; 17 18 int a[maxn],maxnum[maxn]; 19 20 int main(){ 21 int m,d,k=0; 22 int t = 0; 23 scanf("%d%d",&m,&d); 24 for(int i=0; i<m; i++){ 25 char op[2]; scanf("%s",op); 26 if(op[0] == 'A'){ 27 int x; scanf("%d",&x); 28 a[++t] = (x+k)%d; 29 for(int i=t; i>0; i--){ 30 if(maxnum[i] < a[t]){ 31 maxnum[i] = a[t]; 32 }else{ 33 break; 34 } 35 } 36 }else{ 37 int x; scanf("%d",&x); 38 k = maxnum[t-x+1]; 39 printf("%d\n",k); 40 } 41 } 42 43 return 0; 44 }
代码二:线段树
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){ 10 ll x=0,f=1;char ch=getchar(); 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 ////////////////////////////////////////////////////////////////////////// 16 const int maxn = 2e5+10; 17 18 int n;ll d; 19 struct node{ 20 int l,r; 21 ll mx; 22 void update(ll val){ 23 mx = max(mx,val); 24 } 25 }tree[maxn*4]; 26 27 void pushup(int rt){ 28 tree[rt].mx = max(tree[rt<<1].mx,tree[rt<<1|1].mx); 29 } 30 31 void build(int rt,int l,int r){ 32 tree[rt].l=l,tree[rt].r = r; 33 tree[rt].mx = 0; 34 if(l == r) 35 return ; 36 int mid = (l+r)/2; 37 build(rt<<1,l,mid); 38 build(rt<<1|1,mid+1,r); 39 pushup(rt); 40 } 41 42 void update(int rt,int pos,ll val){ 43 int L = tree[rt].l, R = tree[rt].r; 44 if(L==R && L==pos){ 45 tree[rt].mx = val; 46 return ; 47 } 48 int mid = (L+R)/2; 49 if(pos<=mid) update(rt<<1,pos,val); 50 if(pos>mid) update(rt<<1|1,pos,val); 51 pushup(rt); 52 } 53 54 ll query(int rt,int l,int r){ 55 int L = tree[rt].l, R = tree[rt].r; 56 if(l<=L && R<=r) 57 return tree[rt].mx; 58 ll ans = 0; 59 int mid = (L+R)/2; 60 if(l<=mid) ans = max(ans,query(rt<<1,l,r)); 61 if(r>mid) ans = max(ans,query(rt<<1|1,l,r)); 62 return ans; 63 } 64 65 int main(){ 66 scanf("%d%lld",&n,&d); 67 build(1,1,n); 68 ll ans = 0,v; 69 int cnt = 0; 70 for(int i=0; i<n; i++){ 71 char op; 72 scanf(" %c%lld",&op,&v); 73 if(op == 'A'){ 74 v = (ans+v)%d; 75 ++cnt; 76 update(1,cnt,v); 77 }else{ 78 ans = query(1,cnt-v+1,cnt); 79 printf("%lld\n",ans); 80 } 81 } 82 83 return 0; 84 }