Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <limits>
- #include <map>
- using namespace std;
- // uzumaki naruto
- #define TRACE
- #ifdef TRACE
- #define dbgarr(a,n) cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
- #define dbg(args...) {debug,args; cerr<<endl;}
- #define pause() cin.get();cin.get();
- #else
- #define dbgarr(a,n)
- #define dbg(args...)
- #define pause()
- #endif
- struct debugger {
- template<typename T> debugger& operator , (const T& v) {
- cerr<<v<<" "; return *this;
- }
- } debug;
- template <typename T1, typename T2>
- inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
- return os << "(" << p.first << ", " << p.second << ")";
- }
- template<typename T>
- inline ostream &operator << (ostream & os,const vector<T>& v) {
- bool first = true; os << "[";
- for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
- if(!first) os << ", ";
- os << *ii; first = false;
- }
- return os << "]";
- }
- template <typename T>
- inline void fi(T *a)
- {
- register char c=0;
- while (c<33) c=getchar_unlocked();
- *a=0;
- int tmp = 0;
- while (c>33)
- {
- if ( c == 45 ) tmp = 1;
- else *a=*a*10+c-'0';
- c=getchar_unlocked();
- }
- if ( tmp == 1 ) *a = 0-(*a);
- }
- #define fr first
- #define se second
- #define pb push_back
- typedef long long LL;
- typedef pair<int,int> pii;
- typedef vector<int> vi;
- const int NN = 250005;
- const int SQ = 500;
- int inp[NN], seg[NN];
- int st[SQ+5], en[SQ+5];
- int t[SQ+5][50005];
- pii q[10005];
- void update(int node,int i,int v){
- for(; i < 50005; i += i&-i)
- t[node][i] += v;
- }
- int query(int node,int i){
- int ans = 0;
- for(; i > 0; i -= i&-i)
- ans += t[node][i];
- return ans;
- }
- LL get(int i,int j,int x,int y,int n){
- if (i > j) return 0LL;
- if (x > y) swap(x,y);
- LL ans = 0;
- int ss = seg[i];
- if (i != st[ss] and ss != seg[j]){
- while(i < en[ss] and i <= j){
- if (inp[i] > x and inp[i] < y)
- ++ans;
- ++i;
- }
- ++ss;
- }
- if (i > j) return ans;
- while(ss != seg[j]){
- ans += (LL)query(ss,y-1);
- ans -= (LL)query(ss,x);
- ++ss;
- i = st[ss];
- }
- if (i != st[ss] or j != en[ss]-1){
- while(i <= j){
- if (inp[i] > x and inp[i] < y)
- ++ans;
- ++i;
- }
- } else {
- ans += (LL)query(ss,y-1);
- ans -= (LL)query(ss,x);
- }
- return ans;
- }
- void solve(){
- int n,m,mx = 0;
- fi(&n);
- for(int i = 0; i < n; ++i)
- fi(&inp[i]), mx = max(mx,inp[i]);
- fi(&m);
- for(int i = 0; i < m; ++i)
- fi(&q[i].fr),fi(&q[i].se), --q[i].fr;
- LL inv_cnt = 0;
- int nodes = 0;
- for(int j = 0; j < n; ++nodes){
- st[nodes] = j;
- while(j < st[nodes]+SQ and j < n)
- seg[j++] = nodes;
- en[nodes] = j;
- }
- for(int i = 0; i < n; ++i){
- inv_cnt += (LL)query(nodes,mx) - (LL)query(nodes,inp[i]);
- update(nodes,inp[i],1);
- }
- for(int i = 0; i < nodes; ++i){
- for(int k = st[i]; k < en[i]; ++k)
- update(i,inp[k],1);
- }
- for(int i = 0; i < m; ++i){
- int x = q[i].fr,y = q[i].se;
- int p = inp[x];
- if (y > p){
- inv_cnt -= get(0,x-1,p,y+1,n);
- inv_cnt += get(x+1,n-1,p-1,y,n);
- }
- if (y < p){
- inv_cnt += get(0,x-1,y,p+1,n);
- inv_cnt -= get(x+1,n-1,y-1,p,n);
- }
- if (y != p){
- update(seg[x],y,1);
- update(seg[x],p,-1);
- inp[x] = y;
- }
- printf("%lld\n",inv_cnt);
- }
- }
- int main()
- {
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment