个人记录成长历程
M201710071229
(题目来自网络)
D. Yet AnotherArray Queries Problem
time limit pertest
2 seconds
memory limit pertest
256 megabytes
input
standard input
output
standard output
You are given anarray a of size n, and q queries toit. There are queries of two types:
· 1 li ri — performa cyclic shift of the segment [li, ri] to theright. That is, for every x suchthat li ≤ x < ri new valueof ax + 1 becomesequal to old value of ax, and new valueof ali becomes equal to old value of ari;
· 2 li ri — reversethe segment [li, ri].
There are m importantindices in the array b1, b2, ..., bm. For each i suchthat 1 ≤ i ≤ m you have to output the number thatwill have index bi in thearray after all queries are performed.
Input
The first linecontains three integer numbers n, q and m (1 ≤ n, q ≤ 2·105, 1 ≤ m ≤ 100).
The second line contains n integernumbers a1, a2, ..., an (1 ≤ ai ≤ 109).
Then q linesfollow. i-th of them contains three integernumbers ti, li, ri, where ti is thetype of i-th query, and [li, ri] is thesegment where this query is performed (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
The last linecontains m integer numbers b1, b2, ..., bm (1 ≤ bi ≤ n) — importantindices of the array.
Output
Print m numbers, i-th of which isequal to the number at index bi after allqueries are done.
Example
input
6 3 51 2 3 4 5 62 1 32 3 61 1 62 2 1 5 3
output
3 3 1 5 2
#include <stdlib.h> #include <stdio.h> struct stu { int num; struct stu *next; }; // struct stu *creatLinkList(int n) {//创建及输入 struct stu *h; h=(struct stu*)malloc(sizeof(struct stu)); h->next=NULL; h->num=n+1;//记录长度 struct stu *newp, *tp; for(int i=1; i<=n; i++) { newp=(struct stu*)malloc(sizeof(struct stu)); newp->next=NULL; //加入至末尾 scanf("%d",&(newp->num));//手动输入任意值 // newp->num=i;//快速输入一些值 (测试用) tp=h; while(tp->next!=NULL)tp=tp->next; //找到原末尾 tp->next=newp; } return h; } // struct stu *plook(struct stu *h, int m) {//查找(除头第m个) if( m<=(h->num-1) ) { struct stu *tp; tp=(struct stu *)malloc(sizeof(struct stu)); tp=h; for(int j=1; j<=m; j++)tp=tp->next; return tp; } } // void Cmove_R_1(struct stu *h, int l, int r) {//部分向右循环移动 if(r>l) { struct stu *tpr; tpr=plook(h, r); plook(h, r-1)->next=tpr->next; tpr->next=plook(h, l); plook(h, l-1)->next=tpr; } } // void Cmove_L_1(struct stu *h, int l, int r) {//部分向左循环移动 if(r>l) { struct stu *tpl, *tp; tp=plook(h, r+1); tpl=plook(h, l); plook(h, r)->next=tpl; plook(h, l-1)->next=tpl->next; tpl->next=tp; } } // void exchange(struct stu *h, int l, int r) {//之间左右对称地交换 int tl=l, tr=r; while(tr-tl>=1) { Cmove_L_1(h, tl, tr); tr--; Cmove_R_1(h, tl, tr); tl++; } } // void delLinkList(struct stu *h) {//销毁链表 struct stu *tp,*tp2; /* tp=h; while(tp!=NULL)//?这样无法判断是否是NULL { tp2=tp->next; free(tp); tp=tp2; } */ tp=h; free(h); for(int i=1; i<=h->num-1; i++) { tp2=tp->next; free(tp); tp=tp2; } } // void outputnumLinkList(struct stu *h) {//顺序输出链表中的全部值 测试用 struct stu *tp; /* tp=h->next; while(tp!=NULL)//?这样无法判断是否是NULL { printf("%d ",tp->num);//顺序输出各个数 测试用 tp=tp->next; } */ tp=h; for(int i=1; i<=h->num-1; i++) { tp=tp->next; printf("%d ",tp->num);//顺序输出各个数 测试用 } printf("\nAll:%d\n",h->num-1);//输出总个数 测试用 } // // int main() {//D.Yet Another Array queries Problem struct stu *H; int N,q,m; scanf("%d%d%d", &N, &q, &m); H=creatLinkList(N);//创建及输入 int t,l,r; for(int z=1; z<=q; z++) { scanf("%d%d%d", &t, &l, &r);//待检测输入是否合理 if(t==1)Cmove_R_1(H, l, r); else if(t==2)exchange(H, l, r); // outputnumLinkList(H);//顺序输出链表中的全部值 测试用 } int b; for(int z=1; z<=m; z++) { scanf("%d", &b);//待检测输入是否合理 printf("%d ",plook(H, b)->num); } delLinkList(H);//销毁链表 return 0; }
D.Yet Another Array queries Problem
最新推荐文章于 2022-11-09 10:54:03 发布