AhmedAshraff

Untitled

May 22nd, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  3. #define ll long long
  4. #define sz(s) (int)(s).size()
  5. #define all(s) (s).begin(),(s).end()
  6. using namespace std;
  7. void File();
  8. void sol();
  9. int main() {
  10.     boAshraf
  11. //    File();
  12.     freopen("army.in", "r", stdin);
  13.     int t = 1;
  14.     cin >> t;
  15.     while (t--) {
  16.         sol();
  17.     }
  18.     return 0;
  19. }
  20.  
  21. void sol() {
  22.     int n,m;
  23.     cin>>n>>m;
  24.     ll tot=1ll*(n)*(n-1)/2;
  25.     ll All=1ll*(n)*(n+1)/2;
  26.     unordered_map<ll,ll>changed;
  27.     auto calc=[&](ll x)->ll{
  28.         ll val=(ll)((-1 + sqrt(1 + 8 * x)) / 2);;
  29.         if(val*(val+1)/2 !=x)val++;
  30.         return val;
  31.     };
  32.     auto equal=[&](ll a, ll b)->bool{
  33.         ll val1=changed.count(a)?changed[a]:calc(a);
  34.         ll val2=changed.count(b)?changed[b]:calc(b);
  35.         return val1==val2;
  36.     };
  37.     auto del=[&](ll x){
  38.         ll before=x-1,after=x+1;
  39.         if(before!=0 && equal(before, x))tot--;
  40.         if(after!=All+1 && equal(x, after))tot--;
  41.     };
  42.     auto add=[&](ll x)->void{
  43.         ll before=x-1,after=x+1;
  44.         if(before!=0 && equal(before, x))tot++;
  45.         if(after!=All+1&& equal(x, after))tot++;
  46.     };
  47.     auto Swap=[&](ll a,ll b){
  48.         ll val1=changed.count(a)?changed[a]:calc(a);
  49.         ll val2=changed.count(b)?changed[b]:calc(b);
  50.         changed[b]=val1;
  51.         changed[a]=val2;
  52.     };
  53.     while(m--){
  54.         ll a,b;
  55.         cin>>a>>b;
  56.         if(equal(a, b))continue;
  57.         del(a);
  58.         del(b);
  59.         Swap(a,b);
  60.         add(a);
  61.         add(b);
  62.     }
  63.     cout<<tot<<'\n';
  64.  
  65. }
  66.  
  67. void File() {
  68. #ifndef ONLINE_JUDGE
  69.     freopen("input.txt", "r", stdin);
  70.     freopen("output.txt", "w", stdout);
  71. #endif
  72. }
Advertisement
Add Comment
Please, Sign In to add comment