• hdu 4638 Group 莫队算法


    题目链接

    很裸的莫队, 就不多说了...

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define pb(x) push_back(x)
     4 #define ll long long
     5 #define mk(x, y) make_pair(x, y)
     6 #define lson l, m, rt<<1
     7 #define mem(a) memset(a, 0, sizeof(a))
     8 #define rson m+1, r, rt<<1|1
     9 #define mem1(a) memset(a, -1, sizeof(a))
    10 #define mem2(a) memset(a, 0x3f, sizeof(a))
    11 #define rep(i, a, n) for(int i = a; i<n; i++)
    12 #define ull unsigned long long
    13 typedef pair<int, int> pll;
    14 const double PI = acos(-1.0);
    15 const double eps = 1e-8;
    16 const int mod = 1e9+7;
    17 const int inf = 1061109567;
    18 const int maxn = 1e5+5;
    19 int vis[maxn];
    20 int a[maxn], ans[maxn];
    21 
    22 struct node {
    23     int l, r, block, id;
    24     bool operator < (node x) const
    25     {
    26         if(block == x.block)
    27             return r < x.r;
    28         return block < x.block;
    29     }
    30 } q[maxn];
    31 int main() {
    32     int t, n, m;
    33     cin>>t;
    34     while (t--) {
    35         scanf("%d%d", &n, &m);
    36         int BLOCK = sqrt(n*1.0);
    37         for(int i=1; i<=n; i++)
    38             scanf("%d", &a[i]);
    39         for(int i=0; i<m; i++) {
    40             scanf("%d%d", &q[i].l, &q[i].r);
    41             q[i].block = q[i].l/BLOCK;
    42             q[i].id = i;
    43         }
    44         sort(q, q+m);
    45         mem(vis);
    46         int tmp = 0;
    47         int x = q[0].l, y = q[0].r;
    48         for(int i=x; i<=y; i++) {
    49             vis[a[i]] = 1;
    50             if (vis[a[i]-1] && vis[a[i]+1])
    51                 tmp--;
    52             else if (!vis[a[i]-1] && !vis[a[i]+1])
    53                 tmp++;
    54         }
    55         ans[q[0].id] = tmp;
    56         int L = x, R = y;
    57         for(int i=1; i<m; i++) {
    58             x = q[i].l, y = q[i].r;
    59             for (int i=x; i<L; i++) {
    60                 vis[a[i]] = 1;
    61                 if (vis[a[i]-1] && vis[a[i]+1])
    62                     tmp--;
    63                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    64                     tmp++;
    65             }
    66             for (int i=R+1; i<=y; i++) {
    67                 vis[a[i]] = 1;
    68                 if (vis[a[i]-1] && vis[a[i]+1])
    69                     tmp--;
    70                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    71                     tmp++;
    72             }
    73             for (int i=L; i<x; i++) {
    74                 vis[a[i]] = 0;
    75                 if (vis[a[i]-1] && vis[a[i]+1])
    76                     tmp++;
    77                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    78                     tmp--;
    79             }
    80             for (int i=y+1; i<=R; i++) {
    81                 vis[a[i]] = 0;
    82                 if (vis[a[i]-1] && vis[a[i]+1])
    83                     tmp++;
    84                 else if (!vis[a[i]-1] && !vis[a[i]+1])
    85                     tmp--;
    86             }
    87             ans[q[i].id] = tmp;
    88             L = x, R = y;
    89         }
    90         for(int i=0; i<m; i++)
    91             printf("%d
    ", ans[i]);
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    uniapp 小程序全屏的实现
    element select失效问题 , vue刷新的两种方式
    正则表达式
    vue+element ui中select组件选择失效问题原因与解决方法
    java removeAll和重写equals、hashcode引起的性能问题
    hive sql取差集
    hive获取日期对应的星期
    Hive分组后取组内排名方法row_number
    用Apache Spark和TensorFlow进行的深度学习
    git从已有分支拉新分支开发
  • 原文地址:https://www.cnblogs.com/yohaha/p/5071692.html
Copyright © 2020-2023  润新知