1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <map> #include <set> #define MAXN 200005 #define MAXM #define INF 0x3f3f3f3f #define rint register int #define LL long long #define LD long double using namespace std;
int n, m, cnt, a[MAXN], b[MAXN], pos[MAXN], t[MAXN], ans[MAXN];
struct Act { int id, x, y, val; Act(int a=0, int b=0, int c=0, int d=0) { id=a; x=b; y=c; val=d; } } act[MAXN*5], temp[MAXN*5];
bool CMP(Act x, Act y) { return x.x<y.x; }
void add(int x, int y) { for(; x<=200001; x+=(x&-x)) t[x]+=y; }
int ask(int x) { int sum=0; for(; x; x-=(x&-x)) sum+=t[x]; return sum; }
void cdq(int l, int r) { if(l==r) return; int mid=(l+r)>>1; cdq(l, mid); cdq(mid+1, r); int L=l, R=mid+1; for(; R<=r; ++R) { if(!act[R].id) continue; while(act[L].x<=act[R].x && L<=mid) { if(!act[L].id) add(act[L].y, act[L].val); L++; } ans[act[R].id]+=act[R].val*ask(act[R].y); } for(int i=l; i<L; i++) if(!act[i].id) add(act[i].y, -act[i].val); merge(act+l, act+mid+1, act+mid+1, act+r+1, temp+l, CMP); for(int i=l; i<=r; i++) act[i]=temp[i]; }
int main() { memset(ans, -1, sizeof(ans)); scanf("%d%d", &n, &m); for(rint i=1; i<=n; ++i) { scanf("%d", &a[i]); pos[a[i]]=i; } for(rint i=1; i<=n; ++i) { scanf("%d", &b[i]); act[++cnt]=Act(0, i+1, pos[b[i]]+1, 1); } for(rint i=1; i<=m; ++i) { int op, la, ra, lb, rb, x, y; scanf("%d", &op); if(op==1) { ans[i]=0; scanf("%d%d%d%d", &la, &ra, &lb, &rb); act[++cnt]=Act(i, lb, la, 1); act[++cnt]=Act(i, lb, ra+1, -1); act[++cnt]=Act(i, rb+1, la, -1); act[++cnt]=Act(i, rb+1, ra+1, 1); } else { scanf("%d%d", &x, &y); act[++cnt]=Act(0, x+1, pos[b[x]]+1, -1); act[++cnt]=Act(0, y+1, pos[b[y]]+1, -1); swap(b[x], b[y]); act[++cnt]=Act(0, x+1, pos[b[x]]+1, 1); act[++cnt]=Act(0, y+1, pos[b[y]]+1, 1); } } cdq(1, cnt); for(int i=1; i<=m; i++) if(ans[i]!=-1) printf("%d\n", ans[i]); }
|