Record of coding:Codeforces 1093E

E. Intersection of Permutations

You are given two permutations a and b, both consisting of n elements. Permutation of nn elements is such a integer sequence that each value from 1 to n appears exactly once in it.

You are asked to perform two types of queries with them:

  • 1 la ra lb rb — calculate the number of values which appear in both segment [la;ra] of positions in permutation aa and segment [lb;rb] of positions in permutation b;
  • 2 x y — swap values on positions x and y in permutation b.

Print the answer for each query of the first type.

It is guaranteed that there will be at least one query of the first type in the input.

https://codeforces.com/contest/1093/problem/E

Obviously we can use renumeration to make A fixed, and the answer won‘t be changed.

Firstly,let‘s consider a weak solution.If we use a 2D-BIT to solve this problem, we find the answer is just the sum on rectangle.

To explain this more intuitively,I use f[i][j] to stand for the sum of number less than or equals to j among the first i numbers of the permutation B.

Then answer for la,ra,lb and rb equals to  f[ra][rb]-f[la-1][rb]-f[ra][lb-1]+f[la-1][lb-1].This is a simple usage of the principle of inclusion-exclusion.

In this weak solution, the complexity of time is O((m+n)(logn)^2), but the complexity of memory come up to O(N^2). Considered of the memory limit of 512MB , this solution is completely unacceptable.

So we must compress the usage of memory.

My solution is to change each node of the BIT from a BIT into a rb_tree, which consists of all the number fell into this node.

This action doesn‘t change the complexity of time but reduce the memory to O(NlogN).

So this problem is solved.

Code:

/**************************************************************************************
 * This code is written by akonoh.                                                    *
 * It can only be compiled by c++17 due to the usage of bits/extc++.h                 *
 * I used pb_ds to reduce the length of my code                                       *
 * ************************************************************************************/
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define N 200*1000
int n,m;
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> tr[N+7],sb;
inline int lb(int x){return x&-x;}
void addup(int x,int y)
{
    for(int i = x; i <= n ; i += lb(i))
    {
        tr[i].insert(y);
    }
}
void del(int x, int y)
{
    for(int i = x; i <= n; i += lb(i))
    {
        tr[i].erase(y);
    }
}
int getans(int x, int y)
{
    int ans = 0;
    for(int i = x; i > 0; i -= lb(i))
    {
        int p = tr[i].order_of_key(y);
        if(*tr[i].lower_bound(y)==y)p++;
        ans += p;
        //this part is to find how many numbers are smaller or equal to y.
    }
    return ans;
}
int a[N+8],b[N+8];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1 ; i<= n; i ++)
    {
        int x;
        scanf("%d",&x);
        a[x]=i;
    }
    for(int i = 1; i <= n ;i ++)
    {
        int x;
        scanf("%d",&x);
        b[i]=a[x];//renumeration
    }
    for(int i = 1; i <= n; i ++)
    addup(i,b[i]);
    for(int i = 1; i <= m; i ++)
    {
        int t;
        scanf("%d",&t);
        if(t==1)
        {
            int la,ra,lb,rb,ans=0;
            scanf("%d%d%d%d",&la,&ra,&lb,&rb);
            ans +=getans(rb,ra);
            ans -=getans(lb-1,ra);
            ans -=getans(rb,la-1);
            ans +=getans(lb-1,la-1);
            printf("%d\n",ans);
        }
        else
        {
            int x,y;
            scanf("%d%d",&x,&y);
            del(x,b[x]);
            del(y,b[y]);
            swap(b[x],b[y]);
            addup(x,b[x]);
            addup(y,b[y]);
        }
    }
}

原文地址:https://www.cnblogs.com/akonoh/p/10233989.html

时间: 2024-10-18 23:33:42

Record of coding:Codeforces 1093E的相关文章

the first blog: record my coding

try the code "hello world" 1 #include<iostream> 2 using namespace std; 3 int main() { 4 cout << "hello world" << endl; 5 return 0; 6 } succeed!

[树状数组][权值线段树] Codeforces 1093E Intersection of Permutations

题目描述 You are given two permutations aa and bb , both consisting of nn elements. Permutation of nn elements is such a integer sequence that each value from 11 to nn appears exactly once in it. You are asked to perform two types of queries with them: 1

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Google&#39;s C++ coding style

v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成     头文件        #define用法        前向声明        内联函数        -inl.h文件        函数参数顺序        include的命名和顺序    作用域        命名空间            未命名空间            命名空间       

Codeforces Round 239 Div 1

都怪自己太懒了 这段时间比赛参加了大部分,但是一直都没写题解,趁这几天没事,计划把这段时间的题解都补上. 上一次比赛(248)终于升到了div1,所以从这次开始就开始写div1了. A. Triangle There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such

《Cracking the Coding Interview》——第17章:普通题——题目9

2014-04-28 23:52 题目:设计算法,找出一本书中某个单词的出现频率. 解法:数就行了. 代码: 1 // 17.9 Given a book, find out the occurrences of any given words in it. 2 // Answer: 3 // 1. process the book as a text file. 4 // 2. find all words, definition of a word must be clearly asser

《Cracking the Coding Interview》——第16章:线程与锁——题目4

2014-04-27 20:06 题目:设计一个类,只有在不产生死锁的时候才分配资源. 解法:不太清楚这个题是要分配何种资源,以何种形式?所以没能动手写个可运行的代码,只是闲扯了几句理论分析. 代码: 1 // 16.4 Design a class which provides a lock only if no deadlock would take place. 2 1. The class will maintain a list of all locks that would be r

PLSQL Coding Standard

Naming and Coding Standards for SQL and PL/SQL "The nice thing about standards is that you have so many to choose from." - Andrew S Tanenbaum Introduction This document is mentioned in a discussion on the OTN forums. One of the first comments be

【codeforces #283(div 1)】ABC题解

A. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an n?×?m rectangular table consisting of lower case English letters. In one operation you can completely r